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

前言

蘋果官方為我們封裝好了一個Social.framework框架,可以簡單的實現Twitter,Facebook,Flickr,Vimeo以及新浪微博和騰訊微博的分享。


第一步:使用stroboard拖一個button,然后拖對應的點擊事件


第二步:在對應的點擊方法中,復制如下代碼

// 記得導入頭文件#import <Social/Social.h>

- (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;
            }
        };
    }
}


效果圖


備注

  • 因為使用了網絡圖片,所以要設置info.plist


  • 如果想集成更多的第三方分享:點擊這里

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容