利用iOS API编写简单微博客户端全过程
2014-11-11 13:55
302 查看
要编写社交网络客户端程序,可以大体上分为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代码
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代码
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代码
id jsonObj = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
解析的jsonObj对象结构根据社交网络服务的不同而不同,详细参数情况请参考不同服务的开发者网站。
下 面我们通过一个实例介绍一下SLRequest的使用,在表视图画面中,可以下拉刷新视图,获得最新的社交网络服务信息。点击画面导航栏的Action按 钮,会弹出撰写信息的模态视图(右图所示),撰写完成之后点击“Save”按钮发送信息,可以点击“Cancel”按钮取消发送。
下面我们按照这个流程,介绍一下:
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代码
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) ⑥ { <认证通过> } }; }];
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代码
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) { <处理请求结果> }];
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代码
id jsonObj = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
id jsonObj = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&err];
解析的jsonObj对象结构根据社交网络服务的不同而不同,详细参数情况请参考不同服务的开发者网站。
下 面我们通过一个实例介绍一下SLRequest的使用,在表视图画面中,可以下拉刷新视图,获得最新的社交网络服务信息。点击画面导航栏的Action按 钮,会弹出撰写信息的模态视图(右图所示),撰写完成之后点击“Save”按钮发送信息,可以点击“Cancel”按钮取消发送。
相关文章推荐
- 利用iOS API编写简单微博客户端全过程
- 利用iOS API编写简单微博客户端全过程
- 利用iOS API编写简单微博客户端全过程
- 利用iOS API编写简单微博客户端全过程
- 利用iOS API编写简单微博客户端全过程
- 利用iOS API编写简单微博客户端全过程
- iOS 类似简单的第三方微博客户端,可进行登录微博浏览相关信息
- 利用 Debug API 编写一个简单的脱壳机
- Socket在iOS客户端上的简单实现 - 利用GCAsyncSocket框架
- Sphinx 客户端API编写过程中遇到的问题
- iOS开发实践:一个类微博客户端从启动到与用户交互的过程
- pb中利用api使pb编写的窗口始终置前
- 针对FFMPEG0.49版本API编写的简单解码程序
- 编写、编译、安装、移除一个内核模块的基本过程,不能再简单了的
- Linux网络编程一步一步学-简单客户端编写
- 利用mfc类和odbcapi执行存储过程
- 利用OpenSSL编写SSL通信程序时常使用的API
- 在C#中利用API编写关机程序
- 简单介绍C#调用API的基本过程
- ATL入门:利用ATL编写简单的COM组件