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:
- - (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 *responseresponseHeaders = response.headers;
- NSArray *responseresponseBodyParts = 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 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:
- 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 ){
- // Print information
- NSLog ([dic objectForKey: @ "text"]);
- NSLog ([dic objectForKey: @ "created_at"]);
- }
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:
- - (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);
- }
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!