IPhone development skills-Web Services

Source: Internet
Author: User

IPhoneDevelopment skillsWebThe service is the content to be introduced in this article.WEBThe most common communication specifications in applications: SOAP, XML-RPC, REST, WSDL, JSON, etc. They are all based on XML protocols.

Several processing methods are introduced here.WebLibraries that can be used in applications: currently cloud computing technology is very popular, whether it is like Google App Engine PAAS, Amazon EC2 IAAS service or Twitter-like SAAS. Inevitably, you need to deal with XML. Therefore, if you have mastered this standard, you will not be afraid to develop network applications.

The specific meanings of these protocols are not described here. You can refer to the relevant documents. Here we only introduce some encapsulated class libraries for development.

WSDL2ObjC

WSDL2ObjC is used to process SOAP-type web Services. It is also an Objective-C Class Library Based on libxml2. In addition to setting libxml2, you must add CFNetwork. framework to the project.

A simple example is as follows:

 
 
  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 is a program Framework that parses JSON using Objective-C. Download and install it ~ /Library. Start XCode and edit the project settings, such:

Image address: http://www.yifeiyang.net/images/iphone/e38394e382afe38381e383a3-1.png

In the compilation settings, double-click structure> Add SDK to add the following sdk.

$ HOME/Library/SDKs/JSON/$ (PLATFORM_NAME). The sdk also adds the following values in "link> other link tags.

-Add # import <JSON/JSON. h> to the code. The following is an example:

 
 
  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. // Print information
  11. NSLog ([dic objectForKey: @ "text"]);
  12. NSLog ([dic objectForKey: @ "created_at"]);
  13. }

It should be noted that the returned value after JSONValue resolution is NSDictionary or NSArray, so it is better to use id to indicate the type of the returned data as follows.

In the above example, the Twitter information is obtained. After the url is changed to the following, you can obtain the photo of the Flickr.

Http://api.flickr.com/services/rest? Method = flickr. photos. search &

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

In addition, TouchJSON is used in similar ways. We will not describe it here.

CocoaREST

CocoaREST is a class library used to process RESTful requests. If your program wants to process Twitter, you can use it.

A simple example is as follows:

 
 
  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);  

In addition, of course there are manyWebService applications. Here we cannot list the methods used in detail. We will introduce them in more detail later.

Summary:IPhoneDevelopment skillsWebThe service content has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.