標簽(空格分隔): iOS
- 微信SDK接入過程
- 接入前原理知識準備:iOS 微信第三方登錄文檔說明
- 官方文檔:iOS接入指南
- 1.工程配置
引入系統(tǒng)庫:
SystemConfiguration.framework, libz.dylib, libsqlite3.0.dylib, libc++.dylib, Security.framework, CoreTelephony.framework, CFNetwork.framework。
Build Setting,在"Other Linker Flags"中加入"-Objc -all_load",在Search Paths中添加 libWeChatSDK.a ,WXApi.h,WXApiObject.h
- 2.代碼
AppDelegate.m
#import"WXApi.h"
引入?yún)f(xié)議:<WXApiDelegate>
didFinishLaunchingWithOption 方法中
向微信注冊自己的App
[WXApi registerApp:@"wx3d40739db8ea9f4f" withDescription:@"第三方登錄"];
還有下面兩個方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:[ViewController new]];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
return [WXApi handleOpenURL:url delegate:[ViewController new]];
}
在觸發(fā)第三方登錄的控制器中;
點擊微信登錄;
-(void)weChatLogin {
// 方法一:只有手機安裝了微信才能使用
// if ([WXApi isWXAppInstalled]) {
// SendAuthReq *req = [[SendAuthReq alloc] init];
// //這里是按照官方文檔的說明來的此處我要獲取的是個人信息內容
// req.scope = @"snsapi_userinfo";
// req.state = @"";
// //向微信終端發(fā)起SendAuthReq消息
// [WXApi sendReq:req];
// } else {
// NSLog(@"安裝微信客戶端");
// }
// 方法二:手機沒有安裝微信也可以使用,推薦使用這個
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = @"";
[WXApi sendAuthReq:req viewController:self delegate:self];
}
代理方法
#pragma mark - WXApiDelegate
-(void)onResp:(BaseResp *)resp{
//判斷是否是微信認證的處理結果
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *temp = (SendAuthResp *)resp;
//如果你點擊了取消,這里的temp.code 就是空值
if (temp.code != NULL) {
//此處判斷下返回來的code值是否為錯誤碼
/*此處接口地址為微信官方提供,我們只需要將返回來的code值傳入再配合appId和appSecret即可獲取到accessToken,openId和refreshToken */
//https://api.weixin.qq.com/sns /oauth2/access_token
NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, WX_App_ID, WX_App_Secret, temp.code];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[manager GET:accessUrlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self p_successedWeiChatLogin:responseObject];
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self p_failureWeiChatLogin:error];
}];
}
}
}
/**
網(wǎng)絡請求成功
@param dic 網(wǎng)絡請求數(shù)據(jù)
*/
- (void)p_successedWeiChatLogin:(NSDictionary *)dic{
NSDictionary *returnObject = [NSDictionary dictionary];
returnObject = dic;
//成功返回
// {
// "access_token" = "fdpTn5awAnJ7g-RAjLjMT7DAFInXhbIjmLZzmrLea8jQtJm2VyEEIB3NKdvnV6gHXPo76ki0z4kiQ1CXA62SnneKZI"; 接口調用憑證
// "expires_in" = 7200;//接口調用憑證超時時間,單位(秒)
// openid = ovMVmwh0TzOnVQX62R5zXg;//授權用戶唯一標識
// "refresh_token" = "4GjXOOIAOBYuxO7wfjimyB1d_H6xLeCeUeng8bKDCzv5-N3yZSueJnz6UTkh9_j6l0tuS4Dlcs6c3ZC1xTmCUe0M0";//用戶刷新access_token
// scope = "snsapi_userinfo";//用戶授權的作用域,使用逗號(,)分隔
// unionid = oTlu3wJzgi6iVVb8txvU;//當且僅當該移動應用已獲得該用戶的userinfo授權時,才會出現(xiàn)該字段
// }
}
```
錯誤
提示:This app is not allowed to query for scheme xxxx
解決:在infor.plist中添加LSApplicationQueriesSchemes(數(shù)組),數(shù)組中元素添加weixin