iPhone开发技巧之网络篇(2)— Web服务
iPhone开发技巧之网络篇(2)--- 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
- (IBAction)pressedRequestButton:(id)sender { FriendsBinding *bFriends = [[FriendsService FriendsBinding] retain]; bFriends.logXMLInOut = YES; bFriends.authUsername = u.text; bFriends.authPassword = p.text; types_getFavoriteColorRequestType *cRequest = [[types_getFavoriteColorRequestType new] autorelease]; cRequest.friend = @"Johnny"; [bFriends getFavoriteColorAsyncUsingRequest:cRequest delegate:self]; } - (void) operation:(FriendsBindingOperation *)operation completedWithResponse:(FriendsBindingResponse *)response { NSArray *responseHeaders = response.headers; NSArray *responseBodyParts = response.bodyParts; for(id header in responseHeaders) { // here do what you want with the headers, if there's anything of value in them } for(id bodyPart in responseBodyParts) { /**** * SOAP Fault Error ****/ if ([bodyPart isKindOfClass:[SOAPFault class]]) { // You can get the error like this: tV.text = ((SOAPFault *)bodyPart).simpleFaultString; continue; } /**** * Get Favorite Color ****/ if([bodyPart isKindOfClass:[types_getFavoriteColorResponseType class]]) { types_getFavoriteColorResponseType *body = (types_getFavoriteColorResponseType*)bodyPart; // Now you can extract the color from the response q.text = body.color; continue; } // ... } |
json-framework
json-framework 是一个用 Objective-C 解析 JSON 的程序 Framework。下载后安装到 ~/Library/ 下。然后启动 XCode,编辑项目的设定,如下图:
编译设定中,双击「结构 > 添加SDK」添加下面的sdk。
$HOME/Library/SDKs/JSON/$(PLATFORM_NAME).sdk
同样在「链接 > 其他的链接标记」中添加如下的值。
-ObjC -ljson
最后,在代码中添加 #import <JSON/JSON.h> 就可以使用了。使用的例子如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
NSString *urlString = @"http://twitter.com/statuses/user_timeline/tomute.json"; NSURL *url = [NSURL URLWithString:urlString]; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSArray *jsonArray = [jsonString JSONValue]; for (NSDictionary *dic in jsonArray) { // 打印信息 NSLog([dic objectForKey:@"text"]); NSLog([dic objectForKey:@"created_at"]); } |
需要注意的是,JSONValue解析后的返回值是 NSDictionary 或者是 NSArray ,所以像下面一样用id来表示返回的类型比较好。
1 |
id jsonItem = [jsonData JSONValue]; |
上面的例子是取得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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
- (void) awakeFromNib { // inside a header file, declare manager as an instance variable SDTwitterManager *manager; // create out manager, retaining it as we want it to stick around manager = [[SDTwitterManager manager] retain]; manager.successSelector = @selector(twitterManager:resultsReadyForTask:); manager.failSelector = @selector(twitterManager:failedForTask:); manager.delegate = self; // this is a must for certain API calls which require authentication // change them to real login values or the tasks will fail manager.username = @"USERNAME"; manager.password = @"PASSWORD"; // 3 tasks can be run simultaneously manager.maxConcurrentTasks = 3; // create and run a basic task SDTwitterTask *mentionsTask = [SDTwitterTask taskWithManager:manager]; mentionsTask.type = SDTwitterTaskGetPersonalTimeline; mentionsTask.count = 3; mentionsTask.page = 10; [mentionsTask run]; } - (void) twitterManager:(SDTwitterManager*)manager resultsReadyForTask:(SDTwitterTask*)task { NSLog(@"%@", task.results); } - (void) twitterManager:(SDTwitterManager*)manager failedForTask:(SDTwitterTask*)task { NSLog(@"%@", task.error); } |
除此之外,当然还有很多的web服务应用,这里不能一一列举使用的方法,在以后会做一些更加详细的介绍。
相关文章
- iPhone开发技巧之发布篇(7)--- 制作自己的Cydia发布源 - (2012-01-20)
- iPhone开发技巧之发布篇(6)--- 不需Developper认证的真机调试方法 - (2011-12-25)
- iPhone开发技巧之环境篇(11) --- 让Xcode对应多个版本的iOS SDK - (2011-12-03)
- iPhone开发技巧之发布篇(5)--- 在程序中添加广告 - (2011-11-20)
- iPhone开发技巧之环境篇(10)--- 在控制台调试iPhone应用程序 - (2011-11-13)
- iPhone开发技巧之调试篇(3)--- 程序Crash后的调试技巧 - (2011-11-06)
- iPhone开发技巧之环境篇(9)--- Xcode中的注释 - (2011-01-12)
- iPhone开发技巧之数据篇(2)--- iPhone程序中的加密处理 - (2011-01-10)
- iPhone开发技巧之发布篇(4)--- 使用 Ad Hoc 发布自己的应用程序 - (2010-07-22)
- iPhone开发技巧之发布篇(3)--- 你的程序被拒了吗? - (2010-07-19)
- iPhone开发技巧之发布篇(2)--- 税务相关手续 - (2010-07-16)
- iPhone开发技巧之发布篇(1)--- 登录银行信息 - (2010-07-12)
- iPhone开发技巧之工具篇(4)--- 使用afconvert转换WAV文件 - (2010-06-24)
- iPhone开发技巧之私有API(8)--- UIApplication - (2010-06-18)
- iPhone开发技巧之私有API(7)--- 用UIWebView访问BASIC认证的页面 - (2010-06-16)
- iPhone开发技巧之私有API(6)--- 设置UIWebView中的User-Agent - (2010-06-14)
- iPhone开发技巧之私有API(5)--- UISegmentedControl - (2010-06-11)
- iPhone开发技巧之私有API(4)--- UIBarButtonItem - (2010-06-09)
- iPhone开发技巧之私有API(3)--- UIButton - (2010-06-07)
- iPhone开发技巧之数据篇(1)--- 使用正则表达式 - (2010-06-04)
- iPhone开发技巧之私有API(2)--- UITableView - (2010-06-01)
- iPhone开发技巧之私有API(1) --- 设备相关信息 - (2010-05-28)
- iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器 - (2010-05-17)
- iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI - (2010-05-14)
- iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接 - (2010-05-12)
