微信登录、新浪登录、qq登录

  • 现在这个功能大部分人都是用UM和share去做的,今天我们以来回味下原味道

新浪登录

  • 新浪API网址大全:http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2,这里我们知道获取完参数后各个信息的请求接口
  • 新浪SDK下载地址: http://open.weibo.com/wiki/SDK
  • 集成的注意点:
  • 1、首先肯定是去新浪开发者平台去创建应用,申请appKey,这一段这里不再啰嗦,大概他们会审一天的样子,所以建议大家提前做这事
  • 2、审核通过,拿到key以后,去下载SDK包,把SDK包中的libWeiboSDK文件夹添加到工程中去。
  • 3、工程中我们需要配置三处地方:第一处添加URLScheme:是wb+你微博应用的appkey(比如wb2045436852)。第二处:添加系统动态包,QuartzCore.framework,ImageIO.framework, SystemConfiguration.framework, Security.framework, CoreTelephony.framework, CoreText.framework, CoreGraphics.framework, libz.tbd, libsqlite3.tbd。 第三处:在系统的infoplist文件中添加白名单,LSApplicationQueriesSchemes中添加名单weibosdk2.5, weibosdk,sinaweibohd, sinaweibo。(如果你的服务器还没有ssl证书,你还需要设置下网络App Transport Security Settings这里的tran..为YES)
  • 4、OK,环境配置完成。接下来是代码了:appdelegate中,要做的事大概是:向新浪开发者注册,判断应用是否是被新浪的回调打开,实现代理,查新浪API,获取用户新浪信息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:@"1292878617"];
    return YES;
}
// 方法1
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    if ([url.host isEqualToString:@"response"]) {
        [WeiboSDK handleOpenURL:url delegate:self];
    }
    return YES;
}
// 方法1有时候不走,可以用以前的方法,也就是方法2
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

在appdelegate中遵守微博代理(当然,实际开发中这个代理肯定给某个工具类),并实现代理方法

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
    // 登录的回调这里
    [self getWeiBoUserInfo:response.userInfo]; // 查微博API,请求用户的微信信息
    }
}
- (void)getWeiBoUserInfo:(NSDictionary *)userDic
{
    NSString *access_token = userDic[@"access_token"];
    NSNumber *uid = userDic[@"uid"];
    NSString *paramter = [NSString stringWithFormat:@"access_token=%@&uid=%@", access_token, uid];
    NSString *baseUrl = @"https://api.weibo.com/2/users/show.json";
    NSString *urlStr = [baseUrl stringByAppendingFormat:@"?%@", paramter];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"1= %@", dic);
        }
    }];
    [dataTask resume];
}
  • 5、然后就完全90%了,剩下的只要写个btn调用下方法就可以了
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
  • 6、gameOver

qq登录

SDK下载地址:http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD#SDKfor.E7.A7.BB.E5.8A.A8.E5.BA.94.E7.94.A8.E6.8E.A5.E5.85.A5

  • 将iOS SDK中的TencentOpenAPI.frameworkTencentOpenApi_IOS_Bundle.bundle文件拷贝到应用开发的目录下
  • 添加SDK依赖的系统库文件。分别是”Security.framework”, “libiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”
  • 需要注意的地方:info中添加URLTypes格式是:比如tencent1105042374。tencent+appid
  • 设置网络请求,兼容http
  • 添加各种白名单
        <string>mqq</string>
        <key>New item - 2</key>
        <string>mqqapi</string>
        <key>New item - 3</key>
        <string>mqqwpa</string>
        <key>New item - 4</key>
        <string>mqqbrowser</string>
        <key>New item - 5</key>
        <string>mttbrowser</string>
        <key>New item - 6</key>
        <string>mqqOpensdkSSoLogin</string>
        <key>New item - 7</key>
        <string>mqqopensdkapiV2</string>
        <key>New item - 8</key>
        <string>mqqopensdkapiV3</string>
        <key>New item - 9</key>
        <string>wtloginmqq2</string>
        <key>New item - 10</key>
        <string>mqzone</string>
        <key>New item - 11</key>
        <string>mqzoneopensdk</string>
        <key>New item - 12</key>
        <string>mqzoneopensdkapi</string>
        <key>New item - 13</key>
        <string>mqzoneopensdkapi19</string>
        <key>New item - 14</key>
        <string>mqzoneopensdkapiV2</string>
        <key>New item - 15</key>
        <string>mqqapiwallet</string>
        <key>New item - 16</key>
        <string>mqqopensdkfriend</string>
        <key>New item - 21</key>
        <string>tencentapi.qzone.reqContent</string>
        <key>New item - 17</key>
        <string>mqqopensdkdataline</string>
        <key>New item - 18</key>
        <string>mqqgamebindinggroup</string>
        <key>New item - 19</key>
        <string>mqqopensdkgrouptribeshare</string>
        <key>New item - 20</key>
        <string>tencentapi.qq.reqContent</string>
  • appdelegate中,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    if ([url.host isEqualToString:@"qzapp"]) {
        // 从qq跳回来的
        [TencentOAuth HandleOpenURL:url];
    }
    return YES;
}
  • VC中跳转事件
import <TencentOpenAPI/TencentOAuth.h>
define kTencentAppId @"1105042374"
@interface ViewController ()<TencentSessionDelegate>
@property (nonatomic, strong) TencentOAuth *tencentOAuth;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib
    
    self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kTencentAppId andDelegate:self];
}
- (IBAction)log:(id)sender {
 
    NSArray* permissions = [NSArray arrayWithObjects:
                            kOPEN_PERMISSION_GET_USER_INFO,
                            kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                            kOPEN_PERMISSION_ADD_SHARE,
                            nil];
    [self.tencentOAuth authorize:permissions inSafari:NO];
}

pragma mark - 代理
- (void)tencentDidLogin
{
    NSLog(@"登陆成功");
    // 获取用户信息
    [self.tencentOAuth getUserInfo];
}
- (void)tencentDidNotLogin:(BOOL)cancelled
{
    NSLog(@"登录失败");
}
- (void)tencentDidNotNetWork
{
    NSLog(@"网络问题");
}
- (void)getUserInfoResponse:(APIResponse *)response
{
    if (response.retCode == URLREQUEST_SUCCEED && kOpenSDKErrorSuccess == response.detailRetCode) {
        NSMutableString *str = [NSMutableString stringWithFormat:@""];
        for (id key in response.jsonResponse) {
            [str appendString: [NSString stringWithFormat:
                                @"%@:%@\n", key, [response.jsonResponse objectForKey:key]]];
        }
        NSLog(@"%@", str);
        
    }else{
        NSString *errMsg = [NSString stringWithFormat:@"errorMsg:%@\n%@",
                            response.errorMsg, [response.jsonResponse objectForKey:@"msg"]];
        NSLog(@"%@", errMsg);
    }
}
  • ok

微信登录

  • 1、大概流程是:先去注册应用,然后交钱,申请开通微信登录功能
  • 2、审核通过,拿到微信应用的AppKey和AppScroet
  • 3、系统环境配置,基本上只要再在上面的基础上加一个Scheme(wxf707ade9f9ff3ebb 这个和新浪挺想象的)就可以了
  • 4、在appdelegate中注册微信App,遵守微信代理,并且实现代理方法
  • 5、AppDelegate中关键代码如下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 注册微信
    [WXApi registerApp:kWeiChatAppId withDescription:@"微信"];    
    return YES;
}
// 接受微信回调方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
   if([url.host isEqualToString:@"oauth"]){
        // 从微信跳回来的
        [WXApi handleOpenURL:url delegate:self];
    }
    return YES;
}
// 微信代理方法
-(void)onResp:(BaseResp*)resp{
    // SendAuthResp是微信登录
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *authResp = (SendAuthResp *)resp;
        // 调用工具类,获取用户的基本数据
        [[GetUserInfo sharedInstance] getUserWXInfoByCode:authResp.code];
    }
}
  • 5、写个button调用微信登录
static NSString *kAuthScope = @"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact";
static NSString *kAuthState = @"xxx";
- (IBAction)login:(id)sender {
    // 调用微信登录
    SendAuthReq *req = [[SendAuthReq alloc] init];
    req.scope = kAuthScope;
    req.state = @"123";
    [WXApi sendReq:req];
}
  • 6、至于获取用户的微信信息,这个和新浪一模一样,都是在代理方法中获取到access_token和一个类似uid一样的,然后去找API接口

[微信API接口][1]
[1]:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容