今天介紹一個(gè)在程序內(nèi)分享到微信好友或者朋友圈的例子扔涧。
程序內(nèi)部分享方式一:
點(diǎn)擊一個(gè)“分享”按鈕滞乙,彈出一個(gè)微信分享界面蚓聘。點(diǎn)擊朋友圈進(jìn)入“最近聊天好友”選擇界面耗美。
Screen Shot 2017-02-27 at 23.22.28.png
點(diǎn)擊分享京髓,彈出下圖
Screen Shot 2017-02-27 at 23.22.31.png
點(diǎn)擊發(fā)送給朋友,彈出下圖
Screen Shot 2017-02-23 at 10.36.20.png
上述功能是如何實(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)聽用戶點(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)了。其中 {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 (
" {id = com.apple.share.SinaWeibo.post}",
" {id = com.apple.share.TencentWeibo.post}",
" {id = com.taobao.taobao4iphone.ShareExtension}",
" {id = com.tencent.qqmail.shareextension}",
" {id = com.apple.mobilenotes.SharingExtension}",
" {id = com.apple.share.Vimeo.post}",
" {id = com.apple.mobileslideshow.StreamShareService}",
" {id = com.tencent.mqq.ShareExtension}",
" {id = com.tencent.xin.sharetimeline}",
" {id = com.apple.share.Twitter.post}",
" {id = com.apple.share.Flickr.post}",
" {id = com.alipay.iphoneclient.ExtensionSchemeShare}",
" {id = com.apple.Music.MediaSocialShareService}",
" {id = com.apple.share.Facebook.post}",
" {id = com.apple.reminders.RemindersEditorExtension}",
" {id = com.up.2.ShareExtension}",
" {id = com.jianshu.Hugo.Share-Extension}",
" {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" = " {id = com.alipay.iphoneclient.ExtensionSchemeShare}";
"com.apple.Health.HealthShareExtension" = " {id = com.apple.Health.HealthShareExtension}";
"com.apple.Music.MediaSocialShareService" = " {id = com.apple.Music.MediaSocialShareService}";
"com.apple.mobilenotes.SharingExtension" = " {id = com.apple.mobilenotes.SharingExtension}";
"com.apple.mobileslideshow.StreamShareService" = " {id = com.apple.mobileslideshow.StreamShareService}";
"com.apple.reminders.RemindersEditorExtension" = " {id = com.apple.reminders.RemindersEditorExtension}";
"com.apple.share.Facebook.post" = " {id = com.apple.share.Facebook.post}";
"com.apple.share.Flickr.post" = " {id = com.apple.share.Flickr.post}";
"com.apple.share.SinaWeibo.post" = " {id = com.apple.share.SinaWeibo.post}";
"com.apple.share.TencentWeibo.post" = " {id = com.apple.share.TencentWeibo.post}";
"com.apple.share.Twitter.post" = " {id = com.apple.share.Twitter.post}";
"com.apple.share.Vimeo.post" = " {id = com.apple.share.Vimeo.post}";
"com.jianshu.Hugo.Share-Extension" = " {id = com.jianshu.Hugo.Share-Extension}";
"com.taobao.taobao4iphone.ShareExtension" = " {id = com.taobao.taobao4iphone.ShareExtension}";
"com.tencent.mqq.ShareExtension" = " {id = com.tencent.mqq.ShareExtension}";
"com.tencent.qqmail.shareextension" = " {id = com.tencent.qqmail.shareextension}";
"com.tencent.xin.sharetimeline" = " {id = com.tencent.xin.sharetimeline}";
"com.up.2.ShareExtension" = " {id = com.up.2.ShareExtension}";
}
2017-02-27 23:59:34.854222 WeixinActivity[6692:2156632] [core] SLComposeViewController initWithServiceType got extension? {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:? {id = com.apple.share.SinaWeibo.post} requestedServiceType: com.apple.social.sinaweibo
2017-02-27 23:59:34.867423 WeixinActivity[6692:2156632] [core] SLComposeViewController dealloc
當(dāng)然我們要注意型诚,如果手機(jī)沒(méi)有裝微信,你是看不到? ? " {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
提示你“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è)界面.
Screen Shot 2017-02-24 at 14.18.14.png
但是如果你沒(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];
Screen Shot 2017-02-24 at 14.18.24.png
然后在UIActivityViewController中才能看到“微信”分享
Screen Shot 2017-02-27 at 23.50.13.png
這種體驗(yàn)在國(guó)內(nèi)app中很少用。
在這篇博客中主要介紹麦撵,程序內(nèi)實(shí)現(xiàn)分享的兩種方式刽肠。
參看上面程序內(nèi)部分享方式一和程序內(nèi)部分享方式二溃肪。
根據(jù)在實(shí)際使用程序內(nèi)部分享方式一中遇到的一些問(wèn)題免胃,后面推出程序內(nèi)分享 之二