IOS 騰訊微博開放平臺 SDK 初體驗
這是今天早上使用騰訊開放平臺被折磨的半死不活的時候寫的微博。這是我遇到的問題。而且這種登錄方式似乎必須要求有 QQ 空間客戶端?放棄這個 SDK,轉向了騰訊微博開放平臺。
1.創建新的接入應用獲取 Key、Secret
這是鏈接,不多說。
2.下載SDK
SDK 在 github 上。
git clone https://github.com/heloyue/TCWeiboSDK
3.加入工程
- 把文件夾中的 doc 文件夾刪除,Xcode 會在編譯的時候彈出警告(內含 html、css 等文件)
-
把三個 .a 文件刪除到只剩 libTCWeiboSDK.a 不然編譯會報錯
linker command failed with exit code 1 (use -v to see invocation)
- 引入庫:Account.framework, Security.framework 和 Social.framework。
- TARGET => info =》URL Types,新增一個。URL Schemes 中填:wb88888888(88888888爲 Key 值)
4.開始寫代碼
1.在 AppDelegate.h 中
加入
@property (strong, nonatomic) PersonalCenterViewController *viewController;//PersonalCenterViewController 是我的 ViewController 這裏作爲例子,替換成你的
2.在 AppDelegate.m 中
實現這兩個方法
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [self.viewController.weiboApi handleOpenURL:url];//這裏的 weiboApi 待會兒再聲明
}
//Available in iOS 4.2 and later.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.viewController.weiboApi handleOpenURL:url];
}
3.在 ViewController.h 中
導入頭文件
#import "WeiboApi.h"
聲明 weiboApi
@interface PersonalCenterViewController : UIViewController{
WeiboApi* weiboApi;
}
@property (weak, nonatomic) IBOutlet UIButton *OAuth;
@property (nonatomic,retain) WeiboApi* weiboApi;
@end
4.在 ViewController.m 中
-
實例化 weiboApi
weiboApi = [[WeiboApi alloc]initWithAppKey:Key andSecret:Secret andRedirectUri:REDIRECTURI andAuthModeFlag:0 andCachePolicy:0] ;
-
實現回調函數
//授權成功的回調函數 - (void)DidAuthFinished:(WeiboApiObject *)wbobj { NSString *str = [[NSString alloc]initWithFormat:@"accesstoken = %@\r\n openid = %@\r\n appkey=%@ \r\n appsecret=%@ \r\n refreshtoken=%@ ", wbobj.accessToken, wbobj.openid, wbobj.appKey, wbobj.appSecret, wbobj.refreshToken]; self.result.text = str; NSLog(@"result = %@",str); //注意回到主線程,有些回調並不在主線程中,所以這裏必須回到主線程 // dispatch_async(dispatch_get_main_queue(), ^{ // // [self showMsg:str]; // }); // NSLog(@"after add pic"); }