In this example, the iOS app demonstrates the use of the following controls to obtain the web content of a specific URL from a third-party website and display it in the uiwebview control.
- Uiswitch)
- Uisegmentedcontrol)
- Web View)
The following figure shows how the iPhone app runs in this example:
The following is the main method for iOS apps to obtain the web content of a specific URL.Code:
-(Ibaction) getflower :(ID) Sender {
Nsurl * imageurl;
Nsurl * detailurl;
Nsstring * imageurlstring;
Nsstring * detailurlstring;
Nsstring * color;
IntSessionid;
Color = [self. colorchoice titleforsegmentatindex: Self. colorchoice. selectedsegmentindex];
Sessionid = random () %50000;
Imageurlstring = [[nsstring alloc] initwithformat:
@"Http://www.floraphotographs.com/showrandomios.php? Color = % @ & session = % d", [Diccolor objectforkey: Color], sessionid];
Detailurlstring = [[nsstring alloc] initwithformat:
@"Http://www.floraphotographs.com/detailios.php? Session = % d", Sessionid];
Imageurl = [[nsurl alloc] initwithstring: imageurlstring];
Detailurl = [[nsurl alloc] initwithstring: detailurlstring];
[Self. flowerview loadrequest: [nsurlrequest requestwithurl: imageurl];
[Self. flowerdetailview loadrequest: [nsurlrequest requestwithurl: detailurl];
Self. flowerdetailview. backgroundcolor = [uicolor clearcolor];
}
The above code briefly explains the basic process as follows:
- First, create an nsstring, that is, the web URL;
- Create an nsurl instance based on the web URL and call the initwithstring method;
- Create an nsurlrequest object that can be passed to the Web View for loading and call the requestwithurl method;
- Finally, the request is passed to the loadrequest method of the Web View. This method takes over the work, processes the loading process, and displays the webpage content in the Web View.
Download the source code of this example app.
IOS 5 Programming (1)-Use of image views, slide blocks, and step controls
IOS 5 Programming (3)-create mode switching, multi-scenario and switching)