今天介紹一個(gè)在程序內(nèi)分享到微信好友或者朋友圈的例子剂娄。
程序內(nèi)部分享方式一:
點(diǎn)擊一個(gè)“分享”按鈕娃属,彈出一個(gè)微信分享界面畅形。點(diǎn)擊朋友圈進(jìn)入“最近聊天好友”選擇界面株扛。
點(diǎn)擊分享尤筐,彈出下圖
點(diǎn)擊發(fā)送給朋友,彈出下圖
上述功能是如何實(shí)現(xiàn)的呢洞就?請(qǐng)看下面代碼
代碼1-1
NSString *test = @"com.tencent.xin.sharetimeline";
if (![SLComposeViewController isAvailableForServiceType:test]) {
NSLog(@"或者沒(méi)有配置相關(guān)的帳號(hào)");
return;
}
// 2.創(chuàng)建分享的控制器
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:test];
if (composeVc == nil){
NSLog(@"沒(méi)有安裝微信");
return;
}
// 2.1.添加分享的文字
[composeVc setInitialText:@"balabalabala..."];
// 2.2.添加一個(gè)圖片
[composeVc addImage:[UIImage imageNamed:@"圖片名"]];
// 2.3.添加一個(gè)鏈接
[composeVc addURL:[NSURL URLWithString:@"blog.csdn.net/lincsdnnet"]];
// 3.彈出分享控制器(以Modal形式彈出)
[self presentViewController:composeVc animated:YES completion:nil];
// 4.監(jiān)聽(tīng)用戶點(diǎn)擊了取消還是發(fā)送
/*
SLComposeViewControllerResultCancelled,
SLComposeViewControllerResultDone
*/
composeVc.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"點(diǎn)擊了取消");
} else {
NSLog(@"點(diǎn)擊了發(fā)送");
}
};
上面代碼是不是感覺(jué)很熟悉盆繁,不錯(cuò)上面就是我們常用的 Social.Framework
如果你仔細(xì)看API,系統(tǒng)只提供了如下幾個(gè)內(nèi)部分享類型旬蟋。
SOCIAL_EXTERN NSString *const SLServiceTypeTwitter NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeFacebook NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeSinaWeibo NS_AVAILABLE(10_8, 6_0);
SOCIAL_EXTERN NSString *const SLServiceTypeTencentWeibo NS_AVAILABLE(10_9, 7_0);
SOCIAL_EXTERN NSString *const SLServiceTypeLinkedIn NS_AVAILABLE(10_9, NA);
仔細(xì)看沒(méi)有提供微信的接口坝桶骸?但是上面就是能調(diào)用微信分享(如果你安裝微信并登陸了)倾贰。難道是系統(tǒng)給我們實(shí)現(xiàn)了冕碟,卻沒(méi)有給我們開放API?但是仔細(xì)想想是不是iOS8之后系統(tǒng)推出的Share Extension躁染?如果細(xì)看上面彈出微信分享的UI鸣哀,我們可以確定這就是一個(gè)Share Extension。這不是系統(tǒng)實(shí)現(xiàn)的微信分享吞彤,這是微信App的Share Extension我衬。所以來(lái)說(shuō)這個(gè)不是私有API。現(xiàn)在想想蘋果封裝的Social饰恕,真的太棒了挠羔。SLComposeViewController能調(diào)用所有Share Extension。
下面給大家介紹一個(gè)簡(jiǎn)單獲取手機(jī)中裝的App的所有Share Extension的方法埋嵌。只需調(diào)用下面一句代碼破加。
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
下面看看控制臺(tái)輸出,手機(jī)內(nèi)所有可用的Share Extension的bundle Id 都出來(lái)了雹嗦。其中<NSExtension: 0x170167800> {id = com.up.2.ShareExtension}是我自己寫的Share Extension范舀。其他所有字符串(Bundle id)都可以當(dāng)做[SLComposeViewController composeViewControllerForServiceType:]的參數(shù)。
2017-02-27 23:59:34.761492 WeixinActivity[6692:2156632] [core] SLComposeViewController initWithServiceType: com.apple.social.sinaweibo
2017-02-27 23:59:34.849563 WeixinActivity[6692:2156679] [core] SLComposeViewController _shareExtensionWithIdentifier: continuous discovery block got extensions (
"<NSExtension: 0x170167b00> {id = com.apple.share.SinaWeibo.post}",
"<NSExtension: 0x170166240> {id = com.apple.share.TencentWeibo.post}",
"<NSExtension: 0x1701663c0> {id = com.taobao.taobao4iphone.ShareExtension}",
"<NSExtension: 0x170166540> {id = com.tencent.qqmail.shareextension}",
"<NSExtension: 0x1701666c0> {id = com.apple.mobilenotes.SharingExtension}",
"<NSExtension: 0x170166b40> {id = com.apple.share.Vimeo.post}",
"<NSExtension: 0x1701669c0> {id = com.apple.mobileslideshow.StreamShareService}",
"<NSExtension: 0x170166840> {id = com.tencent.mqq.ShareExtension}",
"<NSExtension: 0x170166cc0> {id = com.tencent.xin.sharetimeline}",
"<NSExtension: 0x170166e40> {id = com.apple.share.Twitter.post}",
"<NSExtension: 0x170166fc0> {id = com.apple.share.Flickr.post}",
"<NSExtension: 0x170167140> {id = com.alipay.iphoneclient.ExtensionSchemeShare}",
"<NSExtension: 0x170167380> {id = com.apple.Music.MediaSocialShareService}",
"<NSExtension: 0x170167500> {id = com.apple.share.Facebook.post}",
"<NSExtension: 0x170167680> {id = com.apple.reminders.RemindersEditorExtension}",
"<NSExtension: 0x170167800> {id = com.up.2.ShareExtension}",
"<NSExtension: 0x170167980> {id = com.jianshu.Hugo.Share-Extension}",
"<NSExtension: 0x1701660c0> {id = com.apple.Health.HealthShareExtension}"
) error (null)
2017-02-27 23:59:34.853193 WeixinActivity[6692:2156679] [core] SLComposeViewController _shareExtensionWithIdentifier: continuous discovery block did update _identifierToShareExtensionMap {
"com.alipay.iphoneclient.ExtensionSchemeShare" = "<NSExtension: 0x170167140> {id = com.alipay.iphoneclient.ExtensionSchemeShare}";
"com.apple.Health.HealthShareExtension" = "<NSExtension: 0x1701660c0> {id = com.apple.Health.HealthShareExtension}";
"com.apple.Music.MediaSocialShareService" = "<NSExtension: 0x170167380> {id = com.apple.Music.MediaSocialShareService}";
"com.apple.mobilenotes.SharingExtension" = "<NSExtension: 0x1701666c0> {id = com.apple.mobilenotes.SharingExtension}";
"com.apple.mobileslideshow.StreamShareService" = "<NSExtension: 0x1701669c0> {id = com.apple.mobileslideshow.StreamShareService}";
"com.apple.reminders.RemindersEditorExtension" = "<NSExtension: 0x170167680> {id = com.apple.reminders.RemindersEditorExtension}";
"com.apple.share.Facebook.post" = "<NSExtension: 0x170167500> {id = com.apple.share.Facebook.post}";
"com.apple.share.Flickr.post" = "<NSExtension: 0x170166fc0> {id = com.apple.share.Flickr.post}";
"com.apple.share.SinaWeibo.post" = "<NSExtension: 0x170167b00> {id = com.apple.share.SinaWeibo.post}";
"com.apple.share.TencentWeibo.post" = "<NSExtension: 0x170166240> {id = com.apple.share.TencentWeibo.post}";
"com.apple.share.Twitter.post" = "<NSExtension: 0x170166e40> {id = com.apple.share.Twitter.post}";
"com.apple.share.Vimeo.post" = "<NSExtension: 0x170166b40> {id = com.apple.share.Vimeo.post}";
"com.jianshu.Hugo.Share-Extension" = "<NSExtension: 0x170167980> {id = com.jianshu.Hugo.Share-Extension}";
"com.taobao.taobao4iphone.ShareExtension" = "<NSExtension: 0x1701663c0> {id = com.taobao.taobao4iphone.ShareExtension}";
"com.tencent.mqq.ShareExtension" = "<NSExtension: 0x170166840> {id = com.tencent.mqq.ShareExtension}";
"com.tencent.qqmail.shareextension" = "<NSExtension: 0x170166540> {id = com.tencent.qqmail.shareextension}";
"com.tencent.xin.sharetimeline" = "<NSExtension: 0x170166cc0> {id = com.tencent.xin.sharetimeline}";
"com.up.2.ShareExtension" = "<NSExtension: 0x170167800> {id = com.up.2.ShareExtension}";
}
2017-02-27 23:59:34.854222 WeixinActivity[6692:2156632] [core] SLComposeViewController initWithServiceType got extension <NSExtension: 0x170167b00> {id = com.apple.share.SinaWeibo.post} for identifier com.apple.share.SinaWeibo.post
2017-02-27 23:59:34.854439 WeixinActivity[6692:2156632] [core] SLComposeViewController initWithExtension: <NSExtension: 0x170167b00> {id = com.apple.share.SinaWeibo.post} requestedServiceType: com.apple.social.sinaweibo
2017-02-27 23:59:34.867423 WeixinActivity[6692:2156632] [core] SLComposeViewController dealloc <SLComposeViewController: 0x10032bd60>
當(dāng)然我們要注意了罪,如果手機(jī)沒(méi)有裝微信锭环,你是看不到 "<NSExtension: 0x170166cc0> {id = com.tencent.xin.sharetimeline}"。
Tips:如果我沒(méi)有裝微信泊藕,但是我強(qiáng)制調(diào)用下面代碼
代碼1-2
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:@"com.tencent.xin.sharetimeline"];
你會(huì)看到下面輸出
SLComposeViewController initWithServiceType failed to get extension for identifier com.tencent.xin.sharetimeline
2017-02-28 00:08:42.124005 WeixinActivity[6704:2158719] [core] SLComposeViewController dealloc <SLComposeViewController: 0x100328060>
提示你“failed to get extension”辅辩。SLComposeViewController 內(nèi)部自己釋放了,返回nil。如果執(zhí)行上面的代碼1-1玫锋,就會(huì)發(fā)生奔潰蛾茉。避免奔潰的一個(gè)辦法就是在調(diào)用代碼1-2后要判斷返回值是不是nil,如果是nil就表示沒(méi)有裝微信撩鹿,否則后面會(huì)奔潰(模態(tài)視圖控制器為nil)谦炬。
Tips:如果手機(jī)裝有微信,但是沒(méi)有登錄节沦。 調(diào)用下面代碼
代碼1-3
if (![SLComposeViewController isAvailableForServiceType:@"com.tencent.xin.sharetimeline"]) {
NSLog(@"或者沒(méi)有配置相關(guān)的帳號(hào)");
return;
}
調(diào)用代碼1-3的時(shí)候吧寺,如果你已經(jīng)安裝了微信但是微信沒(méi)有登錄則返回NO;在未登錄狀態(tài)下散劫,強(qiáng)制調(diào)用微信分享功能,會(huì)彈出下面這個(gè)界面.
但是如果你沒(méi)有裝微信幕帆,這個(gè)方法仍然會(huì)返回YES获搏。如果想判斷有沒(méi)有安裝微信,請(qǐng)調(diào)用代碼1-2失乾,并檢測(cè)返回值是否是nil常熙。
程序內(nèi)部分享方式二:
我們網(wǎng)上經(jīng)常遇到用UIActivityViewController來(lái)實(shí)現(xiàn)微信內(nèi)部分享,這個(gè)是可以實(shí)現(xiàn)碱茁,但是這個(gè)實(shí)現(xiàn)UI太難定制裸卫,而且彈出的界面有很多功能不是我們想要的。而且用UIActivityViewController有一個(gè)問(wèn)題比較棘手纽竣,就是彈出的可用分享默認(rèn)沒(méi)有微信墓贿。我們需要到下面這個(gè)界面把微信選上。
代碼調(diào)用如下:
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:@[@"這里是標(biāo)題", [UIImage imageNamed:@"Oauth"], [NSURL URLWithString:@"http://www.google.com"]] applicationActivities:activity];
activityView.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePrint];
[self presentViewController:activityView animated:YES completion:nil];
然后在UIActivityViewController中才能看到“微信”分享
這種體驗(yàn)在國(guó)內(nèi)app中很少用蜓氨。
在這篇博客中主要介紹聋袋,程序內(nèi)實(shí)現(xiàn)分享的兩種方式。
參看上面程序內(nèi)部分享方式一 和 程序內(nèi)部分享方式二穴吹。
根據(jù)在實(shí)際使用程序內(nèi)部分享方式一中遇到的一些問(wèn)題幽勒,后面推出程序內(nèi)分享 之二