配置Share Extension
設置允許發(fā)送的數(shù)據(jù)類型:text、url扛稽、image吁峻、mp3、mp4在张、pdf用含、word、excel帮匾、ppt
處理Share Extension中的數(shù)據(jù)啄骇。
Share Extension中默認都會有一個數(shù)據(jù)展現(xiàn)的UI界面。該界面繼承SLComposeServiceViewController這個類型辟狈,如:(這是系統(tǒng)幫我們生成的)
#import <UIKit/UIKit.h>
#import <Social/Social.h>
@interface ShareViewController : SLComposeServiceViewController
@end
當然我們也可以自定義新的控制器肠缔,并在Share Extension的plist里做下對應的匹配
處理Share Extension中的數(shù)據(jù)
分享界面頂部包括了標題夏跷、取消(Cancel)按鈕和提交(Post)按鈕。然后下面跟著左邊就是一個文本編輯框明未,右邊就是一個圖片顯示控件槽华。那么,每當用戶點擊取消按鈕或者提交按鈕時趟妥,都會分別觸發(fā)下面的方法:
/**
* 點擊取消按鈕
*/
- (void)didSelectCancel
{
[super didSelectCancel];
}
/**
* 點擊提交按鈕
*/
- (void)didSelectPost
{
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
在這兩個方法里面可以進行一些自定義的操作猫态。一般情況下,當用戶點擊提交按鈕的時候披摄,擴展要做的事情就是要把數(shù)據(jù)取出來亲雪,并且放入一個與Containing App(** 容器程序,盡管蘋果開放了Extension疚膊,但是在iOS中extension并不能單獨存在义辕,要想提交到AppStore,必須將Extension包含在一個App中提交寓盗,并且App的實現(xiàn)部分不能為空,這個包含Extension的App就叫Containing app灌砖。Extension會隨著Containing App的安裝而安裝,同時隨著ContainingApp的卸載而卸載傀蚌。)共享的數(shù)據(jù)介質中(包括NSUserDefault基显、Sqlite偎血、CoreData)衍菱,要跟容器程序進行數(shù)據(jù)交互需要借助AppGroups服務,下面先來看看怎么獲取擴展中的數(shù)據(jù)退唠。
在ShareExtension中箩艺,UIViewController包含一個ExtensionContext這樣的上下文對象窜醉,NSExtensionContext的結構比較簡單,包含一個屬性和三個方法艺谆。其說明如下:
類的下面還定義了一些通知酱虎,這些通知都是跟宿主程序的行為相關,在設計擴展的時候可以根據(jù)這些通知來進行對應的操作擂涛。其說明如下:
從inputItems中獲取數(shù)據(jù)
inputItems是包含NSExtensionItem類型對象的數(shù)組读串。
NSExtensionItem包含四個屬性:
對應userInfo結構中的NSExtensionItem屬性的鍵名如下:
為了要取到宿主程序提供的數(shù)組,那么只要關注loadItemTypeIdentifier:options:completionHandler方法的使用即可撒妈。有了上面的了解恢暖,那么接下來就是對inputItems進行數(shù)據(jù)分析并提取了,這里以一個鏈接的分享為例狰右,改寫視圖控制器中的didSelectPost方法杰捂。看下面的代碼:
- (void)didSelectPost
{
__block BOOL hasExistsUrl = NO;
[self.extensionContext.inputItems enumerateObjectsUsingBlock:^(NSExtensionItem * _Nonnull extItem, NSUInteger idx, BOOL * _Nonnull stop) {
[item.attachments enumerateObjectsUsingBlock:^(NSItemProvider * _Nonnull itemProvider, NSUInteger idx, BOOL * _Nonnull stop) {
if ([itemProvider hasItemConformingToTypeIdentifier:@"public.url"])
{
[itemProvider loadItemForTypeIdentifier:@"public.url"
options:nil
completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if ([(NSObject *)item isKindOfClass:[NSURL class]])
{
NSLog(@"分享的URL = %@", item);
}
}];
hasExistsUrl = YES;
*stop = YES;
}
}];
if (hasExistsUrl)
{
*stop = YES;
}
}];
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
// [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
上面的例子中遍歷了extensionContext的inputItems數(shù)組中所有NSExtensionItem對象棋蚌,然后從這些對象中遍歷attachments數(shù)組中的所有NSItemProvider對象嫁佳。匹配第一個包含public.url標識的附件(具體要匹配什么資源挨队,數(shù)量是多少皆有自己的業(yè)務所決定)。
- 注意:在上面代碼中注釋了[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];這行代碼蒿往,主要是使到視圖控制器不被關閉盛垦,等到實現(xiàn)相應的處理后再進行調用該方法,對分享視圖進行關閉瓤漏。