美文网首页
iOS集成系统分享功能

iOS集成系统分享功能

作者: 番薯大佬 | 来源:发表于2018-10-20 01:00 被阅读14次

    使用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];
    }
    

    注意
    Info.plist设置属性Localized resources can be mixed值为YES,以使取消按钮为中文显示。

    相关文章

      网友评论

          本文标题:iOS集成系统分享功能

          本文链接:https://www.haomeiwen.com/subject/borpzftx.html