UIActivityViewController系統(tǒng)原生分享
先上效果圖新蟆,諸位請看:
效果1.gif
效果2.gif
接下來介紹UIActivityViewController:
1. 創(chuàng)建要分享的數(shù)據(jù)內(nèi)容疲吸,加在一個數(shù)組 ActivityItems里锁右。NSString *textToShare =@"我是且行且珍惜_iOS今豆,歡迎關注我扰付!";UIImage *imageToShare = [UIImage imageNamed:@"wang.png"];NSURL *urlToShare = [NSURL URLWithString:@"https://github.com/wslcmk"];NSArray *activityItems = @[urlToShare,textToShare,imageToShare];//自定義 customActivity繼承于UIActivity,創(chuàng)建自定義的Activity加在數(shù)組Activities中校套。? customActivity * custom = [[customActivity alloc] initWithTitie:@"且行且珍惜_iOS" withActivityImage:[UIImage imageNamed:@"wang"] withUrl:urlToShare withType:@"customActivity" withShareContext:activityItems];NSArray *activities = @[custom];
這里需要注意:不同的 Activity 類型所支持的數(shù)據(jù)類型不同,當不支持時蔬充,應用程序支持的系統(tǒng)服務按鈕就不會出現(xiàn)蝶俱,比如說打印、添加書簽饥漫,榨呆,,庸队,
不同的 Activity 類型所支持的數(shù)據(jù)類型.png
/**2. //創(chuàng)建分享視圖控制器积蜻,初始化UIActivityViewController ActivityItems 在執(zhí)行activity中用到的數(shù)據(jù)對象數(shù)組。數(shù)組中的對象類型是可變的彻消,并依賴于應用程序管理的數(shù)據(jù)竿拆。例如,數(shù)據(jù)可能是由一個或者多個字符串/圖像對象宾尚,代表了當前選中的內(nèi)容丙笋。 Activities 是一個UIActivity對象的數(shù)組,代表了應用程序支持的自定義服務煌贴。這個參數(shù)可以是nil御板。 */UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:activities];
// 分享功能(Facebook, Twitter, 新浪微博, 騰訊微博...)需要你在手機上設置中心綁定了登錄賬戶, 才能正常顯示。//關閉系統(tǒng)的一些Activity類型,不需要的功能關掉牛郑。 activityVC.excludedActivityTypes = @[UIActivityTypePostToVimeo ];//Activity 類型又分為“操作”和“分享”兩大類/*
UIKIT_EXTERN NSString *const UIActivityTypePostToFacebook? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypePostToTwitter? ? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypePostToWeibo? ? ? ? NS_AVAILABLE_IOS(6_0);? ? //SinaWeibo
UIKIT_EXTERN NSString *const UIActivityTypeMessage? ? ? ? ? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypeMail? ? ? ? ? ? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypePrint? ? ? ? ? ? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypeCopyToPasteboard? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypeAssignToContact? ? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypeSaveToCameraRoll? NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN NSString *const UIActivityTypeAddToReadingList? NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIActivityTypePostToFlickr? ? ? NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIActivityTypePostToVimeo? ? ? ? NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIActivityTypePostToTencentWeibo NS_AVAILABLE_IOS(7_0);
UIKIT_EXTERN NSString *const UIActivityTypeAirDrop? ? ? ? ? ? NS_AVAILABLE_IOS(7_0);
*/
3.//判斷系統(tǒng)版本,初始化點擊回調(diào)方法if([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0){//初始化Block回調(diào)方法,此回調(diào)方法是在iOS8之后出的怠肋,代替了之前的方法UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError) {NSLog(@"activityType :%@", activityType);if (completed) {NSLog(@"completed"); }else {NSLog(@"cancel"); } };// 初始化completionHandler,當post結(jié)束之后(無論是done還是cancell)該blog都會被調(diào)用 activityVC.completionWithItemsHandler = myBlock;}else{//此Block回調(diào)方法在iOS8.0之后就棄用了淹朋,被上面的所取代UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) {NSLog(@"activityType :%@", activityType);if (completed) {NSLog(@"completed"); }else {NSLog(@"cancel"); } };// 初始化completionHandler笙各,當post結(jié)束之后(無論是done還是cancell)該blog都會被調(diào)用 activityVC.completionHandler = myBlock;}4.//在展現(xiàn)view controller時,必須根據(jù)當前的設備類型础芍,使用適當?shù)姆椒ㄨ厩馈T趇Pad上,必須通過popover來展現(xiàn)view controller者甲。在iPhone和iPodtouch上春感,必須以模態(tài)的方式展現(xiàn)。[self presentViewController:activityVC animated:YES completion:nil];
接下來介紹幾個系統(tǒng)Activity 類型的API虏缸,用于自定義title鲫懒、Image的時候用。
- (void)SystemAPI{//復制鏈接功能UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];pasteboard.string =@"需要復制的內(nèi)容";//用safari打開網(wǎng)址[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://github.com/wslcmk"]];//保存圖片到相冊UIImage *image = [UIImage imageNamed:@"wang"];id completionTarget =self;SEL completionSelector =@selector(didWriteToSavedPhotosAlbum);void *contextInfo =NULL;UIImageWriteToSavedPhotosAlbum(image, completionTarget, completionSelector, contextInfo);//添加書簽NSURL *URL = [NSURL URLWithString:@"https://github.com/wslcmk"];BOOL result = [[SSReadingList defaultReadingList] addReadingListItemWithURL:URL? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? title:@"WSL"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? previewText:@"且行且珍惜_iOS"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? error:nil];if (result) {NSLog(@"添加書簽成功");}//發(fā)送短信MFMessageComposeViewController *messageComposeViewController = [[MFMessageComposeViewController alloc] init];messageComposeViewController.recipients = @[@"且行且珍惜_iOS"];//messageComposeViewController.delegate = self;messageComposeViewController.body =@"你好刽辙,我是且行且珍惜_iOS窥岩,請多指教!";messageComposeViewController.subject =@"且行且珍惜_iOS";//發(fā)送郵件MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];[mailComposeViewController setToRecipients:@[@"mattt@nshipster?com"]];[mailComposeViewController setSubject:@"WSL"];[mailComposeViewController setMessageBody:@"Lorem ipsum dolor sit amet"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isHTML:NO];if([MFMailComposeViewController? canSendMail]){? ? [self presentViewController:mailComposeViewController animated:YES completion:nil];};//發(fā)送推文TWTweetComposeViewController *tweetComposeViewController =[[TWTweetComposeViewController alloc] init];[tweetComposeViewController setInitialText:@"夢想還是要有的,萬一實現(xiàn)了呢!-----且行且珍惜_iOS"];[tweetComposeViewController addURL:[NSURL URLWithString:@"https://github.com/wslcmk"]];[tweetComposeViewController addImage:[UIImage imageNamed:@"wang"]];if ([TWTweetComposeViewController canSendTweet]) {? ? [self presentViewController:tweetComposeViewController animated:YES completion:nil];
}
}
接下來介紹自定義UIActivity宰缤,主要就是重寫了以下方法:
+ (UIActivityCategory)activityCategory{// 決定在UIActivityViewController中顯示的位置颂翼,最上面是AirDrop晃洒,中間是Share,下面是ActionreturnUIActivityCategoryAction;}
- (NSString *)activityType{return _type;}
- (UIImage *)_activityImage {//這個得注意朦乏,當self.activityCategory = UIActivityCategoryAction時球及,系統(tǒng)默認會渲染圖片,所以不能重寫為 - (UIImage *)activityImage {return _image;}return _image;}
- (NSURL *)activityUrl{return _url;}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {returnYES;}
- (void)prepareWithActivityItems:(NSArray *)activityItems {//準備分享所進行的方法呻疹,通常在這個方法里面吃引,把item中的東西保存下來,items就是要傳輸?shù)臄?shù)據(jù)。;}
- (void)performActivity {//這里就可以關聯(lián)外面的app進行分享操作了//也可以進行一些數(shù)據(jù)的保存等操作//操作的最后必須使用下面方法告訴系統(tǒng)分享結(jié)束了[self activityDidFinish:YES];}