標籤:
註:此教程來源於http://www.raywenderlich.com的《iOS8 by Tutorials》
1.準備
這次例子來源於國外的圖片分享網站Imgur.com
首先現在Imgur上註冊你的帳號,之後按下面的步驟來,點擊settings選擇applications中建立app,即下面的create your own,這裡上面的App是我得Demo之後點擊register註冊你的應用輸入你App的名字後選擇第二項,後面輸入你的郵箱與面述,點擊submit這裡請記好App的Client ID和Client secret到此,即註冊App成功,可以在你的App中調用Imgur的API
2.設定分享擴充前首先開啟源碼JMImgure_original,這是我還未設定分享擴充的源碼,你可以自己將下面過程全部演練一遍,但前提是你需要將警告處的Group ID與Client ID更改為你自己的,否則會報錯!!!你需要建立你自己的Group ID需要注意的地方如下:以及下面的源碼部分:
1 //AppDelegate.m 2 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 3 // Override point for customization after application launch. 4 #warning 在這裡設定成你的CLIENT ID 5 [RWTImgurService setClientId:@"CLIENT ID"]; 6 7 [[UITabBar appearance] setTintColor:[UIColor imgvueGreen]]; 8 [[UINavigationBar appearance] setTintColor:[UIColor imgvueGreen]]; 9 [[UIProgressView appearance] setTintColor:[UIColor imgvueGreen]];10 11 return YES;12 }13 14 //RWTSavedImageService.m15 - (NSURL *)URLForDirectoryWithName:(NSString *)name {16 #warning 在這裡設定你的APP GROUP ID17 NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.qq100858433.JMImgvue"];18 containerURL = [containerURL URLByAppendingPathComponent:name];19 20 if (![[NSFileManager defaultManager] fileExistsAtPath:containerURL.path]) {21 [[NSFileManager defaultManager] createDirectoryAtURL:containerURL withIntermediateDirectories:NO attributes:nil error:nil];22 }23 24 return containerURL;25 }
3.本文如果已經可以正常運行,那麼就可以進行分享擴充的設定首先依然是添加Share Extension 接下來輸入App分享的名字後點擊Finish,點擊Activate,之後為分享擴充也設定App Groups下面我們來看share檔案夾下面的Info檔案中的NSExtension項,系統預設如下需要關心的是NSExtensionActivationRule,Apple預設設定為TRUEPREDICATE,意思就是這個擴充將一直有效,這方便與開發,但是實際中Apple不允許這個值預設如此,App擴充最基本的要求就是輕量,因此如果你直接預設的話,提交到App Store是不會過審的,在這裡我們做這樣的修改意思是每次選擇的附件只能選一個,這裡就設定一處即可,更多關於NSExtension請點擊此處:App Extension Keys 接下來進行擴充的實現,Apple預設提供的類為SLComposeServiceViewController,它做出來與Sina微博類似
直接使用即可,在對SharViewController編程前,由於我們要使用原有的標頭檔和第三方庫,所以將cocoapods的Podfile檔案做如下更改,增加你建的擴充項目
1 # Uncomment this line to define a global platform for your project 2 # platform :ios, ‘6.0‘ 3 inhibit_all_warnings! 4 5 target ‘JMImgure‘ do 6 7 pod ‘SDWebImage‘ 8 pod ‘AFNetworking‘ 9 10 end11 12 target ‘JMImgure Share‘ do13 14 pod ‘SDWebImage‘15 pod ‘AFNetworking‘16 17 end
之後在命令列重新運行pod install即可,下面是更改後的ShareViewController.m,有注釋說明
1 // 2 // ShareViewController.m 3 // JMImgure Share 4 // 5 // Created by JackMa on 15/11/29. 6 // Copyright © 2015年 JackMa. All rights reserved. 7 // 8 9 #import <MobileCoreServices/MobileCoreServices.h>10 #import "ShareViewController.h"11 12 @interface ShareViewController ()13 14 @property (nonatomic, strong) UIImage *image;15 16 @end17 18 @implementation ShareViewController19 20 - (void)viewDidLoad {21 //擷取inputItems,在這裡itemProvider是你要分享的圖片22 NSExtensionItem *firstItem = self.extensionContext.inputItems.firstObject;23 NSItemProvider *itemProvider;24 if (firstItem) {25 itemProvider = firstItem.attachments.firstObject;26 }27 28 //這裡的kUTTypeImage代指@"public.image",也就是從相簿擷取的圖片類型29 //這裡的kUTTypeURL代指網站連結,如在Safari中開啟,則應該拷貝儲存當前網頁的連結30 if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {31 [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {32 if (!error) {33 //對itemProvider夾帶著的URL進行解析34 NSURL *url = (NSURL *)item;35 [UIPasteboard generalPasteboard].URL = url;36 }37 }];38 }39 if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {40 [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {41 if (!error) {42 //對itemProvider夾帶著的圖片進行解析43 NSURL *url = (NSURL *)item;44 NSData *imageData = [NSData dataWithContentsOfURL:url];45 self.image = [UIImage imageWithData:imageData];46 }47 }];48 }49 }50 51 //設定Post是否有效,當你每次輸入內容的時候,都會調用此方法52 - (BOOL)isContentValid {53 if (self.image) {54 return YES;55 } else {56 return NO;57 }58 }59 60 //設定點擊Post後的動作61 - (void)didSelectPost {62 [self shareImage];63 }64 65 //在這裡設定彈出sheet的底部,要求用SLComposeSheetConfigurationItem的對象66 - (NSArray *)configurationItems {67 SLComposeSheetConfigurationItem *configItem = [[SLComposeSheetConfigurationItem alloc] init];68 configItem.title = @"連結將被拷貝到剪貼簿";69 return @[configItem];70 }71 72 - (void)shareImage {73 //在這裡寫圖片上傳的代碼74 }
在運行時選擇Photos後Run
至此,完成了分享擴充最基本的設定。源碼有原始版本(original)和設定好的share版本供參考
源碼點擊這裡
iOS8中添加的extensions總結(二)