Swift share postotfacebook activity7/25/2023 ![]() ![]() Swift will check this for you: if we had written "shareTaped" by accident – missing the second P – Xcode will refuse to build our app until we fix the typo. What it does is tell the Swift compiler that a method called "shareTapped" will exist, and should be triggered when the button is tapped. The part in #selector bears explaining a bit more, because it's new and unusual syntax. The action parameter is saying "when you're tapped, call the shareTapped() method," and the target parameter tells the button that the method belongs to the current view controller – self. The target and action parameters go hand in hand, because combined they tell the UIBarButtonItem what method should be called. action system item displays an arrow coming out of a box, signaling the user can do something when it's tapped. to have code completion tell you the many other options available. On the right we create a new instance of the UIBarButtonItem data type, setting it up with three parameters: a system item, a target, and an action. In this case, we're setting the right bar button item, which is a button that appears on the right of the navigation bar when this view controller is visible. This navigation item is used by the navigation bar so that it can show relevant information. This is easily split into two parts: on the left we're assigning to the rightBarButtonItem of our view controller's navigationItem. You’ll get an error for a moment, but that’s OK please read on. action, target: self, action: #selector(shareTapped)) Directly beneath the title = line, navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem. Now launch it in Xcode, open the file DetailViewController.swift, and find the viewDidLoad() method. First, take a copy of your existing Project1 folder (the whole thing), and rename it to be Project3. By default, a UINavigationController has a bar across the top, called a UINavigationBar, and as developers we can add buttons to this navigation bar that call our methods. Project 1, if you recall, used a UINavigationController to let users move between two screens. But before we touch UIActivityViewController, we first need to give users a way to trigger sharing, and the way we’re going to use it is to add a bar button item. It even hooks into AirDrop and the iOS extensions system so that other apps can read the image straight from us.īest of all, it takes just a handful of lines of code to make it all work. This component is called UIActivityViewController: you tell it what kind of data you want to share, and it figures out how best to share it.Īs we're working with images, UIActivityViewController will automatically give us functionality to share by iMessage, by email and by Twitter and Facebook, as well as saving the image to the photo library, assigning it to contact, printing it out via AirPrint, and more. As a result, it should be your first port of call when adding sharing to an app. Sharing things using iOS uses a standard, powerful component that other apps can plug into. ![]()
0 Comments
Leave a Reply. |