iOS使用Social.framework集成新浪微博分享

前言

苹果官方为我们封装好了一个Social.framework框架,可以简单的实现Twitter,Facebook,Flickr,Vimeo以及新浪微博和腾讯微博的分享。


第一步:使用stroboard拖一个button,然后拖对应的点击事件


第二步:在对应的点击方法中,复制如下代码

// 记得导入头文件#import 

- (IBAction)shareButton:(id)sender {
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登录新浪微博账号\n打开设置->新浪微博->登录账号" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:alertAction];
        [self presentViewController:alert animated:YES completion:nil];
     
    }else {
        SLComposeViewController *slVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
        [self presentViewController:slVc animated:YES completion:^{ }];
        // 分享的文字内容
        [slVc setInitialText:@"默认文字"];
        // 分享的图片
        [slVc addImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://cms-bucket.nosdn.127.net/catchpic/2/21/2122f33b01798096b7a8041e3736c2f2.jpg"]]]];
        // 利用block回调返回分享结果
        slVc.completionHandler = ^(SLComposeViewControllerResult result)
        {
            switch (result) {
            case SLComposeViewControllerResultDone:{
                UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alart show];
     
                break;
            }
            case SLComposeViewControllerResultCancelled:{
                UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alart show];
       
            }
      
                break;
            }
        };
    }
}


效果图

iOS使用Social.framework集成新浪微博分享_第1张图片
iOS使用Social.framework集成新浪微博分享_第2张图片
iOS使用Social.framework集成新浪微博分享_第3张图片

备注

  • 因为使用了网络图片,所以要设置info.plist


  • 如果想集成更多的第三方分享:点击这里

你可能感兴趣的:(iOS使用Social.framework集成新浪微博分享)