iOS整合系統分享功能
阿新 • • 發佈:2018-12-25
使用UIActivityViewController
整合系統分享功能
/**
分享
@param title 標題
@param image 圖示
@param url 連結
@param target r檢視控制器
@param complete 回撥
*/
+ (void)shareWithTitle:(NSString *)title image:(UIImage *)image url:(NSString *)url target:(UIViewController *)target complete:(void (^)(BOOL isSuccess, UIActivityType type) )complete
{
// 分享內容
NSString *shareTitle = title;
UIImage *shareImage = image;
NSURL *shareUrl = [NSURL URLWithString:url];
NSArray *activityItemsArray = @[shareTitle, shareImage, shareUrl];
//
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems: activityItemsArray applicationActivities:nil];
activityVC.modalInPopover = YES;
// 禁用分享渠道
// activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypeAirDrop, UIActivityTypeOpenInIBooks];
//
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
if (complete) {
complete(completed, activityType);
}
};
//
[target presentViewController:activityVC animated:YES completion:nil];
}