1、创建其分享的标题,内容或图片或链接
NSString *testToShare = @"分享的标题";
UIImage *imageToShare = [UIImage imageNamed:@"client"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.baidu.com"];
NSArray *activityItems = @[testToShare, imageToShare, urlToShare];
2、创建控制器,并将分享内容数组传给此控制器
UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
3、很多系统自带的分享功能如果不需要的可以隐去,即设置不出现在活动项目中的选项数组
activityVc.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAirDrop, UIActivityTypeAddToReadingList];
4、推出控制器及分享回调
activityVc.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
if (completed) {
NSLog(@"分享成功");
}else{
NSLog(@"分享取消");
}
};
[self presentViewController:activityVc animated:YES completion:nil];
5、系统的分享文字默认是英文的,要想改成中文的,修改info.plist中的 Localization native development region字段为China即可