简单使用
NSString*textToShare =@"要分享的文本内容";
UIImage*imageToShare = [UIImageimageNamed:@"蒙版组 22"];// 本地图片可直接用
UIImage*imageToShare1 = [UIImageimageNamed:@"home_bg"];// 本地图片可直接用
NSURL*urlToShare = [NSURLURLWithString:@"http://www.baidu.com"];
NSArray*activityItems =@[imageToShare,imageToShare1];
// NSArray *activityItems =@[ imageToShare,imageToShare1,urlToShare,textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController: activityVC animated:YES completion:nil];
分享pdf
首先需要将pdf保存到本地然后,获取本地路径,将其填写至分享的内容里面,系统会自动取内容,和计算分享内容的尺寸,名称等
心得总结
分享多个内容时需要将其保存到本地,然后获取本地路径再分享即可
ipad分享需要进行处理否则崩溃,代码如下(可以自己变形)
崩溃信息
Failed to request default share mode for fileURL:file:///private/var/mobile/Containers/Data/Application
Failed to request default share mode for fileURL:file:///private/var/mobile/Containers/Data/Application
error:Error Domain=NSOSStatusErrorDomain Code=-10814 "(null)" UserInfo={_LSLine
LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54
Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self presentViewController:activityVC animated:YES completion:nil];
} else {
activityVC.popoverPresentationController.sourceView = self.view;
activityVC.popoverPresentationController.sourceRect = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
activityVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:activityVC animated:YES completion:nil];
}
网友评论