1. 把要分享的内容放入一个Array中
NSMutableArray *activityItems = [NSMutableArray array];UIImage *imageItem = [UIImage imageNamed:@"YOUR_IMAGE"];NSString *textItem = @"YOUR_TEXT";NSURL *urlItem = [NSURL URLWithString:@"YOUR_URL"];[activityItems addObject:imageItem];[activityItems addObject:textItem];[activityItems addObject:urlItem];
2. 使用这个Array初始化UIActivityViewController,并显示分享页面
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];UIActivityViewControllerCompletionHandler activityBlock = ^(NSString *activityType, BOOL completed) { [activityViewController dismissViewControllerAnimated:YES completion:nil];};activityViewController.completionHandler = activityBlock;[self presentViewController:activityViewController animated:YES completion:nil];
OK.
分享页面包含的APP会根据你所分享的内容的数据类型,自动显示(有些APP要在分享界面的"More"按钮里手动开启)。
如果要限制分享页面上显示的APP,可在present activityViewController之前再给它赋一个excludedActivityTypes属性值
NSArray *excludeActivities = @[UIActivityTypeAirDrop, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo]; activityViewController.excludedActivityTypes = excludeActivities;
参考(需FQ):http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/