https://github.com/sinaweibosdk/weibo_ios_sdk 官方SDK下载地址
一、下载微博SDK
微博SDK的官方github : https://github.com/sinaweibosdk/weibo_ios_sdk
下载所需要的SDK
二、集成SDK
下载后的SDK文件结构如下:
将这个文件夹拖拽到你所要添加的工程中。
三、引入依赖库及相关设置
如图点击添加依赖库
所需依赖库:QuartzCore.framework,
ImageIO.framework,
SystemConfiguration.framework,
Security.framework,
CoreTelephony.framework,
CoreText.framework,
UIKit.framework,
Foundation.framework,
CoreGraphics.framework,
libz.dylib,
ibsqlite3.dylib
添加编译选项:
引入静态库后,需添加-ObjC编译选项,避免静态库中类加载不全引起崩溃。
四、创建应用
参考SDK包文件中文档进行应用的创建,这里要强调一下,申请应用时的AppleID和BundleID一定要与工程对应,否则运行会出错,无法进行授权。
五、设置工程回调URL Scheme
如图,设置URLSchemes为你申请的AppKey,
例如:wb204543436852
六、编写代码
1、在Appdelegate中导入头文件,声明遵循微博代理
[objc]
- #import <UIKit/UIKit.h>
- #import "WeiboSDK.h"
- #define kAppKey @"这里是你的AppKey"
- #define kRedirectURI @"这里是你的回调网址"
- AppDelegate : UIResponder <UIApplicationDelegate, WeiboSDKDelegate>
- (strong, nonatomic) UIWindow *window;
2、在Appdelegate中注册微博SDK
[objc]
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- [WeiboSDK enableDebugMode:YES];
- [WeiboSDK registerApp:@"这里填写你申请的Appkey"];
- return YES;
- }
[objc]
- - (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];
- }
4、在你所要添加登录方法的controller中添加以下登录方法,该方法会跳转微博授权
[objc]
- - (void)weiboLoginButtonPressed
- {
- NSLog(@"%s",__func__);
- 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];
- }
这里说明一下,下面的方法实现在Appdelegate中,当在你所需登录的controller调用的登录方法后会跳转微博进行授权,授权信息会以回调的方式调用Appdelegate中的下面方法,从该方法中我们可以得出授权所得到的userId(用户唯一标识),和accessToken。拿到这两个参数后可以调用微博接口进行一系列的获取个人信息、转发、评论、发表微博等操作,这里我们只需要获取用户的个人信息就可以了。
[objc]
- //微博回调
- - (void)didReceiveWeiboResponse:(WBBaseResponse *)response
- {
- if ([response isKindOfClass:WBAuthorizeResponse.class])
- {
- NSString *userId = [(WBAuthorizeResponse *)response userID];
- NSString *accessToken = [(WBAuthorizeResponse *)response accessToken];
- NSLog(@"userId %@",userId);
- NSLog(@"accessToken %@",accessToken);
- NSDictionary *notification = @{
- @"userId" : userId,
- @"accessToken" : accessToken
- };
- [[NSNotificationCenter defaultCenter] postNotificationName:@"weiboDidLoginNotification"
- object:self userInfo:notification];
- }
- }
6、在实现登录的controller中的相应的通知方法中实现以下功能
用刚刚在微博的回调方法中得到的userId和accessToken来请求微博的接口以获取用户的个人信息
[objc]
- https://api.weibo.com/2/users/show.json
[objc]
- #pragma mark - Weibo Methods
- - (void)weiboDidLoginNotification:(NSNotification *)notification
- {
- NSLog(@"%s",__func__);
- NSDictionary *userInfo = [notification userInfo];
- NSString *accessToken = [userInfo objectForKey:@"accessToken"];
- NSString *uid = [userInfo objectForKey:@"userId"];
- NSLog(@"userInfo %@",userInfo);
- [self getWeiboUserInfoWithAccessToken:accessToken uid:uid];
- }
- - (void)getWeiboUserInfoWithAccessToken:(NSString *)accessToken uid:(NSString *)uid
- {
- NSString *url =[NSString stringWithFormat:
- @"https://api.weibo.com/2/users/show.json?access_token=%@&uid=%@",accessToken,uid];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSURL *zoneUrl = [NSURL URLWithString:url];
- NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl
- encoding:NSUTF8StringEncoding error:nil];
- NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
- dispatch_async(dispatch_get_main_queue(), ^{
- if (data)
- {
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data
- options:NSJSONReadingMutableContainers error:nil];
- NSLog(@"%@",dic);
- NSString *openId = [dic objectForKey:@"id"];
- NSString *memNickName = [dic objectForKey:@"name"];
- NSString *memSex = [[dic objectForKey:@"gender"] isEqualToString:@"m"] ? @"1" : @"0";
- [self loginWithOpenId:openId memNickName:memNickName memSex:memSex];
- }
- });
- });
- }
info.plist 添加:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
</array>
问题:
'-[UIView wbsdkCenterX]: unrecognized selector sent to instance 0x7fbc1a4344b0'
解决:
解决的办法:
Build Settings -> Linking -> Other Linker Flags 添加一个-all_load