不知道從什么時候開始田藐,app要集成微信登錄必須要配置universalLink了。否則調不起來微信陡舅。之前只需要[WXApi registerApp:SDK_WX];就可以了酌住,現在必須要[WXApi registerApp:SDK_WX universalLink:UNIVERSAL_LINK];才行。
配置UNIVERSAL_LINK分為幾下幾步刃麸。
一醒叁、登錄開發(fā)者網站進入證書配置頁面,進入?Identifiers里面把對應的Bundle ID下的Associated Domains勾選上。
在把xcode對應的applinks添加上把沼。
然后在配置下URL Types
“LSApplicationQueriesSchemes“欄添加
新建一個不帶后綴名的文件啊易,(必須純文本,命名為apple-app-site-association饮睬,去除后綴名)租谈。最好讓后臺人員在其本地創(chuàng)建,因為我創(chuàng)建完發(fā)給后臺時捆愁,自動生成了后綴名割去,改完也不行,所以讓他自己建昼丑。內容是這樣的呻逆,XXX表示蘋果賬號的團隊ID,OOO表示項目的BundleID矾克。
放在后臺配置的域名服務下就行了页慷。然后讓他給你一個地址,例如這樣:https://www.baidu.com/apple-app-site-association胁附。
如果后臺提供的url地址是https://www.baidu.com/apple-app-site-association酒繁。那么,
Associated Domains中填寫applinks:www.baidu.com控妻,
代碼注冊方法及微信開放平臺中都填https://www.baidu.com/州袒,
配置就差不多了。
如果在appdelegate里面回調不走弓候,看看加沒加這個方法郎哭。
- (BOOL)application:(UIApplication*)applicationcontinueUserActivity:(NSUserActivity*)userActivity
? ? restorationHandler:(void(^)(NSArray> *__nullablerestorableObjects))restorationHandler {
? ? return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}。
接下來就是獲取信息了菇存。
//微信回調代理
- (void)onResp:(BaseResp*)resp{
? ? // =============== 獲得的微信登錄授權回調 ============
? ? if([respisMemberOfClass:[SendAuthRespclass]])? {
? ? ? ? NSLog(@"******************獲得的微信登錄授權******************");
? ? ? ? SendAuthResp*aresp = (SendAuthResp*)resp;
? ? ? ? if(aresp.errCode!=0) {
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? [PKProgressHUD pkShowErrorWithStatueTitle:@"微信授權失敗"];
? ? ? ? ? ? });
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //授權成功獲取 OpenId
? ? ? ? NSString*code = aresp.code;
? ? ? ? [selfgetWeiXinOpenId:code];
? ? }
? ? // =============== 獲得的微信支付回調 ============
? ? if([respisKindOfClass:[PayRespclass]]){
? ? ? ? //支付返回結果夸研,實際支付結果需要去微信服務器端查詢
? ? }
}
//通過code獲取access_token,openid依鸥,unionid
- (void)getWeiXinOpenId:(NSString *)code{
? ? /*
?? ? appid? ? 是? ? 應用唯一標識亥至,在微信開放平臺提交應用審核通過后獲得
?? ? secret? ? 是? ? 應用密鑰AppSecret,在微信開放平臺提交應用審核通過后獲得
?? ? code? ? 是? ? 填寫第一步獲取的code參數
?? ? 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:@"微信授權失敗"];return ;
? ? ? ? }
? ? ? ? // 授權成功贱迟,獲取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:@"微信授權失敗"];return ;
? ? ? ? ? ? }
? ? ? ? ? ? // 獲取用戶信息字典
? ? ? ? ? ? NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
? ? ? ? ? ? //用戶信息中沒有access_token 我將其添加在字典中
? ? ? ? ? ? [dicsetValue:access_tokenforKey:@"token"];
? ? ? ? ? ? NSLog(@"用戶信息字典:===%@",dic);
? ? ? ? ? //微信返回信息后,會跳到登錄頁面,添加通知進行其他邏輯操作
? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:GETWXPARAMS object:dic];
? ? ? ? });
? ? });
}