

- (void)viewDidLoad - (BOOL)isContentValid - (void)didSelectPost - (NSArray *)configurationItems
以上三步就能實現一個簡單的share Extension 編譯運行之後,我們會看到一個很簡單的分享頁面,當然現在我們還沒有做任何東西,那麼怎麼把我們要分享的東西,或者其他操作和我們的應用關聯呢或者上傳到我們的服務器呢?
________________________下面講如何分享出去網頁的內容______________________
4 首先我們需要獲取到需要分享的數據(已分享網頁為例子) 首先要判斷數據類型是否是我們所想要的
- (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here
NSExtensionItem * imageItem = [self.extensionContext.inputItems firstObject];
if(!imageItem)
{
return NO;
}
NSItemProvider * imageItemProvider = [[imageItem attachments] firstObject];
if(!imageItemProvider)
{
return NO;
}
if([imageItemProvider hasItemConformingToTypeIdentifier:@"public.url"]&&self.contentText)
{
return YES;
}
return YES;
}
- (void)viewDidLoad
{
NSExtensionItem * imageItem = [self.extensionContext.inputItems firstObject];
NSItemProvider * imageItemProvider = [[imageItem attachments] firstObject];
if([imageItemProvider hasItemConformingToTypeIdentifier:(NSString*)kUTTypeURL])
{
NSLog(@"xxxxxxxx");
[imageItemProvider loadItemForTypeIdentifier:(NSString*)kUTTypeURL options:nil completionHandler:^(NSURL* imageUrl, NSError *error) {
//在這兒做自己的工作
NSLog(@"xxxxxxx123 = %@",imageUrl.absoluteString);
urlString = imageUrl.absoluteString;
}];
}
}didSelectPost 代碼實現
- (void)didSelectPost {
NSExtensionItem * imageItem = [self.extensionContext.inputItems firstObject];
//完成一些自己的操作 保存,添加 http請求
UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"11" message:@"111" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alter animated:YES completion:^{
}];
NSExtensionItem * outputItem = [imageItem copy];
outputItem.attributedContentText = [[NSAttributedString alloc] initWithString:self.contentText attributes:nil];
NSArray * outPutitems= @[outputItem];
[self.extensionContext completeRequestReturningItems:outPutitems completionHandler:nil];
}7 configurationItems 代碼實現 這個方法給了我們一種默認的,然我們可以選擇一些內容的種方式,就是下面的123

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+teO79zEyM7XEY2VsbKOsztLDx7/J0tS1r7P20ru49tDCtcRWQ6OsPC9wPgo8cD48YnI+CjwvcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140929/2014092909154975.jpg" alt="\">
在這裡我只彈出了一個空的VC在這個界面我們可以添加任何我們想要的東西,當然,別添加太復雜的東西
實現方法如下
- (NSArray *)configurationItems {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
SLComposeSheetConfigurationItem * oneItem = [[SLComposeSheetConfigurationItem alloc]init];
oneItem.title = @"123";
oneItem.valuePending = NO;
oneItem.tapHandler = ^(void)
{
ListViewController * listVC = [[ListViewController alloc] init];
[self pushConfigurationViewController:listVC];
};
return @[oneItem];
}