不知道從什么時(shí)候開始,app要集成微信登錄必須要配置universalLink了。否則調(diào)不起來微信。之前只需要[WXApi registerApp:SDK_WX];就可以了,現(xiàn)在必須要[WXApi registerApp:SDK_WX universalLink:UNIVERSAL_LINK];才行。
配置UNIVERSAL_LINK分為幾下幾步。
一、登錄開發(fā)者網(wǎng)站進(jìn)入證書配置頁面,進(jìn)入?Identifiers里面把對(duì)應(yīng)的Bundle ID下的Associated Domains勾選上。
在把xcode對(duì)應(yīng)的applinks添加上。
然后在配置下URL Types
“LSApplicationQueriesSchemes“欄添加
新建一個(gè)不帶后綴名的文件,(必須純文本,命名為apple-app-site-association,去除后綴名)。最好讓后臺(tái)人員在其本地創(chuàng)建,因?yàn)槲覄?chuàng)建完發(fā)給后臺(tái)時(shí),自動(dòng)生成了后綴名,改完也不行,所以讓他自己建。內(nèi)容是這樣的,XXX表示蘋果賬號(hào)的團(tuán)隊(duì)ID,OOO表示項(xiàng)目的BundleID。
放在后臺(tái)配置的域名服務(wù)下就行了。然后讓他給你一個(gè)地址,例如這樣:https://www.baidu.com/apple-app-site-association。
如果后臺(tái)提供的url地址是https://www.baidu.com/apple-app-site-association。那么,
Associated Domains中填寫applinks:www.baidu.com,
代碼注冊(cè)方法及微信開放平臺(tái)中都填https://www.baidu.com/,
配置就差不多了。
如果在appdelegate里面回調(diào)不走,看看加沒加這個(gè)方法。
- (BOOL)application:(UIApplication*)applicationcontinueUserActivity:(NSUserActivity*)userActivity
? ? restorationHandler:(void(^)(NSArray> *__nullablerestorableObjects))restorationHandler {
? ? return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}。
接下來就是獲取信息了。
//微信回調(diào)代理
- (void)onResp:(BaseResp*)resp{
? ? // =============== 獲得的微信登錄授權(quán)回調(diào) ============
? ? if([respisMemberOfClass:[SendAuthRespclass]])? {
? ? ? ? NSLog(@"******************獲得的微信登錄授權(quán)******************");
? ? ? ? SendAuthResp*aresp = (SendAuthResp*)resp;
? ? ? ? if(aresp.errCode!=0) {
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? [PKProgressHUD pkShowErrorWithStatueTitle:@"微信授權(quán)失敗"];
? ? ? ? ? ? });
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //授權(quán)成功獲取 OpenId
? ? ? ? NSString*code = aresp.code;
? ? ? ? [selfgetWeiXinOpenId:code];
? ? }
? ? // =============== 獲得的微信支付回調(diào) ============
? ? if([respisKindOfClass:[PayRespclass]]){
? ? ? ? //支付返回結(jié)果,實(shí)際支付結(jié)果需要去微信服務(wù)器端查詢
? ? }
}
//通過code獲取access_token,openid,unionid
- (void)getWeiXinOpenId:(NSString *)code{
? ? /*
?? ? appid? ? 是? ? 應(yīng)用唯一標(biāo)識(shí),在微信開放平臺(tái)提交應(yīng)用審核通過后獲得
?? ? secret? ? 是? ? 應(yīng)用密鑰AppSecret,在微信開放平臺(tái)提交應(yīng)用審核通過后獲得
?? ? code? ? 是? ? 填寫第一步獲取的code參數(shù)
?? ? grant_type? ? 是? ? 填authorization_code
?? ? */
? ? NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",SDK_WX,SDK_WX_SECRET,code];
? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
? ? ? ? NSURL*zoneUrl = [NSURLURLWithString:url];
? ? ? ? NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
? ? ? ? NSData *data1 = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
? ? ? ? if(!data1) {
? ? ? ? ? ? [PKProgressHUD pkShowErrorWithStatueTitle:@"微信授權(quán)失敗"];return ;
? ? ? ? }
? ? ? ? // 授權(quán)成功,獲取token、openID字典
? ? ? ? NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
? ? ? ? NSLog(@"token、openID字典===%@",dic);
? ? ? ? NSString*access_token = dic[@"access_token"];
? ? ? ? NSString*openid= dic[@"openid"];
? ? ? ? // 獲取微信用戶信息
? ? ? ? [self getUserInfoWithAccessToken:access_token WithOpenid:openid];
? ? });
}
-(void)getUserInfoWithAccessToken:(NSString *)access_token WithOpenid:(NSString *)openid
{
? ? NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",access_token,openid];
? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
? ? ? ? NSURL*zoneUrl = [NSURLURLWithString:url];
? ? ? ? NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
? ? ? ? NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? // 獲取用戶信息失敗
? ? ? ? ? ? if(!data) {
? ? ? ? ? ? ? ? [PKProgressHUD pkShowErrorWithStatueTitle:@"微信授權(quán)失敗"];return ;
? ? ? ? ? ? }
? ? ? ? ? ? // 獲取用戶信息字典
? ? ? ? ? ? NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
? ? ? ? ? ? //用戶信息中沒有access_token 我將其添加在字典中
? ? ? ? ? ? [dicsetValue:access_tokenforKey:@"token"];
? ? ? ? ? ? NSLog(@"用戶信息字典:===%@",dic);
? ? ? ? ? //微信返回信息后,會(huì)跳到登錄頁面,添加通知進(jìn)行其他邏輯操作
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:GETWXPARAMS object:dic];
? ? ? ? });
? ? });
}