iOS5.0以上使用新浪微博開放平台OAuth 續(及解決登入無效問題)

來源:互聯網
上載者:User

標籤: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
  1. NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];  
  2.     [params setObject:[engine accessToken]forKey:@"access_token"];  
  3.     [params setObject:[engine userID]forKey:@"uid"];  
  4.     NSLog(@"params:%@", params);  
  5.       
  6.     [engine loadRequestWithMethodName:@"users/show.json"  
  7.                            httpMethod:@"GET"  
  8.                                params:params  
  9.                          postDataType:kWBRequestPostDataTypeNone  
  10.                      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
  1. UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  2.     [closeButton setFrame:CGRectMake(280, 430, 60, 60)];  
  3.     [closeButton setImageEdgeInsets:UIEdgeInsetsMake(3, 0, 0, 0)];  
  4.     [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];  
  5.       
  6.     [closeButton addTarget:self action:@selector(hideAndCleanUp) forControlEvents:UIControlEventTouchUpInside];  
  7.     [self addSubview:closeButton];  


close.png圖片sdk裡內建就有。hideAndCleanUp方法就是把視窗移除。hideAndCleanUp方法原來就有。運行效果:

看右下角有個關閉按鈕,為什麼放在右下角呢,因為右上方有個註冊按鈕,容易被點到。一會把網頁視窗最大化了就能看到了。

擴大視窗

在WBAuthorizeWebView.m檔案的方法- (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation 修改如下:

上面的尺寸是橫屏的時候的,我修改了豎屏時的視窗的大小。

[cpp] view plaincopy
  1. - (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation  
  2. {  
  3.     [self setTransform:CGAffineTransformIdentity];  
  4.       
  5.     if (UIInterfaceOrientationIsLandscape(orientation))  
  6.     {  
  7.         [self setFrame:CGRectMake(0, 0, 480, 320)];  
  8.         [panelView setFrame:CGRectMake(10, 30, 460, 280)];  
  9.         [containerView setFrame:CGRectMake(10, 10, 440, 260)];  
  10.         [webView setFrame:CGRectMake(0, 0, 440, 260)];  
  11.         [indicatorView setCenter:CGPointMake(240, 160)];  
  12.     }  
  13.     else  
  14.     {  
  15.         [self setFrame:CGRectMake(0, 5, 320, 470)];  
  16.         [panelView setFrame:CGRectMake(0, 5, 320, 470)];  
  17.         [containerView setFrame:CGRectMake(0, 5, 320, 460)];  
  18.         [webView setFrame:CGRectMake(0, 0, 320, 460)];  
  19.         [indicatorView setCenter:CGPointMake(160, 240)];  
  20.     }  
  21.       
  22.     [self setCenter:CGPointMake(160, 240)];  
  23.       
  24.     [self setTransform:[self transformForOrientation:orientation]];  
  25.       
  26.     previousOrientation = orientation;  
  27. }  

運行效果:

這個狀態差不多就可以了。

 

還有在調用WeiBoEngine 的Logout 登出無效的情況。修改如下:

在WBAuthorize.m檔案,把startAuthorize函數修改如下:

[cpp] view plaincopy
  1. NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:appKey, @"client_id",  
  2.                                                                       @"code", @"response_type",  
  3.                                                                       redirectURI, @"redirect_uri",   
  4.                                                                       @"mobile", @"display",  
  5.                                                                       @"true",@"forcelogin", nil];  


就是在 params裡添加@”true”,@”forcelogin”。

以上是使用新浪微博sdk開發遇到的問題和解決的一些方法。

修改過的項目代碼:http://download.csdn.net/detail/totogo2010/4928029

 

來源:http://blog.csdn.net/totogo2010/article/details/8435174

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.