自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開發(fā)技巧之網(wǎng)絡(luò)Web服務(wù)

移動開發(fā) iOS
本文介紹的是iPhone開發(fā)技巧之網(wǎng)絡(luò)Web服務(wù),詳細的介紹了iphone開發(fā)應用中網(wǎng)絡(luò)web服務(wù),我們來看內(nèi)容。

iPhone開發(fā)技巧之網(wǎng)絡(luò)Web服務(wù)是本文要介紹的內(nèi)容,說到XML不得不提WEB應用中最常見的幾種通訊規(guī)范:SOAP,XML-RPC,REST,WSDL,JSON等,他們都是基于XML協(xié)定的。

在這里介紹幾種處理web應用中可以利用的程序庫:現(xiàn)在云計算技術(shù)很火,無論是類似 Google App Engine 的 PAAS 還是 Amazon EC2 的 IAAS 服務(wù)或者是類似 Twitter 的 SAAS。不可避免的都需要與 XML 打交道。所以掌握了這個標準,開發(fā)網(wǎng)絡(luò)應用就不怕了。

關(guān)于這些協(xié)議的具體意義這里就不詳述了,可查閱相關(guān)文檔。這里只介紹一些封裝好的類庫,以便于開發(fā)。

WSDL2ObjC

WSDL2ObjC用來處理SOAP類型的web服務(wù)。同樣也是基于libxml2的Objective-C類庫。使用的時候除了libxml2的設(shè)定以外,還要添加 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,編輯項目的設(shè)定,如下圖:

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

編譯設(shè)定中,雙擊「結(jié)構(gòu) > 添加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服務(wù)應用,這里不能一一列舉使用的方法,在以后會做一些更加詳細的介紹。

小結(jié):iPhone開發(fā)技巧之網(wǎng)絡(luò)Web服務(wù)的內(nèi)容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-19 09:46:38

2011-07-19 09:58:36

2011-08-10 15:48:10

iPhone網(wǎng)絡(luò)

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-08-02 16:28:40

iPhone Web開發(fā) 事件

2011-10-18 13:58:32

高性能web

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-15 11:31:27

iPhone開發(fā)日志

2015-06-04 10:44:59

WebAPP開發(fā)技巧

2015-06-17 10:28:10

WebAPP開發(fā)技巧

2009-07-31 09:32:12

ASP.NET網(wǎng)絡(luò)硬盤開發(fā)

2012-04-26 13:26:58

iPhone應用技巧

2013-09-10 16:16:19

移動網(wǎng)站性能優(yōu)化移動web

2011-04-07 13:39:24

WebHTTP

2011-08-08 13:57:19

iPhone開發(fā) 打包 DEB

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2012-05-17 11:45:12

iPhone

2011-03-17 13:38:37

2011-07-18 14:39:53

iPhone SDK UIKit
點贊
收藏

51CTO技術(shù)棧公眾號