微信分享流程
1 在苹果开发者中心配置
2 微信开放中心设置
3 Xcode 设置
4 文件设置apple-app-site-association
5 代码开发
为了方便叙述,设置几个变量
通用链接: https://abc.def.com/
路径Path :apps/
苹果账号的 TeamID :teamId
BundleId: com.hig.xyz
一 在苹果开发者中心配置
1 在苹果开发者中心申请bundleId时需要勾选 Associated domains,对于已经申请过的证书需要重新申请
2 申请证书成功后,要记录BundleID 和 TeamID,在设置apple-app-site-association文件需要用到
二 微信设置
微信开放平台 创建移动应用填写信息填写
Bundle ID 一般和测试版本的Bundle ID写的一样
Universal Links 应该是 域名 + path
https://abc.def.com/apps/
/*其中 https://abc.def.com/ 是后台给的域名, apps/是配置的path*/
应用审核过之后会有WechatID 这个要用在开发中
三 Xcode 设置
1 在安装证书后,按照以下步骤可以设置 Associated domains
applinks和webcredentials 设置的值是一样的
applinks:abc.def.com //这里设置的是通用链接取掉前面的 https://
2 设置Queried URL Schemes
3 设置URL Types
四 文件设置apple-app-site-association
{
"applinks":{
"apps":[],
"details":[
{
"appID":"teamId.com.hig.xyz",///这里是teamId+.+bundleID
"paths":["/apps/*"] ///因为微信在设置通用链接时最好加上Path,我们的设置了apps/*,如果不想设置 可以直接写 ["*"]
}
]
}
}
- 建好文件让后台把文件上传到域名的根目录下或者是根目录下的.well-known 下,我们刚开始放在根目录下分享有问题,在之后的修改中放到.well-known
- APP 删除,重新运行
- 在safair 中输入https://abc.def.com/apps/ 在页面上可以出现APP的跳转
五 Xcode开发
1 在 pod 文件引入
pod 'WechatOpenSDK-XCFramework'
2 在AppDelegate和SceneDelegate 加入以下代码
// 在AppDelegate的didFinishLaunchingWithOptions 方法注册 微信
//WeChatAppID 是微信开放平台给的
//WXUniversalLinks 和 微信开放平台移动应用Universal Links一致
//在register之前打开log, 后续可以根据log排查问题
[WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
NSLog(@"WeChatSDK: %@", log);
}];
[WXApi registerApp:WeChatAppID universalLink:WXUniversalLinks];
//调用自检函数
[WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
//WXULCheckStepFinal
NSLog(@"WXApi===自检函数:编号: %@ ,执行结果: %u ,当前错误信息: %@ ,修正建议: %@", @(step), result.success, result.errorInfo, result.suggestion);
}];
//还要重写一下方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(@"handleOpenURL");
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(@"sourceApplication");
return [WXApi handleOpenURL:url delegate:self];
}
//在SceneDelegate的重写这几个方法 并实现WXApiDelegate返回的方法
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
NSLog(@"application continueUserActivity");
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity API_AVAILABLE(ios(13.0)){
NSLog(@"scene continueUserActivity");
[WXApi handleOpenUniversalLink:userActivity delegate:self];
}
#pragma mark - WXDelegate 微信分享/登录/支付方法回调
- (void)onResp:(BaseResp*)resp{
NSLog(@"resp22 ===%d, %@,%d",resp.type,resp.errStr,resp.errCode);
// 1.分享后回调类
if([resp isKindOfClass:[SendMessageToWXResp class]]){
if (resp.errCode == 0) {
WQKAlertWithMessage(@"分享成功", 1.5)
}else{
WQKAlertWithMessage(@"分享失败", 1.5)
}
}
// 2.微信登录向微信请求授权回调类
if ([resp isKindOfClass:[SendAuthResp class]]){
if (resp.errCode == 0) {
//成功
}else{
WQKAlertWithMessage(@"授权失败", 1.5)
}
}
// 3.支付后回调类
if ([resp isKindOfClass:[PayResp class]]){
}
}
APP启动时checkUniversalLinkReady函数可以走完7步,说明分享接入成功
3 在需要分享的位置 调微信的分享方法 我们是网页分享所以只写了这个方法
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareTitle;
message.description = @"xxxx";
[message setThumbImage:[UIImage imageNamed:@"shareIcon"]];
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = shareUrl;
message.mediaObject = webpageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = platformType;
[WXApi sendReq:req completion:^(BOOL success) {
}];
微信分享遇到的问题:
1 使用自检函数在分享的时候一直无法打开微信分享的页面
控制台日志
-> 重新检查
//APP启动时只能运行5步,启动流程app ->微信->app 没有后续
wxlog:using sdk:pod--
wxlog:set property list
wxlog:launch app with ul only success
wxlog:Successfully starting WeChat with the universal link.
WXApi=== 0 ,执行结果: 1 ,当前错误信息: check passed ,修正建议:
WXApi===1 ,执行结果: 1 ,当前错误信息: check passed ,修正建议:
WXApi===2 ,执行结果: 1 ,当前错误信息: check passed ,修正建议:
WXApi===3 ,执行结果: 1 ,当前错误信息: check passed ,修正建议:
WXApi===4 ,执行结果: 1 ,当前错误信息: check passed ,修正建议:
->参考微信开发文档 一步一步按照开发文档 对照修改
//在SceneDelegate.m添加以下方法
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
NSLog(@"application continueUserActivity");
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity API_AVAILABLE(ios(13.0)){
NSLog(@"scene continueUserActivity");
[WXApi handleOpenUniversalLink:userActivity delegate:self];
}
//在AppDelegate.m添加以下方法
#pragma mark ---Universal Link回调
//-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
// NSLog(@"continueUserActivity");
// return [WXApi handleOpenUniversalLink:userActivity delegate:self];
//}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSLog(@"handleOpenURL");
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(@"sourceApplication");
return [WXApi handleOpenURL:url delegate:self];
}
2 微信分享时遇到的问题
-> 检查后的修改
//req.scene 之前写死了写的1 分享不了
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareTitle;
message.description = @"XXXXXX";
[message setThumbImage:[UIImage imageNamed:@"shareIcon"]];
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = shareUrl;
message.mediaObject = webpageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = platformType; //好友 WXSceneSession/朋友圈WXSceneTimeline
[WXApi sendReq:req completion:^(BOOL success) {
}];
3 微信分享跳转到微信,取消分享跳转到APP还是分享成功,取消分享和分享成功的返回信息是一样的(未解决)
参考文档:(微信开放平台)
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/guideline/create.html