先上效果圖,諸位請看:
接下來介紹UIActivityViewController:
1. 創建要分享的數據內容,加在一個數組 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,創建自定義的Activity加在數組Activities中。 customActivity * custom = [[customActivity alloc] initWithTitie:@"且行且珍惜_iOS" withActivityImage:[UIImage imageNamed:@"wang"] withUrl:urlToShare withType:@"customActivity" withShareContext:activityItems]; NSArray *activities = @[custom];
這裡需要注意:不同的 Activity 類型所支持的數據類型不同,當不支持時,應用程序支持的系統服務按鈕就不會出現,比如說打印、添加書簽。
/** 2. //創建分享視圖控制器,初始化UIActivityViewController ActivityItems 在執行activity中用到的數據對象數組。數組中的對象類型是可變的,並依賴於應用程序管理的數據。例如,數據可能是由一個或者多個字符串/圖像對象,代表了當前選中的內容。 Activities 是一個UIActivity對象的數組,代表了應用程序支持的自定義服務。這個參數可以是nil。 */ UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:activities]; // 分享功能(Facebook, Twitter, 新浪微博, 騰訊微博...)需要你在手機上設置中心綁定了登錄賬戶, 才能正常顯示。 //關閉系統的一些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. //判斷系統版本,初始化點擊回調方法 if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){ //初始化Block回調方法,此回調方法是在iOS8之後出的,代替了之前的方法 UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError) { NSLog(@"activityType :%@", activityType); if (completed) { NSLog(@"completed"); } else { NSLog(@"cancel"); } }; // 初始化completionHandler,當post結束之後(無論是done還是cancell)該blog都會被調用 activityVC.completionWithItemsHandler = myBlock; }else{ //此Block回調方法在iOS8.0之後就棄用了,被上面的所取代 UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) { NSLog(@"activityType :%@", activityType); if (completed) { NSLog(@"completed"); } else { NSLog(@"cancel"); } }; // 初始化completionHandler,當post結束之後(無論是done還是cancell)該blog都會被調用 activityVC.completionHandler = myBlock; } 4. //在展現view controller時,必須根據當前的設備類型,使用適當的方法。在iPad上,必須通過popover來展現view controller。在iPhone和iPodtouch上,必須以模態的方式展現。 [self presentViewController:activityVC animated:YES completion:nil];
接下來介紹幾個系統Activity 類型的API,用於自定義title、Image的時候用。
- (void)SystemAPI{ //復制鏈接功能 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = @"需要復制的內容"; //用safari打開網址 [[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(@"添加書簽成功"); } //發送短信 MFMessageComposeViewController *messageComposeViewController = [[MFMessageComposeViewController alloc] init]; messageComposeViewController.recipients = @[@"且行且珍惜_iOS"]; //messageComposeViewController.delegate = self; messageComposeViewController.body = @"你好,我是且行且珍惜_iOS,請多指教!"; messageComposeViewController.subject = @"且行且珍惜_iOS"; //發送郵件 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]; }; //發送推文 TWTweetComposeViewController *tweetComposeViewController = [[TWTweetComposeViewController alloc] init]; [tweetComposeViewController setInitialText:@"夢想還是要有的,萬一實現了呢!-----且行且珍惜_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,下面是Action return UIActivityCategoryAction;} - (NSString *)activityType{ return _type;} - (NSString *)activityTitle { return _title;} - (UIImage *)_activityImage { //這個得注意,當self.activityCategory = UIActivityCategoryAction時,系統默認會渲染圖片,所以不能重寫為 - (UIImage *)activityImage {return _image;} return _image;} - (NSURL *)activityUrl{ return _url;} - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { return YES;} - (void)prepareWithActivityItems:(NSArray *)activityItems { //准備分享所進行的方法,通常在這個方法裡面,把item中的東西保存下來,items就是要傳輸的數據。;} - (void)performActivity { //這裡就可以關聯外面的app進行分享操作了 //也可以進行一些數據的保存等操作 //操作的最後必須使用下面方法告訴系統分享結束了 [self activityDidFinish:YES];}
文章轉自 且行且珍惜_iOS的簡書