iOS Developer友盟使用系列

iOS-友盟v6.3.0 微信登录

2017-03-02  本文已影响2875人  青巷水彩

1.将U-Share SDK添加到工程

11

2.添加项目配置 在Other Linker Flags加入-ObjC

22

3.加入依赖系统库

33
加入以下系统库:
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>
//打开调试日志
   [[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.设置系统回调

//   设置系统回调 支持所有iOS系统
- (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的回调

    }
    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的回调
    }
    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源数据
            NSLog(@"--------------originalResponse: %@", resp.originalResponse);
            
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary: resp.originalResponse];
            //OpenID登录的接口
            [self openIDWith:resp.uid with:dic];
        }
    }];
}

8.下面是我们自己的要求,根据授权返回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{
         // 需要的操作 保存数据等
 
        }
 
    } enError:^(NSError *error) {
        NSLog(@"登录失败%@",error);
    }];
    
}

绑定手机号的ViewController

除了username和verifyCode,其他参数是微信授权返回值中获取的

- (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);
    }];    
}
上一篇下一篇

猜你喜欢

热点阅读