
最近(?)、中国進出が騒がれる中、ついに中国語対応のアプリ依頼が・・。元々のアプリはTwitter連携だけど、中国ではTwitterは閲覧不可なので中国語版ツイッターであるWeibo(微博)を代用することに。基本的な流れはTwitterやFacebookなど各種SDKと変わらないが、全て中国語なのが辛い。
手順
手順1. Weiboアカウント登録
まずはWeiboのアカウントを登録します。
⇒ http://weibo.com/signup/signup.php
手順2. Weibo開発者登録
手順1のWeiboアカウント登録とは別に、開発者登録が必要になります。
⇒ http://open.weibo.com/developers/basicinfo
手順3. Weiboアプリケーション登録
iOSで利用するためのアプリケーション登録をします。
⇒ http://open.weibo.com/apps
- 「创建应用」(アプリケーション新規登録)をクリック。
- 「移动应用」(モバイルアプリケーション)をクリック。
- AppKeyとAppKeySecretが発行される。(↓こんな感じです)
- 「应用信息」→「高级信息」から「OAuth2.0 授权设置」(OAuth2.0の認証設定)でコールバック設定をする。(特に指定がなければ”https://api.weibo.com/oauth2/default.html”でもよいみたい)
手順4. iOSにWeiboSDKの設定
WeiboSDKの対応言語は幅広くに対応している。下記から「iOS SDK」を選択して、遷移先のgithubのページからSDKをダウンロードする。git cloneなら下記の通り。
⇒ http://open.weibo.com/wiki/SDK
1 |
[]$ git clone https://github.com/sinaweibosdk/weibo_ios_sdk.git |
ダウンロードされたフォルダの中にあるlibWeiboSDKフォルダを自分のプロジェクトフォルダ内にコピーして、さらにXcode上にドロップ&ドラッグで追加する。下記キャプチャ画像↓のように「Link Binary With Library」に「libWeiboSDK.a」、「Copy Bundle Resources」にWeiboSDK.bundle」が追加されていればOK。
手順4. iOSで実装1 (設定)
まずは全体の流れとしては下記の通り。
1. 「Prefix.pch」
アプリキーとコールバックURIをそれぞれ定義しておきます。どちらもWeiboのアプリ管理ページで設定・取得できます。
1 2 |
#define kAppKey @"アプリキー" #define kRedirectURI @"https://api.weibo.com/oauth2/default.html" // アプリのコールバック設定と合わせる。 |
2. AppDelegate.h
1 2 3 4 5 6 7 8 9 |
#import <UIKit/UIKit.h> #import "WeiboSDK.h" // 追加 @interface AppDelegate : UIResponder <UIApplicationDelegate, WeiboSDKDelegate> // デリゲート追加 @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) NSString *wbtoken; // 追加 (トークン用) @end |
3. AppDelegate.m
WebioSDKを設定し、デリゲートメソッドを3つ追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [WeiboSDK enableDebugMode:YES]; // 追加 [WeiboSDK registerApp:kAppKey]; // 追加 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window makeKeyAndVisible]; WeiboViewController* weiboViewController = [[WeiboViewController alloc] init]; self.window.rootViewController = weiboViewController; return YES; } pragma - Weibo SDK delegate methods - (void)didReceiveWeiboRequest:(WBBaseRequest *)request { if ([request isKindOfClass:WBProvideMessageForWeiboRequest.class]) { WeiboViewController *controller = [[WeiboViewController alloc] init]; [self.window.rootViewController presentViewController:controller animated:YES completion:nil]; } } - (void)didReceiveWeiboResponse:(WBBaseResponse *)response { if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]) { NSString *title = @"发送结果"; NSString *message = [NSString stringWithFormat:@"响应状态: %d\n响应UserInfo数据: %@\n原请求UserInfo数据: %@", response.statusCode, response.userInfo, response.requestUserInfo]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; } else if ([response isKindOfClass:WBAuthorizeResponse.class]) { NSString *title = @"认证结果"; NSString *message = [NSString stringWithFormat:@"响应状态: %d\nresponse.userId: %@\nresponse.accessToken: %@\n响应UserInfo数据: %@\n原请求UserInfo数据: %@", response.statusCode, [(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], response.userInfo, response.requestUserInfo]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; self.wbtoken = [(WBAuthorizeResponse *)response accessToken]; [alert show]; } } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [WeiboSDK handleOpenURL:url delegate:self]; } これでひとまず、Weiboの基本設定完了。次にWeiboでのログインとツイートを実装します。 |
手順5. iOSで実装2 (ツイート)
WeiboViewControllerを作成します。ここでログイン&ツイートボタンとそれぞれの処理を実装します。コードは以下の通り。
WeiboViewController.h
1 2 3 4 5 |
#import <UIKit/UIKit.h> @interface WeiboViewController : UIViewController @end |
WeiboViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#import "WeiboViewController.h" #import "WeiboSDK.h" #import "AppDelegate.h" @interface WeiboViewController () <WBHttpRequestDelegate> @end @implementation WeiboViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = UIColor.grayColor; // ログインボタン UIButton *ssoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [ssoButton setTitle:@"ログイン" forState:UIControlStateNormal]; [ssoButton addTarget:self action:@selector(ssoButtonPressed) forControlEvents:UIControlEventTouchUpInside]; ssoButton.frame = CGRectMake(20, 250, 280, 50); [self.view addSubview:ssoButton]; UIButton *tweetButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [tweetButton setTitle:@"つぶやく" forState:UIControlStateNormal]; [tweetButton addTarget:self action:@selector(tweetButtonPressed) forControlEvents:UIControlEventTouchUpInside]; tweetButton.frame = CGRectMake(20, 370, 280, 50); [self.view addSubview:tweetButton]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)ssoButtonPressed { WBAuthorizeRequest *request = [WBAuthorizeRequest request]; request.redirectURI = kRedirectURI; request.scope = @"all"; request.userInfo = @{@"SSO_From": @"WeiboViewController", @"Other_Info_1": [NSNumber numberWithInt:123], @"Other_Info_2": @[@"obj1", @"obj2"], @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}}; [WeiboSDK sendRequest:request]; } - (void)tweetButtonPressed { NSLog(@"*** tweetButtonPressed "); AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate]; [WBHttpRequest requestWithAccessToken:myDelegate.wbtoken url:@"https://api.weibo.com/2/statuses/update.json" httpMethod:@"POST" params:@{@"status":@"Hello World"} delegate:self withTag:nil]; } - (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result { NSString *title = nil; UIAlertView *alert = nil; title = @"成功しました。"; alert = [[UIAlertView alloc] initWithTitle:title message:[NSString stringWithFormat:@"%@",result] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } - (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error; { NSString *title = nil; UIAlertView *alert = nil; title = @"失敗しました。"; alert = [[UIAlertView alloc] initWithTitle:title message:[NSString stringWithFormat:@"%@",error] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } @end |
上記のコードはGithubにもUP。 ⇒ kuman/ios-weibo-test
テスト
シミュレーターを起動し、「ログイン」ボタンを押すとWebioのブラウザベースのWeiboログインフォームが表示される。ログイン後に「つぶやく」ボタンをタッチするとWeibo上に”Hello World!”とツイートされる。
MEMO
- ドキュメントが中国語が辛い。
- API操作はWeiboWikiのドキュメントを見れればまぁいける。
- ログイン後にコールバックでアプリに戻ってくれず少しはまる。(Weibo管理画面のバンドルID/コールバック設定やXcode上のpListなど見直して解決)
- Google ChromeだとWeiboの新規登録ができなかったのでFirefoxで登録した。