ios开发整理

ios 三方登录(qq、微信)

2016-09-06  本文已影响2568人  fjytqiu

qq三方登录

1.前往腾讯开放平台注册帐号并创建应用提交审核
1.1 创建应用(帐号的注册此处不做说明)
1.1
1.2 选择iOS,并留意APP ID;APP KEY(后面需要用到)
1.2
1.3 填写相应的信息并保存、提交(一般来说这个审核几个小时内就会通过)
1.3
1.4 审核通过后->管理中心->点击已获取QQ登录能力的应用->点击QQ登录->填写调试者qq
1.41
1.42
2. 导入SDK: 点我下载 ,并进行相关配置
2.1 下载后将TencentOpenAPI.framework和TencentOpenApi_iOS_Bundle.bundle拖入你的项目
2.1
2.2 添加依赖库

点击Project navigator 点击TARGETS -> General -> Linked Frameworks and Libraries->点击加号添加

2.2

需要添加的依赖库:

”Security.framework”;
“libiconv.tbd”;
“SystemConfiguration.framework”;
“CoreGraphics.Framework”;
“libsqlite3.tbd”;
“CoreTelephony.framework”;
“libstdc++.tbd”;
“libz.tbd”。
2.3 修改配置属性

点击Project navigator 点击TARGETS -> Build Settings ->Linking->Other Linker Flags->点击加号添加属性值“-fobjc-arc”


2.3
2.4 添加URL Scheme

点击Project navigator 点击TARGETS ->info ->URL type-> 添加URL type
Identifier 填写:tencentopenapi
URL Scheme填写: tencent +APP ID(APP ID: 从上文1.2中科获得)
🍐 :你的APP ID是1234567 则填入tencent1234567

2.4
2.5 ios9以后,需要添加白名单

在info.plist文件中加入 LSApplicationQueriesSchemes


2.5
2.6 针对iOS9默认使用https,现在先还原成http请求方式

第一步:在plist中添加NSAppTransportSecurity项,此项为NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads类型为Boolean,value为YES

2.6
3.代码区
3.1 在Appdelegate.m中

导入并重写两个方法

#import <TencentOpenAPI/TencentOAuth.h>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [TencentOAuth HandleOpenURL:url];
}
3.2 在ThirdLogin.m(用于封装三方登录的类)中
#import "ThirdLogin.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "HttpClient.h"

#define NAME @"name"
#define PICTURE @"picture"
#define OPENID @"openId"

@interface ThirdLogin ()<TencentSessionDelegate>
{
    TencentOAuth *tencentOAuth;
    NSArray *permissions;
}
@property (copy, nonatomic) void (^success)(NSDictionary *result);
@property (copy, nonatomic) void (^failure)(NSError *error);

@end
@implementation ThirdLogin
#pragma mark--------------------------------------------------------
#pragma mark  qq三方登录
// 对外的api: 即点击qq三方登录按钮,调这个方法
- (void)qqLoginWithSuccess:(void (^)(NSDictionary *userInfo))success failure:(void (^)(NSError *error))failure {
    // 成功或失败的回调block    
    self.success = [success copy];
    self.failure = [failure copy];
      
    //  需要放在主线程中执行  
    dispatch_async(dispatch_get_main_queue(), ^{
       tencentOAuth=[[TencentOAuth alloc]initWithAppId:@"1105549185" andDelegate:self];
        
        //  设置需要的权限列表,此处尽量使用什么取什么。
        permissions= [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_USER_INFO,
                      kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, @"add_t", nil];
        [tencentOAuth authorize:permissions];
    });
}

- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions {
    return YES;
}
#pragma mark -- TencentSessionDelegate
//登陆完成调用
- (void)tencentDidLogin
{
    if (tencentOAuth.accessToken && 0 != [tencentOAuth.accessToken length]) {
        
        //  记录登录用户的OpenID、Token以及过期时间
        [tencentOAuth getUserInfo];
    }
    else {
        [self failureWith:@"未授权成功"];
    }
}

//非网络错误导致登录失败:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled){
        [self failureWith:@"用户取消登录"];
    }else{
        [self failureWith:@"登录失败"];
    }
}
// 网络错误导致登录失败:
-(void)tencentDidNotNetWork {
    [self failureWith:@"网络错误"];
}

- (void)tencentDidLogout{
    //   NSLog(@"登出");
}

-(void)getUserInfoResponse:(APIResponse *)response {
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:[response.jsonResponse objectForKey:@"nickname"], [response.jsonResponse objectForKey:@"figureurl_qq_2"], tencentOAuth.openId, NAME, PICTURE, OPENID,nil];
    self.success(result);
}

- (void)failureWith:(NSString *)domin {
    NSError *error = [NSError errorWithDomain:domin code:0 userInfo:nil];
    self.failure(error);
}
补充:
登陆成功的方法里面调用
       [tencentOAuth getUserInfo];
然后系统会调用一个方法(我们需要提前实现)
-(void)getUserInfoResponse:(APIResponse *)response {
}

在getUserInfoResponse中可以得到所需要的用户信息

微信三方登录

1.前往微信开放平台注册帐号并创建应用提交审核

在微信开放平台注册开发者帐号并创建应用,审核通过后,获得相应的AppID和AppSecret,基本流程和qq三方登录类似(这里不做说明)

2. 导入SDK: 点我下载 ,并进行相关配置
2.1 下载后将下面文件导入工程目录中
2.1
2.2 添加依赖库
2.2

需要添加的依赖库:

“SystemConfiguration.framework”;
“CoreTelephony.framework”;
“libsqlite3.0.tbd”;
“libstdc++.tbd”;
“libz.tbd”;
"libWeChatSDK.a"
2.3 添加URL Scheme

点击Project navigator 点击TARGETS ->info ->URL type-> 添加URL type
Identifier 填写:可自定义
URL Scheme填写: APP ID(APP ID: 从上文1.2中科获得)
🍐 :你的APP ID是wx1234567 则填入wx1234567


2.3
2.4 ios9以后,需要添加白名单

在info.plist文件中加入 LSApplicationQueriesSchemes

2.4
2.5 针对iOS9默认使用https,现在先还原成http请求方式

第一步:在plist中添加NSAppTransportSecurity项,此项为NSDictionary
第二步:在NSAppTransportSecurity下添加 NSAllowsArbitraryLoads类型为Boolean,value为YES


2.6
3.代码区
3.1 在Appdelegate.m中
#import "AppDelegate.h"
#import "WXApi.h"
#import "ThirdLogin.h"   //  用于封装三方登录的类

@interface AppDelegate ()<WXApiDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //  @"wx1235467" : APP ID
    [WXApi registerApp:@"wx1235467" withDescription:@"Wechat"];
    return YES;
}

// 这个方法是用于从微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    //  这里我的代理设置的是ThirdLogin对象,有的直接设self(即AppDelegate)
    [WXApi handleOpenURL:url delegate:[ThirdLogin shareThirdLogin]];
    return YES;
}
3.1 在ThirdLogin.m中
#import "ThirdLogin.h"
#import "HttpClient.h"
#import "WXApi.h"

#define NAME @"name"
#define PICTURE @"picture"
#define OPENID @"openId"

@interface ThirdLogin ()<WXApiDelegate>

@property (copy, nonatomic) void (^success)(NSDictionary *result);
@property (copy, nonatomic) void (^failure)(NSError *error);

@end
#pragma mark--------------------------------------------------------
#pragma mark  微信三方登录
//  对外的api: 即点击微信三方登录按钮,调这个方法
- (void)weixinLoginWithsuccess:(void (^)(NSDictionary *userInfo))success failure:(void (^)(NSError *error))failure {
    //  成功或失败的回调block
    self.success = [success copy];
    self.failure = [failure copy];
   
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN];
    NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID];
    // 如果已经请求过微信授权登录,那么考虑用已经得到的access_token
    if (accessToken && openID) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSString *refreshToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_REFRESH_TOKEN];
        NSString *refreshUrlStr = [NSString stringWithFormat:@"%@/oauth2/refresh_token?appid=%@&grant_type=refresh_token&refresh_token=%@", WX_BASE_URL, WXPatient_App_ID, refreshToken];
        [manager GET:refreshUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"请求reAccess的response = %@", responseObject);
            NSDictionary *refreshDict = [NSDictionary dictionaryWithDictionary:responseObject];
            NSString *reAccessToken = [refreshDict objectForKey:WX_ACCESS_TOKEN];
            // 如果reAccessToken为空,说明reAccessToken也过期了,反之则没有过期
            if (reAccessToken) {
                // 更新access_token、refresh_token、open_id
                [[NSUserDefaults standardUserDefaults] setObject:reAccessToken forKey:WX_ACCESS_TOKEN];
                [[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WX_OPEN_ID] forKey:WX_OPEN_ID];
                [[NSUserDefaults standardUserDefaults] setObject:[refreshDict objectForKey:WX_REFRESH_TOKEN] forKey:WX_REFRESH_TOKEN];
                [[NSUserDefaults standardUserDefaults] synchronize];
                // reAccessToken不为空说明未超时,直接执行wechatLoginByRequestForUserInfo方法获取数据
                if ([self respondsToSelector:@selector(wechatGetUserInfo)]) {
                    [self wechatGetUserInfo];
                }
            }else {
                //  refresh_token失效的后需重新授权
                [self wechatLogin];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self failureWith:@"请求失败"];
        }];
    }
    else {
        //  本地保存的accessToken、openID为空,说明是第一次登陆,或者数据遗失
        [self wechatLogin];
    }
}

- (void)wechatLogin {
    if ([WXApi isWXAppInstalled]) {
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.scope = @"snsapi_userinfo";
        req.state = @"App";
        [WXApi sendReq:req];
    }else{
        //把微信登录的按钮隐藏掉。
    }
}

- (void)wechatGetUserInfo {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN];
    NSString *openID = [[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID];
    NSString *userUrlStr = [NSString stringWithFormat:@"%@/userinfo?access_token=%@&openid=%@", WX_BASE_URL, accessToken, openID];
    // 请求用户数据
    [manager GET:userUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:responseObject[@"nickname"], responseObject[@"headimgurl"], openID, NAME, PICTURE, OPENID,nil];
        self.success(result);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self failureWith:@"请求失败"];
    }];
}

/*====================delegate方法=====================*/
-(void) onResp:(BaseResp*)resp{
    SendAuthResp *temp = (SendAuthResp *)resp;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, WXPatient_App_ID, WXPatient_App_Secret, temp.code];
    [manager GET:accessUrlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];
        NSString *accessToken = [accessDict objectForKey:WX_ACCESS_TOKEN];
        NSString *openID = [accessDict objectForKey:WX_OPEN_ID];
        NSString *refreshToken = [accessDict objectForKey:WX_REFRESH_TOKEN];
        // 本地持久化,以便access_token的使用、刷新或者持续
        if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
            [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:WX_ACCESS_TOKEN];
            [[NSUserDefaults standardUserDefaults] setObject:openID forKey:WX_OPEN_ID];
            [[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:WX_REFRESH_TOKEN];
            [[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里,来避免数据的丢失
        }
        [self wechatGetUserInfo];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self failureWith:@"请求失败"];
    }];
}
补充:
     access_token   接口调用凭证 
     refresh_token  用户刷新access_token 
     openid 授权用户唯一标识

access_token有效期为2小时,当再次登陆时通过refresh_token进行刷新,有两种情况:

  1. access_token未超时,access_token不会改变,但超时时间会刷新,相当于续期access_token。
  2. 超时,那么会获取一个新的access_token,新的超时时间;
注:refresh_token拥有有效期:30天

Comments

如有错误,望指正

上一篇下一篇

猜你喜欢

热点阅读