天天看點

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

新浪微網誌開放平台為第三方應用提供了簡便的合作模式,滿足了手機使用者和平闆電腦使用者随時随地分享資訊的需求。通過調用平台的api即可實作很多微網誌上的功能。

本篇主要目的是記錄新浪微網誌移動SDK iOS版本的在iOS5下的嵌入和使用。

申請位址:http://open.weibo.com/development,申請後得到App key 和 App Secret

下載下傳位址:http://open.weibo.com/wiki/SDK#iOS_SDK  ,下載下傳第一個就ok了。

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

導入SDK

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

這時候運作程式,你會發現很多關于ARC的錯誤,因為sdk裡是沒有使用arc的。這時候如果想sdk的檔案不參與arc方式的編譯,那就需要做下設定,在Build Phases裡添加“-fno-objc-arc”标示

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

輕按兩下需要辨別的檔案,輸入-fno-objc-arc。

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

這樣weibo SDK的檔案就不會以arc的方式編譯了。

這時候運作,程式就編譯運作正常了

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

登入調用

    [weiBoEnginelogIn]; 

登出調用

    [weiBoEnginelogOut];

發微網誌:

可以調用SDK預設的界面發送:

    WBSendView *sendView = [[WBSendViewalloc] initWithAppKey:appKeyappSecret:appSecrettext:@"test"image:[UIImageimageNamed:@"bg.png"]];

    [sendView setDelegate:self];

    [sendView show:YES];

lat

false

float

緯度,有效範圍:-90.0到+90.0,+表示北緯,預設為0.0。

long

經度,有效範圍:-180.0到+180.0,+表示東經,預設為0.0。

6步驟裡調用的是sdk裡封裝好的,那微網誌這麼api和功能,怎麼調用呢?

我們試着擷取個人資訊

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

這樣可以擷取微網誌自己的昵稱等資訊。

需要什麼用什麼接口,把loadRequestWithMethodName 改變成自己需要的接口,params參數改成需要的參數,就可以了。

有的接口是不需要params的,比如

statuses/friends_timeline.json擷取關注人的微網誌,這裡params可以是nil.

PS:本篇記錄用的是Oauth認證,xauth認證需要稽核資格才能使用的。 

過後,新浪微網誌授權彈出的網頁又有調整,中間還有過癱瘓情況。如果按上篇做出來的授權頁面就成這樣了:

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

第一:網頁頁面變大了,

第二:沒有了取消按鈕。

根據這個情況在sina weibo SDK裡做了寫調整

調整:增加一個關閉按鈕,彈出視窗大小。

在WBAuthorizeWebView.m檔案的方法:bounceOutAnimationStopped裡添加按鈕:

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方法原來就有。運作效果:

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

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

擴大視窗

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

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

- (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;  

}  

運作效果:

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

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

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

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

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:appKey, @"client_id",  

                                                                      @"code", @"response_type",  

                                                                      redirectURI, @"redirect_uri",   

                                                                      @"mobile", @"display",  

                                                                      @"true",@"forcelogin", nil];  

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

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

本文轉自夏雪冬日部落格園部落格,原文連結:http://www.cnblogs.com/heyonggang/p/3709791.html,如需轉載請自行聯系原作者

繼續閱讀