Objective-C
/**
调用iOS系统的分享
@param path 文件路径
@param aSourceView 源视图
*/
- (void)shareWithPath: (NSURL *)path sourceView:(UIView *)aSourceView {
// 把不需要的UIActivityType都写到excludedActivities中
NSArray *excludedActivities = @[UIActivityTypePostToTwitter, // 发送到Twitter
UIActivityTypePostToFacebook, // 发送到Facebook
UIActivityTypePostToWeibo, // 发送到新浪微博
UIActivityTypePostToFlickr, // 发送到Flickr
UIActivityTypePostToVimeo, // 发送到Vimeo
UIActivityTypePostToTencentWeibo,// 发送到腾讯微博
UIActivityTypeMail, // 邮件
UIActivityTypeAirDrop, // AirDrop传输
UIActivityTypeMessage, // 短信
UIActivityTypePrint, // 打印
UIActivityTypeCopyToPasteboard,// 拷贝到粘贴板
UIActivityTypeAssignToContact, // 指定到联系人
UIActivityTypeSaveToCameraRoll,// 保存到相机胶卷
UIActivityTypeAddToReadingList,// 添加到阅读列表
UIActivityTypeOpenInIBooks // 使用Books打开
];
// 初始化分享控制器
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[path] applicationActivities:nil];
// 设置排除类型
controller.excludedActivityTypes = excludedActivities;
// 处理分享回调, 如果不需要可以设置可以为nil
controller.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
NSLog(@"activityType=%@", activityType);
NSLog(@"completed=%d", completed);
NSLog(@"returnedItems=%@", returnedItems);
NSLog(@"activityError=%@", activityError);
};
// 适配iPad,否则会carsh
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
controller.popoverPresentationController.sourceView = aSourceView;
}
// 显示分享控制器
[self presentViewController:controller animated:YES completion:nil];
}
使用
- (IBAction)share:(UIButton *)sender {
NSURL *path = [[NSBundle mainBundle] URLForResource:@"bkg" withExtension:@"png"];
[self shareWithPath:path sourceView:sender];
}
Swift
/// 调用iOS系统的分享
///
/// - Parameters:
/// - withPath: 文件路径
/// - sourceView: 源视图
func share(withPath: URL, sourceView: UIView) {
/// 把不需要的UIActivityType都写到excludedActivities中
let excludedActivities: [UIActivityType] = [
.postToTwitter, // 发送到Twitter
.postToFacebook, // 发送到Facebook
//.postToWeibo, // 发送到新浪微博
.postToFlickr, // 发送到Flickr
.postToVimeo, // 发送到Vimeo
//.postToTencentWeibo, // 发送到腾讯微博
//.mail, // 邮件
.airDrop, // AirDrop传输
//.message, // 短信
.print, // 打印
.copyToPasteboard, // 拷贝到粘贴板
.assignToContact, // 指定到联系人
//.saveToCameraRoll, // 保存到相机胶卷
.addToReadingList, // 添加到阅读列表
.openInIBooks // 使用Books打开
]
/// 初始化分享控制器
let controller = UIActivityViewController(activityItems: [withPath], applicationActivities: nil)
/// 设置排除类型
controller.excludedActivityTypes = excludedActivities
/// 处理分享回调, 如果不需要可以设置可以为nil
controller.completionWithItemsHandler = {
(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, activityError: Error?) in
}
/// 适配iPad,否则会carsh
if UIDevice.current.userInterfaceIdiom == .pad {
controller.popoverPresentationController?.sourceView = sourceView
}
/// 显示分享控制器
present(controller, animated: true, completion: nil)
}
使用
@IBAction func share(_ sender: UIButton) {
let path = Bundle.main.url(forResource: "myImage", withExtension: "png")!
share(withPath: path, sourceView: sender)
}
还不快抢沙发