iOS UIActivityViewController系统分享使用

  • 必须将UIActivityViewController变为属性,否则无法弹出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    NSString *textToShare = @"标题";
    UIGraphicsBeginImageContextWithOptions(self.cardImg.bounds.size, NO, 0.0); //NO,YES 控制是否透明
    [self.cardImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImage *imageToShare = image;
    NSURL *urlToShare = [NSURL URLWithString:@"https://demonxu.com"];
    // 分享的图片不能为空
    NSArray *activityItems = @[textToShare, imageToShare, urlToShare];
    self.activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    // 排除(UIActivityTypeAirDrop)AirDrop 共享、(UIActivityTypePostToFacebook)Facebook
    self.activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop];
    [self presentViewController:self.activityVC animated:YES completion:nil];
    // 通过block接收结果处理
    UIActivityViewControllerCompletionWithItemsHandler completionHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
    NSString *msg;
    if (completed) {
    msg = @"恭喜你,分享成功!";
    }else{
    msg = @"很遗憾,分享失败!";
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享结果" message:msg preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *ture = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertController addAction:ture];
    [self presentViewController:alertController animated:YES completion:^{

    }];
    };
    self.activityVC.completionWithItemsHandler = completionHandler;
本文总阅读量