利用iOS API編寫簡單微部落格戶端全過程

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   使用   java   sp   

要編寫社交網路用戶端程式,可以大體上分為4個主要的步驟

 

下面我們按照這個流程,介紹一下:

1、引入Accounts和Social架構

工 程中需要引入Accounts和Social架構,Accounts架構中有進行使用者賬戶認證所需類,Social架構中SLRequest類是我們所需 要的。添加具體步驟是選擇工程中的TARGETS→WeiBo→Build Phases→Link Binary With Libraries,選擇 右下角的“+”按鈕,開啟架構和庫選擇對話方塊。

分別選擇Social.framework添加,再選擇Accounts.framework添加。

 

2、使用者賬戶認證

用 戶賬戶認證使用ACAccount、ACAccountStore和ACAccountType類,ACAccount類是封裝使用者賬戶資訊,這些資訊存 儲在賬戶資料庫中,ACAccountStore類用來管理賬戶資料庫,ACAccountType類描述了賬戶類型。

認證過程的模板代碼如下:

Java代碼  
  1. ACAccountStore *account = [[ACAccountStore alloc] init]; ①  
  2.   
  3. ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:  
  4.   
  5. ACAccountTypeIdentifierSinaWeibo]; ②  
  6.   
  7. [account requestAccessToAccountsWithType:accountType options:nil  
  8.   
  9. completion:^(BOOL granted, NSError *error) ③  
  10.   
  11. {  
  12.   
  13. if (granted == YES) ④  
  14.   
  15. {  
  16.   
  17. NSArray *arrayOfAccounts = [account  
  18.   
  19. accountsWithAccountType:accountType]; ⑤  
  20.   
  21. if ([arrayOfAccounts count] > 0) ⑥  
  22.   
  23. {  
  24.   
  25. <認證通過>  
  26.   
  27. }  
  28.   
  29. };  
  30.   
  31. }];  
ACAccountStore *account = [[ACAccountStore alloc] init]; ①ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo]; ②[account requestAccessToAccountsWithType:accountType options:nilcompletion:^(BOOL granted, NSError *error) ③{if (granted == YES) ④{NSArray *arrayOfAccounts = [accountaccountsWithAccountType:accountType]; ⑤if ([arrayOfAccounts count] > 0) ⑥{<認證通過>}};}];

 

 

3、發送請求

使用者認證通過就可以進行發送使用SLRequest對象發送請求,建立SLRequest對象可以使用類級構造方法

Java代碼  
  1. requestForServiceType:requestMethod:URL:parameters:,下面是代碼是建立SLRequest對象:  
  2.   
  3. SLRequest *request = [SLRequest  requestForServiceType:SLServiceTypeSinaWeibo  
  4.   
  5. requestMethod:SLRequestMethodGET  
  6.   
  7. URL:requestURL  
  8.   
  9. parameters:parameters];  
  10.   
  11. 上面的代碼還只是建立了SLRequest對象,我們還需要為請求對象設定賬戶資訊,使用下面的語句:  
  12.   
  13. request.account = weiboAccount;  
  14.   
  15. weiboAccount賬戶資訊是我們從使用者賬戶資訊資料庫中獲得的,設定給請求對象的account屬性,然後才能提交給社交網路伺服器進行認證。  
  16.   
  17. 具體開始請求是通過調用SLRequest 的performRequestWithHandler:方法實現的,代碼如下:  
  18.   
  19. [request performRequestWithHandler:^(NSData *responseData,  
  20.   
  21. NSHTTPURLResponse *urlResponse, NSError *error) {  
  22.   
  23. <處理請求結果>  
  24.   
  25. }];  
requestForServiceType:requestMethod:URL:parameters:,下面是代碼是建立SLRequest對象:SLRequest *request = [SLRequest  requestForServiceType:SLServiceTypeSinaWeiborequestMethod:SLRequestMethodGETURL:requestURLparameters:parameters];上面的代碼還只是建立了SLRequest對象,我們還需要為請求對象設定賬戶資訊,使用下面的語句:request.account = weiboAccount;weiboAccount賬戶資訊是我們從使用者賬戶資訊資料庫中獲得的,設定給請求對象的account屬性,然後才能提交給社交網路伺服器進行認證。具體開始請求是通過調用SLRequest 的performRequestWithHandler:方法實現的,代碼如下:[request performRequestWithHandler:^(NSData *responseData,NSHTTPURLResponse *urlResponse, NSError *error) {<處理請求結果>}];

 

 

4、處理請求結果

請求結束會調用代碼塊,我們在代碼塊中處理請求結果。基本工作是解析資料,以及UI的更新等操作。這3個社交網路服務返回的都是JSON格式資料,其中代碼塊中的responseData參數可以使用NSJSONSerialization解析JSON對象:

Java代碼  
  1. id jsonObj = [NSJSONSerialization JSONObjectWithData:responseData  
  2.   
  3. options:NSJSONReadingAllowFragments error:&err];  
id jsonObj = [NSJSONSerialization JSONObjectWithData:responseDataoptions:NSJSONReadingAllowFragments error:&err];

 

解析的jsonObj對象結構根據社交網路服務的不同而不同,詳細參數情況請參考不同服務的開發人員網站。

下 面我們通過一個執行個體介紹一下SLRequest的使用,在表視圖畫面中,可以下拉重新整理視圖,獲得最新的社交網路服務資訊。點擊畫面導覽列的Action按 鈕,會彈出撰寫資訊的模態視圖(右圖所示),撰寫完成之後點擊“Save”按鈕發送資訊,可以點擊“Cancel”按鈕取消發送。

 

利用iOS API編寫簡單微部落格戶端全過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.