1. 程式人生 > >開放平臺:新浪微博 for iOS

開放平臺:新浪微博 for iOS

引言:

新浪微博幾乎是把全平臺數據的API介面都開放了出來,因此,很多優秀的第三方微部落格戶端在功能方面都非常的全面.

而通過SNS的分享推廣方式在App世界裡已經非常的普遍,甚至隨處可見,本篇主要介紹一下App是如何跟新浪微博關聯的.

參考資料: 

1.開發平臺首頁:

2.API文件首頁:

3.API錯誤程式碼說明地址:

4.iOS SDK 地址:

5.授權機制:

6.開發者管理中心


使用:

在管理中心中建立自己的應用以後,會得到AppKey App Secret 

這兩個值 是初始化新浪微博SDK必須要用到的兩個引數. 

當執行 login 函式時 可能遇到的錯誤如下

1:訪問出錯提示


表示: 微博SDK初始化時設定的 appRedirectURI  和微博開放平臺-開發者管理中心-應用資訊-高階資訊-OAuth2.0 授權設定-授權回撥頁

所設定的值不一樣,才會出現如上錯誤.



2:呼叫新浪微部落格戶端授權以後沒有正常返回應用.

1:檢查 URL type  "URL scheme” 是否設定了名為: sinaweibosso.加AppID 如圖: 

2:AppDelegate 中 是否實現委託函式:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"%@",url.scheme);
    //如果涉及其他應用互動,請做如下判斷,例如:還可能和新浪微博進行互動
    if ([url.scheme isEqualToString:Key_weiXinAppID]) {
        return [WXApi handleOpenURL:url delegate:self];
    }else if ([url.scheme isEqualToString:[@"sinaweibosso" stringByAppendingPathExtension:Key_sinaWeiboAppID]])
    {
        return [[SinaWeiBoManage defaultInstance].sinaWeibo handleOpenURL:url];
    }else
    {
        return YES;
    }
}

以上設定完成以後,不出意外,將會響應授權結果.

接下來就主要開始呼叫API來進行微博的資料互動了.

舉個簡單的例子,如何獲取授權使用者的個人資訊:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:userId forKey:@"uid"];
[sinaWeibo requestWithURL:@"users/show.json"
                   params:params
               httpMethod:@"GET"
                 delegate:self];

具體要傳入什麼引數,請查閱官方API文件.

得到結果後會響應成功或者失敗的委託:

此時可以用過連結名稱來識別請求型別:

- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
    //獲取關注列表
    if ([request.url hasSuffix:@"friendships/friends.json"])
    {
        if ([delegate respondsToSelector:@selector(sinaWeiBoManage:withFriendListResult:withRequestDataType:isSuccess:)]) {
            [delegate sinaWeiBoManage:self withFriendListResult:nil withRequestDataType:self.requestDataType isSuccess:NO];
        }
    }
}

關於iOS 6中內建微博功能:

在iOS6中蘋果集成了新浪微博的社交環境,所以,如果使用者在設定介面中授權了新浪微博賬戶,我們第三方應用中就可以直接使用,利用其發微博等等

首先引入兩個 新的 framework

分別是: 

Accounts.framework :用於獲取系統設定中的 賬戶資訊

Social.framework :用於對第三方開放平臺進行資料互動.

流程分為兩步:

首先要知道使用者有沒有在系統的 設定中 授權了對應的賬戶,

如果拿到對應的賬戶資訊以後就可以開始對第三方開放平臺進行資料互動了,程式碼如下:

// Create an account store object. 建立賬戶集合
ACAccountStore *accountStore = [[ACAccountStore alloc] init];

// Create an account type that ensures Twitter accounts are retrieved. 確定好 賬戶型別  新浪微博 還是 Facebook  還是 Twitter
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];

// Request access from the user to use their Twitter accounts. //非同步請求 來得到對應型別的賬戶資訊
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
    if(granted) {//如果 granted 返回 NO 表示一個賬戶都沒有設定
        // Get the list of Twitter accounts.
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; //可能存在多個,看你要用哪個,最好讓使用者選擇一下
        
        // For the sake of brevity, we'll assume there is only one Twitter account present.
        // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
        if ([accountsArray count] > 0) {
            // Grab the initial Twitter account to tweet from.
            ACAccount *sinaWeiboAccount = [accountsArray objectAtIndex:0];
            
            NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
            [params setObject:@"一條新的微博" forKey:@"status"];
            SLRequest *slRequest = [SLRequest requestForServiceType:SLServiceTypeSinaWeibo requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://open.weibo.cn/2/statuses/update.json"] parameters:params];
            slRequest.account = sinaWeiboAccount;//這行程式碼一定要賦值,負責資料互動一定失敗
            [slRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSLog(@"%@ %@",urlResponse.URL,error);
                
                NSDictionary *dic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
                NSLog(@"%@",dic);
            }];
            
        }
    }
}];

在拿到每個 ACAccount 以後 自身都有一個 identifier 啊在使用者確認選好了使用哪個賬戶時最好能夠儲存下來,那麼下次可以直接通過如下程式碼獲取到對應的賬戶

[accountStore accountWithIdentifier:sinaWeiboAccount.identifier];



總結:

SNS做為應用推廣的一個主要途徑,是必須好好學習一下的,如何勾起和引起使用者分享的慾望,讓更多的人知道你的應用,那麼離成功就不遠了.