IOS develops simple weather applications (get and XML)

Source: Internet
Author: User

1. Create an empty application and add the uiviewcontroller view. The view design is as follows:

 

// Homeviewcontroller. h

// Weatherquery

# Import <uikit/uikit. h>

@ Interface homeviewcontroller: uiviewcontroller <uiapplicationdelegate, uisearchbardelegate>
{
Nsmutabledata * data; // used to receive results returned by the server
Nsmutablearray * elements; // the text in all XML elements returned by the server
Nsstring * element; // The text stored when parsing an element in XML. After the element is parsed, it is added to elements.
}

@ Property (retain, nonatomic) iboutlet uilabel * city;
@ Property (retain, nonatomic) iboutlet uilabel * date;
@ Property (retain, nonatomic) iboutlet uitextview * detail;

@ End

API:

 

Idea: Get data, receive data, parse data, and display data

// Homeviewcontroller. m
// Weatherquery

# Import "homeviewcontroller. H"

@ Interface homeviewcontroller ()

@ End

@ Implementation homeviewcontroller

@ Synthesize city;
@ Synthesize date;
@ Synthesize detail;

// RESPONSE event of uisearchbardelegate Protocol
-(Void) searchbarsearchbuttonclicked :( uisearchbar *) searchbar
{
[Searchbar resignfirstresponder];
Nsstring * search = searchbar. text;
Nsstring * urlstring = [@ "http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather? Theuserid = XXXXXXXX & Thecitycode = "stringbyappendingstring: Search];
// Call the stringbyappendingstring of nsstring when generating an nsurl object
Nsurl * url = [nsurl urlwithstring: [urlstring stringbyaddingpercentescapesusingencoding: nsutf8stringencoding];
Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestreturncachedataelseload timeoutinterval: 60.0];

Data = [[nsmutabledata data] retain];
[Nsurlconnection connectionwithrequest: Request delegate: Self];
}

// Initialize the Data Length to 0 when the response is received
-(Void) connection :( nsurlconnection *) connection didreceiveresponse :( nsurlresponse *) Response
{
[Data setlength: 0];
}

// After receiving the data, add the data to the end of the data
-(Void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) incomingdata
{
[Data appenddata: incomingdata];
}

// After data transmission is complete, use nsxmlparser to parse the data
-(Void) connectiondidfinishloading :( nsurlconnection *) Connection
{
Nsxmlparser * parser = [[nsxmlparser alloc] initwithdata: Data];
[Parser setdelegate: Self];
Elements = [nsmutablearray arraywithcapacity: 0];
[Parser parse];
[Parser release];
[Data release];
}

// Connection failed. log output and Data Object released
-(Void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) Error
{
Nslog (@ "Weather query error: % @", [error localizeddescription]);
[Data release];
}

// Start parsing an XML Element and initialize the element as an empty string
-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME attributes :( nsdictionary *) attributedict
{
Element = @"";
}

// After parsing an XML element, add the element to the elements array.
-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri qualifiedname :( nsstring *) QNAME
{
[Elements addobject: element];
}

// Add the obtained string to the end of the element during element parsing. Because only some characters of the element are obtained here, the element parsing process will be performed multiple times.
// Call this method to connect all the strings.
-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String
{
Element = [element stringbyappendingstring: String];
}

// After parsing the XML document, the relevant information is displayed on the interface.
-(Void) parserdidenddocument :( nsxmlparser *) parser
{
City. Text = [elements objectatindex: 0];
Date. Text = [elements objectatindex: 3];
Detail. Text = [[[elements objectatindex: 4] stringbyappendingstring: @ "\ n"] stringbyappendingstring: [elements objectatindex: 6];
}

-(Void) viewdidload
{
[Super viewdidload];
// Do any additional setup after loading the view from Its nib.
}

-(Void) viewdidunload
{
[Self setcity: Nil];
[Self setdate: Nil];
[Self setdetail: Nil];

[Super viewdidunload];
// Release any retained subviews of the main view.
// E.g. Self. myoutlet = nil;
}

-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation = uiinterfaceorientationportrait );
}

-(Void) dealloc {
[City release];
[Date release];
[Detail release];

[Super dealloc];
}
@ End

 

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.