前言
蘋果官方為我們封裝好了一個(gè)Social.framework框架,可以簡(jiǎn)單的實(shí)現(xiàn)Twitter,Facebook,Flickr,Vimeo以及新浪微博和騰訊微博的分享径缅。
第一步:使用stroboard拖一個(gè)button枷颊,然后拖對(duì)應(yīng)的點(diǎn)擊事件
第二步:在對(duì)應(yīng)的點(diǎn)擊方法中祟峦,復(fù)制如下代碼
// 記得導(dǎo)入頭文件#import <Social/Social.h>
- (IBAction)shareButton:(id)sender {
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"請(qǐng)登錄新浪微博賬號(hào)\n打開設(shè)置->新浪微博->登錄賬號(hào)" 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:^{ }];
// 分享的文字內(nèi)容
[slVc setInitialText:@"默認(rèn)文字"];
// 分享的圖片
[slVc addImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://cms-bucket.nosdn.127.net/catchpic/2/21/2122f33b01798096b7a8041e3736c2f2.jpg"]]]];
// 利用block回調(diào)返回分享結(jié)果
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;
}
};
}
}
效果圖
備注
-
因?yàn)槭褂昧司W(wǎng)絡(luò)圖片,所以要設(shè)置info.plist
如果想集成更多的第三方分享:點(diǎn)擊這里