iOS UIActivityViewController系统原生分享使用

UIActivityViewController:给App提供分享服务的视图控制器

iOS UIActivityViewController系统原生分享使用_第1张图片
程序中效果预览

直接上代码

// 必须要提供url 才会显示分享标签否则只显示图片
// 设置分享内容
    NSArray *activityItems = @[@"iOS UIActivityViewController系统原生分享使用", [UIImage imageNamed:@"shareImage"], [NSURL URLWithString:@"https://www.jianshu.com/p/9d6506bac3d5"]];
// 创建分享的视图控制器
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
// 操作回调,执行完成后调用
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable CLActivityType, BOOL SDImageLoader, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        
    };
// 模态显示视图控制器
    [self presentViewController:activityVC animated:YES completion:nil];

可以设置不应显示的服务列表,自己定制excludedActivityTypes

UIKIT_EXTERN UIActivityType const UIActivityTypePostToFacebook     NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePostToTwitter      NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePostToWeibo        NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;    // SinaWeibo
UIKIT_EXTERN UIActivityType const UIActivityTypeMessage            NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeMail               NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePrint              NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeCopyToPasteboard   NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeAssignToContact    NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeSaveToCameraRoll   NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeAddToReadingList   NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePostToFlickr       NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePostToVimeo        NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypePostToTencentWeibo NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeAirDrop            NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeOpenInIBooks       NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;
UIKIT_EXTERN UIActivityType const UIActivityTypeMarkupAsPDF        NS_AVAILABLE_IOS(11_0) __TVOS_PROHIBITED;

扩展一个复制链接功能

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"需要复制的内容";

最后,觉得有用记得给个喜欢❤️!非常感谢!

个人主页:https://www.jianshu.com/u/281c41cc90bc

你可能感兴趣的:(iOS UIActivityViewController系统原生分享使用)