Gold field (GitHub sample source code)
In the previous blog, there is a content blocking section about Safari extensibility, here---The safari ad blocker for IOS 9 (content Blocker), and today we'll take a look at Another extension point for Safari (plug-in)-Shared Links.
OK, let's take a look at how to use this feature:
- Open Safari, click the Bookmarks button (like an open book button)
Figure 1 Bookmark button
- You can see all the shared Links in the current device by selecting the "@" shared column.
Figure 2 Shared Links Example
- If you don't see your app's Share link, you can click the Subscriptions button in the lower right corner to activate your app.
Figure 3 Subscriptions
4. Select a link to open it directly in Safari, it's that simple L
From the above steps and can be seen, shared links as its name, share the link, like we in QQ, (Xenophilia a little facebook,twitter) and other IM software, often have friends, with the incident over the various network links, As it workers often want to see a variety of the latest technical pages, after a period of time, if you want to continue to review, basic can only go to the history or Safari bookmarks (of course if you have manually added). Now that we have a Shared links, we can not be swollen, the cost of energy, everything is so automatic intelligent ~ lazy people always have lazy people's own way ~
In summary, content blocking is used to block the contents, and shared links is used to share content.
Get to the chase and start!
First you need to create an extension in the project (extension cannot exist independently, need a containing App, even if it is empty, as long as you are not too ugly), such as choosing shared Links Extension.
Figure 4 Extension Template
The system will generate the RequestHandler class file by default, adding methods in RequestHandler-(void) Beginrequestwithextensioncontext: (Nsextensioncontext *) Context, when you open the "@" shared column in Safari, the IOS system automatically calls this method to add the links you need to share.
Examples are as follows:
-(void) Beginrequestwithextensioncontext: (Nsextensioncontext *) Context {
Nsextensionitem *extensionitem = [[Nsextensionitem alloc] init];
Extensionitem.userinfo = @{@ "uniqueidentifier": @ "Uniqueidentifierforsampleitem" @ "urlstring" @ "http:// Www.kdanmobile.com "@" date ": [NSDate Date]};
Extensionitem.attributedtitle = [[Nsattributedstring alloc] initwithstring:@ "kdanmobile title"];
Extensionitem.attributedcontenttext = [[Nsattributedstring alloc] initwithstring:@ "kdanmobile description text"];
[Context completerequestreturningitems:@[Extensionitem] completionhandler:nil];
}
As can be seen, each link we share corresponds to a Nsextensionitem object, where Attributedtitle is the title that link displays in the Shared Links list. Attributedcontenttext describes the content for link, UserInfo is a link that needs to be shared, so if you need to share multiple links, we just need to create multiple Nsextensionitem objects.
Start with a demo run:
Figure 5 Example
Small details
1. If you need to modify the app extension, simply modify the value of the bundle display name in Info.plist, as shown in:
<key>CFBundleDisplayName</key>
<string>shared links</string>
2. If you want to change the blank icon on the left side of the Sharedlinks list to a style, such as a company logo, just add the following code:
Extensionitem. = @[[[Nsitemprovider alloc] initwithcontentsofurl:[[nsbundle Mainbundle] urlforresource:@ "Custormlogo" withExtension : @ "PNG"]];
3. The extension and containing apps each have their own data containers, although the extensions are embedded inside the containing app, but by default they do not exchange data with each other. In order to realize the containing app and the extended data sharing, we need to use the app Group. When the app group is turned on, you can use the Nsuserdefaults method to access the shared area, as in the following code, note that it is not [Nsuserdefaults Standarduserdefaults]:
Nsuserdefaults *shareduserdefault = [[Nsuserdefaults alloc]
initwithsuitename:@ "Group.com.kdanmobile.ExtensionDemo"];
You can also access the shared data area using the Nsfilemanager Containerurlforsecurityapplicationgroupidentifier method:
Nsurl *containerurl = [[Nsfilemanager Defaultmanager]
containerurlforsecurityapplicationgroupidentifier:@ "Group.com.kdanmobile.ExtensionDemo"];
Reference links
https://developer.apple.com/videos/wwdc/2015/?id=511
Https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewInSafari/Articles/Safari_9.html
All rights reserved, the transfer must indicate the author (Jintian) and the original source (original)
Shared Links Extension IOS 9 (Safari Extensibility)