Creating Shared Links for Safari - Extensions - iOS 9 Swift Programming Cookbook (2015)

iOS 9 Swift Programming Cookbook (2015)

Chapter 5. Extensions

5.2 Creating Shared Links for Safari

Problem

You want to display your own links inside Safari’s shared links on users’ devices.

Solution

Add the new Shared Links Extension target to your existing app and code the extension. It is already prepopulated, so you don’t really have to do much.

Discussion

Shared links are like bookmarks, but lead to content defined in your app or a website. The links are visible inside Safari on iOS when the user taps on the bookmarks button and then on the shared links icon. To get started, create a single view controller project and then add a new target to your project. In the target selection screen, under the iOS main section, choose Application Extension and then Shared Links Extension (see Figure 5-4).

Figure 5-4. Creating a new shared link extension in Xcode

I suggest that you also add some proper icons to your app’s bundle, because your app’s icon will also appear in the list of shared links when iOS shows your shared link. You can just enter “public domain icon” into Google and find some really awesome icons that you can use in your app. Also make sure to add a simple icon to your shared link extension, as our code will show this icon in the list. Your extension’s icon will appear on the left side of the link and your app icon on top right (see Figure 5-5).

Figure 5-5. Your shared link your extension’s icon on the left and your main app’s icon on upper right

Then head to the new file called RequestHandler.swift that has been created in your extension. Xcode has already populated this file with all the code that you need to display your shared link. Uncomment the line that starts with extensionItem.attachments, load your extensions’ icon, and attach it to the extension item like so:

import Foundation

class RequestHandler: NSObject, NSExtensionRequestHandling {

func beginRequestWithExtensionContext(context: NSExtensionContext) {

let extensionItem = NSExtensionItem()

extensionItem.userInfo = [

"uniqueIdentifier": "uniqueIdentifierForSampleItem",

"urlString": "http://reddit.com/r/askreddit",

"date": NSDate()

]

extensionItem.attributedTitle = NSAttributedString(string: "Reddit")

extensionItem.attributedContentText = NSAttributedString(

string: "AskReddit, one of the best subreddits there is")

guard let img = NSBundle.mainBundle().URLForResource("ExtIcon",

withExtension: "png") else {

context.completeRequestReturningItems(nil, completionHandler: nil)

return

}

extensionItem.attachments = [NSItemProvider(contentsOfURL: img)!]

context.completeRequestReturningItems([extensionItem], completionHandler: nil)

}

}

Run your code and then open Safari on the device. Navigate to the bookmarks button and then shared links to see your link displayed (Figure 5-6).

Figure 5-6. Our shared link is displayed in the list

The user can also subscribe or unsubscribe from various shared link providers by tapping on the Subscriptions button (see Figure 5-7):

Figure 5-7. The user can subscribe to or unsubscribe from shared links providers right in Safari

See Also