美文网首页
【IOS】利用UIActivityViewController实

【IOS】利用UIActivityViewController实

作者: 雨影 | 来源:发表于2017-08-24 13:17 被阅读100次

直接上代码:

    NSURL *videoURL = ....

    NSArray *activityItems = @[videoURL];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    
    //ipad兼容
    UIPopoverPresentationController *popover = activityVC.popoverPresentationController;

    if (popover) {
        popover.sourceView =....;
        popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
    }
    
    [self presentViewController:activityVC animated:YES completion:nil];

是不是非常简单?

附录:
分享本地图片:

 NSData *date = [NSData dataWithContentsOfFile:item.imageUrl.absoluteString];
    NSArray *activityItems = @[date];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:nil];

分享链接:

    NSString *textToShare = @"文本内容";
    UIImage *imageToShare = [UIImage imageNamed:@"imagename"];
    NSURL *urlToShare = [NSURL URLWithString:@"http:baidu.com"];
    NSArray *activityItems = @[textToShare, imageToShare, urlToShare];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems
                                                                            applicationActivities:nil];
    //不出现在活动项目
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
    
    //给activityVC的属性completionHandler写一个block。
    //用以UIActivityViewController执行结束后,被调用,做一些后续处理。
    UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * activityError)
    {
        if (completed)
        {
            LyLog(@"completed");
        }
        else
        {
            LyLog(@"cancel");
        }
    };
    
    // 初始化completionHandler,当post结束之后(无论是done还是cancell)该blog都会被调用
    activityVC.completionWithItemsHandler = myBlock;
    
    UIViewController * rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootVc presentViewController:activityVC animated:TRUE completion:nil];
    

相关文章

网友评论

      本文标题:【IOS】利用UIActivityViewController实

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