可以選擇從友盟進行配置接入(分享和第三方登錄),現在是直接從微信開放平台配置。
一、微信開放平台
1、開發者資質認證,認證有效期:一年,有效期最后三個月可申請年審即可續期;審核費用:中國大陸地區:300元,非中國大陸地區:120美元。
2、管理中心進行應用配置:
1.應用名稱+簡介+官網地址+應用圖片(28*28像素,108*108像素,僅支持PNG格式,大小都不超過300KB);
2.應用提交審核;
3.配置Bundle ID
二、接入指南 微信官網文檔
1.推薦cocopods下載sdk:pod 'WechatOpenSDK',pod install下載,注意:命令行下執行pod search WechatOpenSDK,如顯示的WechatOpenSDK版本不是最新的,則先執行pod repo update操作更新本地repo的內容
2.在Xcode中,選擇你的工程設置項,選中“TARGETS”一欄,在“info”標簽欄的“URL type“添加“URL scheme”為你所注冊的應用程序AppID;
3.info.plist文件中配置白名單:LSApplicationQueriesSchemes(weixin,wechat)
4.appDelegate中,程序啟動代理方法application:didFinishLaunchingWithOptions中進行注冊
[WXApi registerApp:AppID];
// NOTE: 9.0以后使用新API接口
//有支付是,可以通過url.host進行判斷,oauth
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{ return [WXApi handleOpenURL:url delegate:self]; } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [WXApi handleOpenURL:url delegate:self]; }
//實現代理WXApiDelegate - (void)onResp:(BaseResp *)resp { if([resp isKindOfClass:[SendAuthResp class]]){ SendAuthResp *authResp = (SendAuthResp *)resp; switch (resp.errCode) { case 0://失敗 ERR_OK = 0(用戶同意) {//通知授權成功 [[NSNotificationCenter defaultCenter] postNotificationName:@"WechatLoginNotificationName" object:nil userInfo:@{@"code":authResp.code}]; } break; case -2://ERR_USER_CANCEL = -2(用戶取消) break; case -4://ERR_AUTH_DENIED = -4(用戶拒絕授權) break; default: break; } } }
5.登錄界面監聽微信登錄授權結果
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNotification:) name:@"WechatLoginNotificationName" object:nil];
#pragma mark - 微信登錄回調 - (void)loginNotification:(NSNotification *)noti { NSString *code = noti.userInfo[@"code"]; if (kStringIsEmpty(code)) { return; } NSString *appid = AppID; NSString *secrect = SECRET; NSString *url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",appid,SECRET,code]; //Get請求獲取access_token和openid } #pragma mark - 通過微信token獲取微信個人信息 /* access_token是調用授權關系接口的調用憑證,由於access_token有效期(目前為2個小時)較短,當access_token超時后,可以使用refresh_token進行刷新 */ - (void)getWechatUserInfo:(NSString *)openid access_token:(NSString *)access_token { NSString *url =[NSString stringWithFormat: @"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",access_token,openid]; //Get請求獲取用戶信息 } - (void)dealloc { [[NSNotificationCenter defaultCenter]removeObserver:self]; }
獲取access_token時序圖: