UIActivityViewController 使用大致使用步驟:
-
1 設(shè)定分享的內(nèi)容,比如:
NSString *shareTitle = @"分享的標題"; UIImage *shareImage = [UIImage imageNamed:@"me"]; NSURL *shareUrl = [NSURL URLWithString:@"http://www.reibang.com/u/acdcce712303"]; NSArray *activityItems = @[shareTitle, shareImage, shareUrl]; // 必須要提供url 才會顯示分享標簽否則只顯示圖片
-
2 創(chuàng)建分享的控制器
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
-
3 設(shè)定不想顯示的平臺和功能
-(NSArray *)excludetypes{ NSMutableArray *excludeTypesM = [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop, UIActivityTypeOpenInIBooks]]; if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) { [excludeTypesM addObject:UIActivityTypeMarkupAsPDF]; } return excludeTypesM; } activityVC.excludedActivityTypes = [self excludetypes];
-
4 設(shè)置操作回調(diào),用戶點擊 菜單按鈕后事件執(zhí)行完成會回調(diào)這個block
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) { NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError); };
5 顯示分享菜單
[self presentViewController:activityVC animated:YES completion:nil];
Snip20180304_2.png
demo地址: https://github.com/TangChangTomYang/TestSystemShare.git