0
点赞
收藏
分享

微信扫一扫

iOS 社会化分享小程序

引言

系统原生Social Framework和 UIActivityViewController的社会化分享只支持text、url、image,所以我们只能使用友盟等第三方分享平台或者微信官方的Open SDK。

I 使用友盟分享小程序

1.1 集成社会化分享

1.2 分享样式设置

  1. 程序新版本的预览图二进制数据,6.5.9及以上版本微信客户端支持 限制大小不超过128KB,自定义图片建议长宽比是 5:4。

thumbData NSData 缩略图的二进制数据 限制内容大小不超过32KB

iOS 社会化分享小程序_加载

  1. 小程序的页面路径,可以只传入 query 部分,来实现传参效果,如:传入​​ "?foo=bar"​

developers.weixin.qq.com/doc/oplatfo…

-(void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject*messageObject =[UMSocialMessageObject messageObject];

UMShareMiniProgramObject*shareObject =[UMShareMiniProgramObject shareObjectWithTitle:@"小程序标题" descr:@"小程序内容描述" thumImage:[UIImage imageNamed:@"icon"]];
shareObject.webpageUrl =@"兼容微信低版本网页地址";
shareObject.userName =@"小程序username,如 gh_3ac2059ac66f";
shareObject.path =@"小程序页面路径,如 pages/page10007/page10007";
messageObject.shareObject = shareObject;
shareObject.hdImageData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
shareObject.miniProgramType =UShareWXMiniProgramTypeRelease;// 可选体验版和开发板

//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data,NSError*error){
if(error){
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if([data isKindOfClass:[UMSocialShareResponseclass]]){
UMSocialShareResponse*resp = data;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);

}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self

II 使用微信官方的Open SDK分享小程序

2.1 版本选择

pod 'WechatOpenSDK' # 指定版本将无法找到Unable to find a specification for `WechatOpenSDK1.8.7.1`
#20200520 -> Installing WechatOpenSDK (1.8.7.1)
#官方下载页面的最新版本是1.9.2

友盟最新版本6.10.4,集成的WechatOpenSDK是v1.9.1。

pod 'UMShare/Social/WeChat','6.10.4'

v6.10.3(更新日期:2021年7月13日) 1、微信SDK升级到v1.9.1。 2、QQSDK升级到 v3.5.3。 3、新浪微博SDK升级到了v3.3.0。 4、优化企业微信和抖音分享面板图标问题。

UMShare v6.9.9(更新日期:2020年6月16日) 1、更新微信完整版SDK1.8.7.1。 2、微信完整版的授权支持本地授权(本地设置appSecret)或通过服务器授权(在服务器配置appSecret)。

2.2 集成

向微信注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//向微信注册
[WXApi registerApp:APP_ID
universalLink:UNIVERSAL_LINK];
return

2.3 用法

developers.weixin.qq.com/doc/oplatfo…

+(void)shareMiniProgramByPlatformType:(UMSocialPlatformType)platformType
currentViewController:(id)currentViewController info:(id)info webpageUrl:(id)webpageUrl shareImage:(id)shareImage hdImageData:(id) hdImageData{

NSLog(@"当前微信SDK的版本号: %@",[WXApi getApiVersion]);//1.9.1
WXMiniProgramObject *object = [WXMiniProgramObject object];
object.webpageUrl = webpageUrl;
object.userName = @"gh_c8";
NSString* path = FMSTR(@"%@%@",@"pages/index/index?",info);
NSLog(@"promotebusinesspath:%@",path);

object.path = path;

object.hdImageData = hdImageData;

object.withShareTicket = YES;


object.miniProgramType = WXMiniProgramTypeRelease;

WXMediaMessage *message = [WXMediaMessage message];
message.title = @"开通收款服务";

message.description = @"小程序描述";
message.thumbData = nil; //兼容旧版本节点的图片,小于32KB,新版本优先
//使用WXMiniProgramObject的hdImageData属性
message.mediaObject = object;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession; //目前只支持会话
[WXApi sendReq:req completion:^(BOOL

III 常见问题

3.1 Universal Links系统回调失败

问题:分享失败,直接跳回原app。

解决方案:​​Domains里必须以applinks:为前缀,并且不需要输入https://字符​​ 。否则无法回调APP,也就是无法完成分享,

3.2 小程序分享后为什么图片很模糊?

推荐width:500px以上的图片吧。然后保持5:4的比例

5:4,建议500x400

  1. thumbData NSData 缩略图的二进制数据 限制内容大小不超过32KB
  2. hdImageData NSData 小程序新版本的预览图二进制数据,6.5.9及以上版本微信客户端支持 限制大小不超过128KB,自定义图片建议长宽比是 5:4。

3.3 小程序封面不显示

原因:​​imageWithContentsOfFile​​加载不到Assets目录下的图片,所以hdImageData为空。

解决方式: 将图片存储在自定义图片路径,不要放在Assets目录。

imageWithName既可以加载Assets目录下的资源也可以加载项目自定义的图片文件路径,区别是 :

  1. 加载Assets目录下的图片对格式有要求,必须是png格式。
  2. 加载自定义图片路径时不能简写图片名,需要带上图片的扩展名。

imageWithContentsOfFile 只能加载到自定义的图片管理路径中的图片资源,加载不到Assets目录下的图片。

"shareicon" ofType:@"png"]];

3.4 分享的小程序在android机器无法显示封面的问题

问题代码

//    NSData* hdImageData =  [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"shareicon250" ofType:@"png"]];

原因: 图片比较大

解决方式:压缩图片

NSData *hdImageData = UIImageJPEGRepresentation([UIImage imageNamed:@"shareicon250"],1.0f);

解决图片压缩之后模糊的问题

3.5 This app is not allowed to query for scheme weixin

-canOpenURL: failed for URL: "weixin://" - error: "This app is not allowed to query for scheme weixin"

  1. CFBundleURLType:当前APP的scheme记录, 通过schema可在其它App中打开当前App。
  2. LSApplicationQueriesSchemes是从iOS9时被引入的。

用意:当前APP允许访问的APP有哪些,即白名单,需要通信双方均设置为对方的scheme,否则当调用对方App时,系统会告诉你​​This app is not allowed to query for scheme。​

解决方法:配置SSO白名单

<key>LSApplicationQueriesSchemes</key>
<array>
<!--微信 URL Scheme白名单-->
<string>wechat</string>
<string>weixin</string>
<string>weixinULAPI</string>
</array>

举报

相关推荐

0 条评论