微信分享流程
1 在蘋(píng)果開(kāi)發(fā)者中心配置
2 微信開(kāi)放中心設(shè)置
3 Xcode 設(shè)置
4 文件設(shè)置apple-app-site-association
5 代碼開(kāi)發(fā)
為了方便敘述挑随,設(shè)置幾個(gè)變量
通用鏈接: https://abc.def.com/
路徑Path :apps/
蘋(píng)果賬號(hào)的 TeamID :teamId
BundleId: com.hig.xyz
一 在蘋(píng)果開(kāi)發(fā)者中心配置
1 在蘋(píng)果開(kāi)發(fā)者中心申請(qǐng)bundleId時(shí)需要勾選 Associated domains汤徽,對(duì)于已經(jīng)申請(qǐng)過(guò)的證書(shū)需要重新申請(qǐng)
2 申請(qǐng)證書(shū)成功后,要記錄BundleID 和 TeamID粱年,在設(shè)置apple-app-site-association文件需要用到
二 微信設(shè)置
微信開(kāi)放平臺(tái) 創(chuàng)建移動(dòng)應(yīng)用填寫(xiě)信息填寫(xiě)
Bundle ID 一般和測(cè)試版本的Bundle ID寫(xiě)的一樣
Universal Links 應(yīng)該是 域名 + path
https://abc.def.com/apps/
/*其中 https://abc.def.com/ 是后臺(tái)給的域名, apps/是配置的path*/
應(yīng)用審核過(guò)之后會(huì)有WechatID 這個(gè)要用在開(kāi)發(fā)中
三 Xcode 設(shè)置
1 在安裝證書(shū)后隅俘,按照以下步驟可以設(shè)置 Associated domains
applinks和webcredentials 設(shè)置的值是一樣的
applinks:abc.def.com //這里設(shè)置的是通用鏈接取掉前面的 https://
2 設(shè)置Queried URL Schemes
3 設(shè)置URL Types
四 文件設(shè)置apple-app-site-association
{
"applinks":{
"apps":[],
"details":[
{
"appID":"teamId.com.hig.xyz",///這里是teamId+.+bundleID
"paths":["/apps/*"] ///因?yàn)槲⑿旁谠O(shè)置通用鏈接時(shí)最好加上Path,我們的設(shè)置了apps/*,如果不想設(shè)置 可以直接寫(xiě) ["*"]
}
]
}
}
- 建好文件讓后臺(tái)把文件上傳到域名的根目錄下或者是根目錄下的.well-known 下,我們剛開(kāi)始放在根目錄下分享有問(wèn)題贷屎,在之后的修改中放到.well-known
- APP 刪除谋币,重新運(yùn)行
- 在safair 中輸入https://abc.def.com/apps/ 在頁(yè)面上可以出現(xiàn)APP的跳轉(zhuǎn)
五 Xcode開(kāi)發(fā)
1 在 pod 文件引入
pod 'WechatOpenSDK-XCFramework'
2 在AppDelegate和SceneDelegate 加入以下代碼
// 在AppDelegate的didFinishLaunchingWithOptions 方法注冊(cè) 微信
//WeChatAppID 是微信開(kāi)放平臺(tái)給的
//WXUniversalLinks 和 微信開(kāi)放平臺(tái)移動(dòng)應(yīng)用Universal Links一致
//在register之前打開(kāi)log, 后續(xù)可以根據(jù)log排查問(wèn)題
[WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
NSLog(@"WeChatSDK: %@", log);
}];
[WXApi registerApp:WeChatAppID universalLink:WXUniversalLinks];
//調(diào)用自檢函數(shù)
[WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
//WXULCheckStepFinal
NSLog(@"WXApi===自檢函數(shù):編號(hào): %@ ,執(zhí)行結(jié)果: %u ,當(dāng)前錯(cuò)誤信息: %@ ,修正建議: %@", @(step), result.success, result.errorInfo, result.suggestion);
}];
//還要重寫(xiě)一下方法
- (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的重寫(xiě)這幾個(gè)方法 并實(shí)現(xiàn)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 微信分享/登錄/支付方法回調(diào)
- (void)onResp:(BaseResp*)resp{
NSLog(@"resp22 ===%d, %@,%d",resp.type,resp.errStr,resp.errCode);
// 1.分享后回調(diào)類(lèi)
if([resp isKindOfClass:[SendMessageToWXResp class]]){
if (resp.errCode == 0) {
WQKAlertWithMessage(@"分享成功", 1.5)
}else{
WQKAlertWithMessage(@"分享失敗", 1.5)
}
}
// 2.微信登錄向微信請(qǐng)求授權(quán)回調(diào)類(lèi)
if ([resp isKindOfClass:[SendAuthResp class]]){
if (resp.errCode == 0) {
//成功
}else{
WQKAlertWithMessage(@"授權(quán)失敗", 1.5)
}
}
// 3.支付后回調(diào)類(lèi)
if ([resp isKindOfClass:[PayResp class]]){
}
}
APP啟動(dòng)時(shí)checkUniversalLinkReady函數(shù)可以走完7步,說(shuō)明分享接入成功
3 在需要分享的位置 調(diào)微信的分享方法 我們是網(wǎng)頁(yè)分享所以只寫(xiě)了這個(gè)方法
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) {
}];
微信分享遇到的問(wèn)題:
1 使用自檢函數(shù)在分享的時(shí)候一直無(wú)法打開(kāi)微信分享的頁(yè)面
控制臺(tái)日志
-> 重新檢查
//APP啟動(dòng)時(shí)只能運(yùn)行5步斋射,啟動(dòng)流程app ->微信->app 沒(méi)有后續(xù)
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 ,執(zhí)行結(jié)果: 1 ,當(dāng)前錯(cuò)誤信息: check passed ,修正建議:
WXApi===1 ,執(zhí)行結(jié)果: 1 ,當(dāng)前錯(cuò)誤信息: check passed ,修正建議:
WXApi===2 ,執(zhí)行結(jié)果: 1 ,當(dāng)前錯(cuò)誤信息: check passed ,修正建議:
WXApi===3 ,執(zhí)行結(jié)果: 1 ,當(dāng)前錯(cuò)誤信息: check passed ,修正建議:
WXApi===4 ,執(zhí)行結(jié)果: 1 ,當(dāng)前錯(cuò)誤信息: check passed ,修正建議:
->參考微信開(kāi)發(fā)文檔 一步一步按照開(kāi)發(fā)文檔 對(duì)照修改
//在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回調(diào)
//-(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 微信分享時(shí)遇到的問(wèn)題
-> 檢查后的修改
//req.scene 之前寫(xiě)死了寫(xiě)的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 微信分享跳轉(zhuǎn)到微信育勺,取消分享跳轉(zhuǎn)到APP還是分享成功,取消分享和分享成功的返回信息是一樣的(未解決)
參考文檔:(微信開(kāi)放平臺(tái))
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/guideline/create.html