1.將U-Share SDK添加到工程
11
2.添加項目配置 在Other Linker Flags加入-ObjC
22
3.加入依賴系統(tǒng)庫
33
加入以下系統(tǒng)庫:
libsqlite3.tbd
CoreGraphics.framework
微信
SystemConfiguration.framework
CoreTelephony.framework
libsqlite3.tbd
libc++.tbd
libz.tbd
4.第三方平臺配置 配置SSO白名單
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名單-->
<string>wechat</string>
<string>weixin</string>
</array>
5.URL Scheme
11474533850_.pic_hd.jpeg
7.初始化U-Share及第三方平
app delegate.m
#import <UMSocialCore/UMSocialCore.h>
//打開調(diào)試日志
[[UMSocialManager defaultManager] openLog:YES];
//設置友盟appkey
[[UMSocialManager defaultManager] setUmSocialAppkey:@"576b40ee67e58ed423000bc1"];
//設置微信的appKey和appSecret
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx4b2322692fe85433" appSecret:@"427ac81412594ef7b7c6921d7d1dc070" redirectURL:@"http://mobile.umeng.com/social"];
6.設置系統(tǒng)回調(diào)
// 設置系統(tǒng)回調(diào) 支持所有iOS系統(tǒng)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回調(diào)
}
return result ||[WXApi handleOpenURL:url delegate:self];
}
// 這個一定要寫哦 當時做過ping++的支付是有這個方法的,而微信登錄時就忘記了補充 BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];捌年,導致微信授權點擊確認登錄時凡恍,沒有任何反應蔑担,也沒有錯誤日志产镐,好慘??
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回調(diào)
}
return result;
}
7.第三方微信平臺登錄
// 在需要進行獲取登錄信息的UIViewController中加入如下代碼
#import <UMSocialCore/UMSocialCore.h>
- (IBAction)weChatLogin:(UIButton *)sender {
NSLog(@"微信登錄");
[[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
if (error) {
NSLog(@"退出登錄 ********%@",error);
} else {
UMSocialUserInfoResponse *resp = result;
// 授權信息
// NSLog(@"Wechat uid: %@", resp.uid);
// NSLog(@"Wechat openid: %@", resp.openid);
// NSLog(@"Wechat accessToken: %@", resp.accessToken);
// NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
// NSLog(@"Wechat expiration: %@", resp.expiration);
//
// // 用戶信息
// NSLog(@"Wechat name: %@", resp.name);
// NSLog(@"Wechat iconurl: %@", resp.iconurl);
// NSLog(@"Wechat gender: %@", resp.gender);
//
// // 第三方平臺SDK源數(shù)據(jù)
NSLog(@"--------------originalResponse: %@", resp.originalResponse);
NSDictionary *dic = [NSDictionary dictionaryWithDictionary: resp.originalResponse];
//OpenID登錄的接口
[self openIDWith:resp.uid with:dic];
}
}];
}
8.下面是我們自己的要求猜敢,根據(jù)授權返回resp.uid的獲取用戶的手機號是否綁定微信脑漫,(手機號直接獲取驗證碼登錄的)如果返回的isUser的值為false秸仙,就是沒有綁定满钟,要去綁定手機號
#pragma mark - OpenID登錄
- (void)openIDWith:(NSString *)unionid with:(NSDictionary *)dic{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_OpenIdDetail];
NSDictionary *paramDic = @{@"unionId":unionid};
[NetworkManager requestGETWithURLStr:url paramDic:paramDic Api_key:self.apikey finish:^(id responseObject) {
NSLog(@"登錄成功返回%@",responseObject);
NSString *isUser = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"isUser"]];
if ([isUser isEqualToString:@"false"]) {// 沒有綁定 不存在
OpenIdUpdateViewController *openIdVC = [[OpenIdUpdateViewController alloc]init];
openIdVC.dic = dic;
[self.navigationController pushViewController:openIdVC animated:YES];
}else{
// 需要的操作 保存數(shù)據(jù)等
}
} enError:^(NSError *error) {
NSLog(@"登錄失敗%@",error);
}];
}
綁定手機號的ViewController
除了username和verifyCode胜榔,其他參數(shù)是微信授權返回值中獲取的
- (void)openIDUpdateWith:(NSString *)username with:(NSString *)verifyCode with:(NSString *)headImage with:(NSString *)nickname with:(NSString *)gender with:(NSString *)unionId{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_OpendIdUpdate];
NSDictionary *dic = @{@"username":username,@"verifyCode":verifyCode,@"headImage":headImage,@"nickname":nickname,@"gender":gender,@"unionId":unionId};
[NetworkManager requestPOSTWithURLStr:url paramDic:dic Api_key:self.apikey finish:^(id responseObject) {
NSLog(@"成功返回apiKey---%@",responseObject);
// 成功返回apiKey,再次微信登錄 返回的isUser的值為ture,不需綁定
// 需要的操作
} enError:^(NSError *error) {
NSLog(@"失敗%@",error);
}];
}