天天看點

iOS 內建微信SDK1.8.6.1

官方文檔:配置應用

微信SDK版本:1.8.6.1

Xcode版本:11.0

1.配置應用的Universal Links

1、建立一個無字尾名的檔案"apple-app-site-association"

内容為:

{

“applinks”: {

“apps”: [],

“details”: [{

“appID”: “8P7343TG54.com.tencent.xin.SDKSample”,

“paths”: ["/sdksample/*"]

}]

}

}

注:appID格式為 <TeamID>.<BundleID>

2、将"apple-app-site-association"檔案放入伺服器根目錄

例:https://<域名>/apple-app-site-association

3、打開Associated Domains開關,将Universal Links域名加到配置上

iOS 內建微信SDK1.8.6.1

項目->TSRGETS->Signing&Capabilities->Capability, 點選Associated Domains添加,在Domains中添加applinks:<域名>.

2、根據文檔添加SDK檔案到項目,配置所需動态庫和靜态庫檔案

3、調用api

1、注冊微信ID

2、APPDelete中設定api

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    return [WXApi handleOpenURL:url delegate:[WXManager sharedManager]];
}
           

3、登入授權

#define wxState @"weixinloginauth"
#define wxScope @"snsapi_userinfo";

// 發起微信登入授權申請
- (void)sendAuthRequest
{
    //構造SendAuthReq結構體
    SendAuthReq* req =[[SendAuthReq alloc]init];
    req.scope = wxScope;
    req.state = wxState;
    //第三方向微信終端發送一個SendAuthReq消息結構
    [WXApi sendAuthReq:req viewController:[UIViewController topViewController] delegate:self completion:nil];
}
           

4、授權回調

// 請求微信回調結果
- (void)onResp:(BaseResp *)resp
{
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *req = (SendAuthResp *)resp;
        if ([req.state isEqualToString:wxState]) {
        	// 将授權碼傳回登入頁面處理
            [self sendCodeDelegate:req.code];
        }
    }
}
           

5、根據code請求token

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

這一步建議放在服務端處理,因為需要使用APPID和微信秘鑰,儲存在服務端安全。

6、根據token和openID擷取使用者資訊

GET https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID

也是服務端處理,直接儲存使用者昵稱頭像等資訊作為使用者初始資訊儲存在服務端。