iPhone开发技巧之网络Web服务

移动开发 iOS
本文介绍的是iPhone开发技巧之网络Web服务,详细的介绍了iphone开发应用中网络web服务,我们来看内容。

iPhone开发技巧之网络Web服务是本文要介绍的内容,说到XML不得不提WEB应用中最常见的几种通讯规范:SOAP,XML-RPC,REST,WSDL,JSON等,他们都是基于XML协定的。

在这里介绍几种处理web应用中可以利用的程序库:现在云计算技术很火,无论是类似 Google App Engine 的 PAAS 还是 Amazon EC2 的 IAAS 服务或者是类似 Twitter 的 SAAS。不可避免的都需要与 XML 打交道。所以掌握了这个标准,开发网络应用就不怕了。

关于这些协议的具体意义这里就不详述了,可查阅相关文档。这里只介绍一些封装好的类库,以便于开发。

WSDL2ObjC

WSDL2ObjC用来处理SOAP类型的web服务。同样也是基于libxml2的Objective-C类库。使用的时候除了libxml2的设定以外,还要添加 CFNetwork.framework 到工程中。

一个简单的例子如下所示:

  1.  - (IBAction)pressedRequestButton:(id)sender {  
  2.         FriendsBinding *bFriends = [[FriendsService FriendsBinding] retain];  
  3.         bFriends.logXMLInOut = YES;  
  4.         bFriends.authUsername = u.text;  
  5.         bFriends.authPassword = p.text;  
  6.         types_getFavoriteColorRequestType *cRequest = [[types_getFavoriteColorRequestType new] autorelease];  
  7.         cRequest.friend = @"Johnny";  
  8.         [bFriends getFavoriteColorAsyncUsingRequest:cRequest delegate:self];  
  9. }  
  10.  
  11. - (void) operation:(FriendsBindingOperation *)operation completedWithResponse:(FriendsBindingResponse *)response  
  12. {  
  13.         NSArray *responseresponseHeaders = response.headers;  
  14.         NSArray *responseresponseBodyParts = response.bodyParts;  
  15.  
  16.         for(id header in responseHeaders) {  
  17.                 // here do what you want with the headers, if there's anything of value in them  
  18.         }  
  19.  
  20.         for(id bodyPart in responseBodyParts) {  
  21.                 /****  
  22.                  * SOAP Fault Error  
  23.                  ****/  
  24.                 if ([bodyPart isKindOfClass:[SOAPFault class]]) {  
  25.                         // You can get the error like this:  
  26.                         tV.text = ((SOAPFault *)bodyPart).simpleFaultString;  
  27.                         continue;  
  28.                 }  
  29.  
  30.                 /****  
  31.                  * Get Favorite Color  
  32.                  ****/  
  33.                 if([bodyPart isKindOfClass:[types_getFavoriteColorResponseType class]]) {  
  34.                         types_getFavoriteColorResponseType *body = (types_getFavoriteColorResponseType*)bodyPart;  
  35.                         // Now you can extract the color from the response  
  36.                         q.text = body.color;  
  37.                         continue;  
  38.                 }  
  39. // ...  

json-framework

json-framework 是一个用 Objective-C 解析 JSON 的程序 Framework。下载后安装到 ~/Library/ 下。然后启动 XCode,编辑项目的设定,如下图:

图片地址:http://www.yifeiyang.net/images/iphone/e38394e382afe38381e383a3-1.png

编译设定中,双击「结构 > 添加SDK」添加下面的sdk。

$HOME/Library/SDKs/JSON/$(PLATFORM_NAME).sdk同样在「链接 > 其他的链接标记」中添加如下的值。

-ObjC -ljson最后,在代码中添加 #import <JSON/JSON.h> 就可以使用了。使用的例子如下所示:

  1. NSString *urlString =  
  2.         @"http://twitter.com/statuses/user_timeline/tomute.json";  
  3. NSURL *url = [NSURL URLWithString:urlString];  
  4. NSString *jsonString = [NSString stringWithContentsOfURL:url  
  5.                                  encoding:NSUTF8StringEncoding  
  6.                                  error:nil];  
  7.  
  8. NSArray *jsonArray = [jsonString JSONValue];  
  9. for (NSDictionary *dic in jsonArray) {  
  10.     // 打印信息  
  11.     NSLog([dic objectForKey:@"text"]);  
  12.     NSLog([dic objectForKey:@"created_at"]);  

需要注意的是,JSONValue解析后的返回值是 NSDictionary 或者是 NSArray ,所以像下面一样用id来表示返回的类型比较好。

上面的例子是取得Twitter信息的,url换为下面的后,又可以取得Flickr的照片了

http://api.flickr.com/services/rest/?method=flickr.photos.search&

api_key=@"APIKEY"&tags=@"Trip"&per_page=10&format=json&nojsoncallback=1

另外还有 TouchJSON,具体使用的方法都差不多,这里就不在叙述了。

CocoaREST

CocoaREST是一个用来处理RESTful的类库。如果你的程序想要处理Twitter,那么就可以用到它。

一个简单的例子如下所示:

  1. - (void) awakeFromNib {  
  2.     // inside a header file, declare manager as an instance variable  
  3.     SDTwitterManager *manager;  
  4.  
  5.     // create out manager, retaining it as we want it to stick around  
  6.     manager = [[SDTwitterManager manager] retain];  
  7.     manager.successSelector = @selector(twitterManager:resultsReadyForTask:);  
  8.     manager.failSelector = @selector(twitterManager:failedForTask:);  
  9.     manager.delegate = self;  
  10.  
  11.     // this is a must for certain API calls which require authentication  
  12.     // change them to real login values or the tasks will fail  
  13.     manager.username = @"USERNAME";  
  14.     manager.password = @"PASSWORD";  
  15.  
  16.     // 3 tasks can be run simultaneously  
  17.     manager.maxConcurrentTasks = 3;  
  18.  
  19.     // create and run a basic task  
  20.     SDTwitterTask *mentionsTask = [SDTwitterTask taskWithManager:manager];  
  21.     mentionsTask.type = SDTwitterTaskGetPersonalTimeline;  
  22.     mentionsTask.count = 3;  
  23.     mentionsTask.page = 10;  
  24.     [mentionsTask run];  
  25. }  
  26.  
  27. - (void) twitterManager:(SDTwitterManager*)manager resultsReadyForTask:(SDTwitterTask*)task {  
  28.     NSLog(@"%@", task.results);  
  29. }  
  30.  
  31. - (void) twitterManager:(SDTwitterManager*)manager failedForTask:(SDTwitterTask*)task {  
  32.     NSLog(@"%@", task.error);  

除此之外,当然还有很多的web服务应用,这里不能一一列举使用的方法,在以后会做一些更加详细的介绍。

小结:iPhone开发技巧之网络Web服务的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-19 09:58:36

2011-07-19 09:46:38

2011-08-10 15:48:10

iPhone网络

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-08-02 16:28:40

iPhone Web开发 事件

2015-06-17 10:28:10

WebAPP开发技巧

2015-06-04 10:44:59

WebAPP开发技巧

2011-10-18 13:58:32

高性能web

2011-08-15 11:31:27

iPhone开发日志

2011-08-01 18:27:58

iPhone开发 UISearchBa

2011-08-10 10:10:21

iPhoneUIPopoverCo

2012-04-26 13:26:58

iPhone应用技巧

2009-07-31 09:32:12

ASP.NET网络硬盘开发

2011-04-07 13:39:24

WebHTTP

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-08 13:57:19

iPhone开发 打包 DEB

2013-09-10 16:16:19

移动网站性能优化移动web

2012-05-17 11:45:12

iPhone

2011-03-17 13:38:37

2011-01-13 09:02:04

Webcloudrovi
点赞
收藏

51CTO技术栈公众号