美文网首页
iOS 自带分享的使用

iOS 自带分享的使用

作者: 陈贺 | 来源:发表于2017-03-08 16:08 被阅读0次

ios自带分享功能,默认可以使用新浪微博

#pragma mark 点击屏幕开始分享
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1. 判断服务的类型是否可用
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"请到设置中, 先添加新浪微博账号, 然后再分享");
    }
    
    //2. 创建分享控制器
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    //2.1 文字
    [composeVC setInitialText:@"---------分享的文字"];
    
    //2.2 图像i
    [composeVC addImage:[UIImage imageNamed:@"分享的图片"]];
    
    //2.3 网址
    [composeVC addURL:[NSURL URLWithString:@"分享的链接"]];
    
    //3. 弹出分享控制器
    [self presentViewController:composeVC animated:YES completion:nil];
}

不是直接分享的可以弹出来一个面板:

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

这是完整的方法:

- (void)buttonclickWithtitlestring:(NSString *)titlestring Withimageurl:(NSString *)imagestring Withurlstring:(NSString *)urlstring
{
    
    NSString *info = titlestring;
    
//    UIImage *image=[UIImage imageNamed:imagestring];
    
    NSString *imageName = imagestring;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];
    
    UIImage *image = [UIImage imageWithContentsOfFile: path];
    
    NSURL *url = [NSURL URLWithString:urlstring];
    
    NSArray *postItems=@[info, image, url];
    
    UIActivityViewController *avc = [[UIActivityViewController alloc]initWithActivityItems:postItems applicationActivities:nil];
    
    [self presentViewController:avc animated:YES completion:nil];
}

显示效果

WechatIMG114.jpeg 678295F57B73C154B7EFD0378816452C.png
参考
http://www.cocoachina.com/industry/20140425/8233.html

相关文章

网友评论

      本文标题:iOS 自带分享的使用

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