今天介绍一个在程序内分享到微信好友或者朋友圈的例子。
程序内部分享方式一:
点击一个“分享”按钮,弹出一个微信分享界面。点击朋友圈进入“最近聊天好友”选择界面。
Screen Shot 2017-02-27 at 23.22.28.png点击分享,弹出下图
Screen Shot 2017-02-27 at 23.22.31.png点击发送给朋友,弹出下图
Screen Shot 2017-02-23 at 10.36.20.png上述功能是如何实现的呢?请看下面代码
代码1-1
NSString *test = @"com.tencent.xin.sharetimeline";
if (![SLComposeViewController isAvailableForServiceType:test]) {
NSLog(@"或者没有配置相关的帐号");
return;
}
// 2.创建分享的控制器
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:test];
if (composeVc == nil){
NSLog(@"没有安装微信");
return;
}
// 2.1.添加分享的文字
[composeVc setInitialText:@"balabalabala..."];
// 2.2.添加一个图片
[composeVc addImage:[UIImage imageNamed:@"图片名"]];
// 2.3.添加一个链接
[composeVc addURL:[NSURL URLWithString:@"blog.csdn.net/lincsdnnet"]];
// 3.弹出分享控制器(以Modal形式弹出)
[self presentViewController:composeVc animated:YES completion:nil];
// 4.监听用户点击了取消还是发送
/*
SLComposeViewControllerResultCancelled,
SLComposeViewControllerResultDone
*/
composeVc.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"点击了取消");
} else {
NSLog(@"点击了发送");
}
};
上面代码是不是感觉很熟悉,不错上面就是我们常用的 Social.Framework
如果你仔细看API,系统只提供了如下几个内部分享类型。
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);
仔细看没有提供微信的接口啊?但是上面就是能调用微信分享(如果你安装微信并登陆了)。难道是系统给我们实现了,却没有给我们开放API?但是仔细想想是不是iOS8之后系统推出的Share Extension?如果细看上面弹出微信分享的UI,我们可以确定这就是一个Share Extension。这不是系统实现的微信分享,这是微信App的Share Extension。所以来说这个不是私有API。现在想想苹果封装的Social,真的太棒了。SLComposeViewController能调用所有Share Extension。
下面给大家介绍一个简单获取手机中装的App的所有Share Extension的方法。只需调用下面一句代码。
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
下面看看控制台输出,手机内所有可用的Share Extension的bundle Id 都出来了。其中<NSExtension: 0x170167800> {id = com.up.2.ShareExtension}是我自己写的Share Extension。其他所有字符串(Bundle id)都可以当做[SLComposeViewController composeViewControllerForServiceType:]的参数。
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>
当然我们要注意,如果手机没有装微信,你是看不到 "<NSExtension: 0x170166cc0> {id = com.tencent.xin.sharetimeline}"。
Tips:如果我没有装微信,但是我强制调用下面代码
代码1-2
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:@"com.tencent.xin.sharetimeline"];
你会看到下面输出
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 内部自己释放了,返回nil。如果执行上面的代码1-1,就会发生奔溃。避免奔溃的一个办法就是在调用代码1-2后要判断返回值是不是nil,如果是nil就表示没有装微信,否则后面会奔溃(模态视图控制器为nil)。
Tips:如果手机装有微信,但是没有登录。 调用下面代码
代码1-3
if (![SLComposeViewController isAvailableForServiceType:@"com.tencent.xin.sharetimeline"]) {
NSLog(@"或者没有配置相关的帐号");
return;
}
调用代码1-3的时候,如果你已经安装了微信但是微信没有登录则返回NO;在未登录状态下,强制调用微信分享功能,会弹出下面这个界面.
Screen Shot 2017-02-24 at 14.18.14.png但是如果你没有装微信,这个方法仍然会返回YES。如果想判断有没有安装微信,请调用代码1-2,并检测返回值是否是nil。
程序内部分享方式二:
我们网上经常遇到用UIActivityViewController来实现微信内部分享,这个是可以实现,但是这个实现UI太难定制,而且弹出的界面有很多功能不是我们想要的。而且用UIActivityViewController有一个问题比较棘手,就是弹出的可用分享默认没有微信。我们需要到下面这个界面把微信选上。
代码调用如下:
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:@[@"这里是标题", [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这种体验在国内app中很少用。
在这篇博客中主要介绍,程序内实现分享的两种方式。
参看上面程序内部分享方式一 和 程序内部分享方式二。
根据在实际使用程序内部分享方式一中遇到的一些问题,后面推出程序内分享 之二
网友评论
看你的例子中有设置分享文字,但是分享出来是“无标题”,请问如何解决呢?
想分享一个URL,有图片,链接,文字的那种,使用SLComposeViewController实现,但就是文字设置不上去、