iOS微博授权登录

微博开放平台:传送门
官方提供的Demo:传送门


第一步:申请微博开放平台移动应用

先要在开放平台注册并创建应用并配置Bundle ID,这里就不赘述了
应用创建成功后会获得AppKey,记录并备用

第二步:集成SDK到工程

将从GitHub上下载的libWeiboSDK文件夹添加至工程,SDK包含4个文件:
WeiboSDK.h
WBHttpRequest.h
libWeiboSDK.a
WeiboSDK.bundle

另外还要引入以下系统库:
QuartzCore.framework
ImageIO.framework
SystemConfiguration.framework
Security.framework
CoreTelephony.framework
CoreText.framework
UIKit.framework
Foundation.framework
CoreGraphics.framework
libz.tbd
libsqlite3.tbd
WebKit.framework

第三步:配置URL Scheme

打开Xcode选择project → Info → URL Types → 新增一个,URL Schemes填wb+AppKey(例如AppKey为204543436852,则URL Schemes为wb204543436852)

第四步:配置LSApplicationQueriesSchemes

打开Xcode找到info.plist,右击选择open as → source code
将下面代码添加进去:

LSApplicationQueriesSchemes

   sinaweibohd
   sinaweibo
   weibosdk
   weibosdk2.5

第五步: 向微博客户端程序注册并初始化

使用第一步中获得的AppKey初始化WeiboSDK

[WeiboSDK enableDebugMode:YES]; //设置调试模式
[WeiboSDK registerApp:@"wb204543436852"];

第六步:发起SSO客户端授权认证

WBAuthorizeRequest *request = [WBAuthorizeRequest request];
request.redirectURI = kRedirectURI;
request.scope = @"all";

request.userInfo = @{@"SSO_From": @"ViewControllerClassName",
                     @"Other_Info_1": [NSNumber numberWithInt:123],
                     @"Other_Info_2": @[@"obj1", @"obj2"],
                     @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];

在AppDelegate.m中获取回调

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    return [WeiboSDK handleOpenURL:url delegate:self ];
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{
    
    if ([response isKindOfClass:WBAuthorizeResponse.class]){
        
        WBAuthorizeResponse *authorizeResponse = (WBAuthorizeResponse *)response;
        
        if(authorizeResponse.statusCode == WeiboSDKResponseStatusCodeSuccess){
            
            NSLog(@"用户ID = %@", authorizeResponse.userID);
            NSLog(@"自定义信息字典 = %@", authorizeResponse.userInfo);
        }
    }
}

如果本文对你有所帮助记得点个赞哈:)

你可能感兴趣的:(iOS微博授权登录)