標籤:style blog class code tar ext
新浪微博開放平台為第三方應用提供了簡便的合作模式,滿足了手機使用者和平板電腦使用者隨時隨地分享資訊的需求。通過調用平台的api即可實現很多微博上的功能。
本篇主要目的是記錄新浪微博移動SDK iOS版本的在iOS5下的嵌入和使用。
1、申請一個新浪微博的行動裝置 App 。
申請地址:http://open.weibo.com/development,申請後得到App key 和 App Secret
2、下載iOS_sdk
:http://open.weibo.com/wiki/SDK#iOS_SDK ,下載第一個就ok了。
3、建立一個項目Sina_weibo,選擇single View app。而且使用5.0後的ARC特性 。 匯入解壓後的sdk
匯入SDK
4、適配SDK在arc環境下運行
這時候運行程式,你會發現很多關於ARC的錯誤,因為sdk裡是沒有使用arc的。這時候如果想sdk的檔案不參與arc方式的編譯,那就需要做下設定,在Build Phases裡添加“-fno-objc-arc”標示
雙擊需要標識的檔案,輸入-fno-objc-arc。
這樣weibo SDK的檔案就不會以arc的方式編譯了。
5、 在自己的工程裡面增加Security.framework。SDK需要使用Security.framework將OAuth認證以後的token放到keyChain裡面從而增加整個工程的安全性。
這時候運行,程式就編譯運行正常了
6、其他的和SDK裡的Demo一樣了
登入調用
[weiBoEnginelogIn];
登出調用
[weiBoEnginelogOut];
發微博:
可以調用SDK預設的介面發送:
WBSendView *sendView = [[WBSendViewalloc] initWithAppKey:appKeyappSecret:appSecrettext:@"test"image:[UIImageimageNamed:@"bg.png"]];
[sendView setDelegate:self];
[sendView show:YES];
對應的發送微博的api是:statuses/upload 發送微博並上傳圖片。如果在微博上顯示地圖,那就發送經緯度參數,多加上
lat |
false |
float |
緯度,有效範圍:-90.0到+90.0,+表示北緯,預設為0.0。 |
long |
false |
float |
經度,有效範圍:-180.0到+180.0,+表示東經,預設為0.0。 |
7、調用自訂api
6步驟裡調用的是sdk裡封裝好的,那微博這麼api和功能,怎麼調用呢?
我們試著擷取個人資訊
[cpp] view plaincopy
- NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
- [params setObject:[engine accessToken]forKey:@"access_token"];
- [params setObject:[engine userID]forKey:@"uid"];
- NSLog(@"params:%@", params);
-
- [engine loadRequestWithMethodName:@"users/show.json"
- httpMethod:@"GET"
- params:params
- postDataType:kWBRequestPostDataTypeNone
- httpHeaderFields:nil];
params的參數是必須的。
返回的資料參考介面http://open.weibo.com/wiki/2/users/show
這樣可以擷取微博自己的暱稱等資訊。
微博所有api文檔都在這個頁面http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2,使用的方法和例子都有。
需要什麼用什麼介面,把loadRequestWithMethodName 改變成自己需要的介面,params參數改成需要的參數,就可以了。
有的介面是不需要params的,比如
statuses/friends_timeline.json擷取關注人的微博,這裡params可以是nil.
PS:本篇記錄用的是Oauth認證,xauth認證需要審核資格才能使用的。
8、項目源碼:http://download.csdn.net/detail/totogo2010/4633077
繼上篇 iOS學習之iOS5.0以上 使用新浪微博開放平台OAuth
過後,新浪微博授權彈出的網頁又有調整,中間還有過癱瘓情況。如果按上篇做出來的授權頁面就成這樣了:
第一:網頁頁面變大了,
第二:沒有了取消按鈕。
根據這個情況在sina weibo SDK裡做了寫調整
調整:增加一個關閉按鈕,快顯視窗大小。
在WBAuthorizeWebView.m檔案的方法:bounceOutAnimationStopped裡添加按鈕:
[cpp] view plaincopy
- UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [closeButton setFrame:CGRectMake(280, 430, 60, 60)];
- [closeButton setImageEdgeInsets:UIEdgeInsetsMake(3, 0, 0, 0)];
- [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
-
- [closeButton addTarget:self action:@selector(hideAndCleanUp) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:closeButton];
close.png圖片sdk裡內建就有。hideAndCleanUp方法就是把視窗移除。hideAndCleanUp方法原來就有。運行效果:
看右下角有個關閉按鈕,為什麼放在右下角呢,因為右上方有個註冊按鈕,容易被點到。一會把網頁視窗最大化了就能看到了。
擴大視窗
在WBAuthorizeWebView.m檔案的方法- (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation 修改如下:
上面的尺寸是橫屏的時候的,我修改了豎屏時的視窗的大小。
[cpp] view plaincopy
- - (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation
- {
- [self setTransform:CGAffineTransformIdentity];
-
- if (UIInterfaceOrientationIsLandscape(orientation))
- {
- [self setFrame:CGRectMake(0, 0, 480, 320)];
- [panelView setFrame:CGRectMake(10, 30, 460, 280)];
- [containerView setFrame:CGRectMake(10, 10, 440, 260)];
- [webView setFrame:CGRectMake(0, 0, 440, 260)];
- [indicatorView setCenter:CGPointMake(240, 160)];
- }
- else
- {
- [self setFrame:CGRectMake(0, 5, 320, 470)];
- [panelView setFrame:CGRectMake(0, 5, 320, 470)];
- [containerView setFrame:CGRectMake(0, 5, 320, 460)];
- [webView setFrame:CGRectMake(0, 0, 320, 460)];
- [indicatorView setCenter:CGPointMake(160, 240)];
- }
-
- [self setCenter:CGPointMake(160, 240)];
-
- [self setTransform:[self transformForOrientation:orientation]];
-
- previousOrientation = orientation;
- }
運行效果:
這個狀態差不多就可以了。
還有在調用WeiBoEngine 的Logout 登出無效的情況。修改如下:
在WBAuthorize.m檔案,把startAuthorize函數修改如下:
[cpp] view plaincopy
- NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:appKey, @"client_id",
- @"code", @"response_type",
- redirectURI, @"redirect_uri",
- @"mobile", @"display",
- @"true",@"forcelogin", nil];
就是在 params裡添加@”true”,@”forcelogin”。
以上是使用新浪微博sdk開發遇到的問題和解決的一些方法。
修改過的項目代碼:http://download.csdn.net/detail/totogo2010/4928029
來源:http://blog.csdn.net/totogo2010/article/details/8435174