·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 利用iOSAPI编写简单微博客户端全过程

利用iOSAPI编写简单微博客户端全过程

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

要编写社交网络客户端程序,可以大体上分为4个主要的步骤

 

5

下面我们按照这个流程,介绍一下:

1、引入Accounts和Social框架

工 程中需要引入Accounts和Social框架,Accounts框架中有进行用户账户认证所需类,Social框架中SLRequest类是我们所需 要的。添加具体步骤是选择工程中的TARGETS→WeiBo→Build Phases→Link Binary With Libraries,选择 右下角的“+”按钮,打开框架和库选择对话框。

6

分别选择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:nil

completion:^(BOOL granted, NSError *error) ③

{

if (granted == YES) ④

{

NSArray *arrayOfAccounts = [account

accountsWithAccountType: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:SLServiceTypeSinaWeibo

requestMethod:SLRequestMethodGET

URL:requestURL

parameters: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:responseData

options:NSJSONReadingAllowFragments error:&err];

 

解析的jsonObj对象结构根据社交网络服务的不同而不同,详细参数情况请参考不同服务的开发者网站。

下 面我们通过一个实例介绍一下SLRequest的使用,在表视图画面中,可以下拉刷新视图,获得最新的社交网络服务信息。点击画面导航栏的Action按 钮,会弹出撰写信息的模态视图(右图所示),撰写完成之后点击“Save”按钮发送信息,可以点击“Cancel”按钮取消发送。

7