iOS 自带分享功能

作者: cb6a1e2768d1 | 来源:发表于2016-10-12 21:05 被阅读3979次

1.直接分享(指定类型,比如微博)

        // 首先判断新浪分享是否可用
        if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
            return;
        }
        // 创建控制器,并设置ServiceType
        SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
        // 添加要分享的图片
        [composeVC addImage:[UIImage imageNamed:@"SinaWeibo"]];
        // 添加要分享的文字
        [composeVC setInitialText:@"share my Blog"];
        // 添加要分享的url
        [composeVC addURL:[NSURL URLWithString:@"http://chrispangpang.github.io"]];
        // 弹出分享控制器
        [self presentViewController:composeVC animated:YES completion:nil];
        // 监听用户点击事件
        composeVC.completionHandler = ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultDone) {
                NSLog(@"点击了发送");
            }
            else if (result == SLComposeViewControllerResultCancelled)
            {
                NSLog(@"点击了取消");
            }
        };

2.非直接分享(弹出一个板板,自己选)

UIActivityViewController *avc = [[UIActivityViewController alloc]initWithActivityItems:@[@"分享给你",[NSURL URLWithString:@"http://chrispangpang.github.io"]] applicationActivities:nil];
 [self presentViewController:avc animated:YES completion:nil];

模拟器可能会出现此类提醒(真机不会)

Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x129355910) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x12917f210). One of the two will be used. Which one is undefined.

参考
http://www.cocoachina.com/industry/20140425/8233.html

相关文章

网友评论

  • Zcocoa:但是纯文本分享有问题, 有什么办法吗
  • 8aa2e08487c6:可是分享到微信或者QQ一直提示发送失败呢?
  • SAW_:初始化SLComposeViewController之后,应该在判断下SLComposeViewController是否为nil,因为这在坑爹的国行手机上,一些情况下会返回nil,然后就崩溃了:smile:
  • 十一岁的加重:这样就不用配置什么key了?

本文标题:iOS 自带分享功能

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