本範例iOS App演示了以下控件的使用,從第三方網站獲取特定URL的Web內容,並顯示在UIWebView控件中。php
開關(UISwitch)ios
分段控件(UISegmentedControl)web
Web視圖(UIWebView)session
該範例 iPhone App運行效果以下所示:dom
下面是iOS App獲取特定URL Web內容的主要代碼:網站
- (IBAction)getFlower:(id)sender {
spa
NSURL *imageURL;
NSURL *detailURL;
NSString *imageURLString;
NSString *detailURLString;
NSString *color;
int sessionID;
color =[self.colorChoice titleForSegmentAtIndex:self.colorChoice.selectedSegmentIndex];
.net
sessionID = random() % 50000;
imageURLString = [[NSString alloc] initWithFormat:
code
@"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];
orm
detailURL = [[NSURL alloc] initWithString:detailURLString];
[self.flowerView loadRequest:[NSURLRequest requestWithURL:imageURL]];
[self.flowerDetailView loadRequest:[NSURLRequest requestWithURL:detailURL]];
self.flowerDetailView.backgroundColor = [UIColor clearColor];
}
上述代碼簡要解釋一下,其基本過程是:
首先建立一個NSString 字符串,也就是web網址;
根據web網址,建立一個NSURL實例,調用initWithString方法;
再建立一個可將其傳遞給Web視圖進行加載的NSURLRequest對象,調用requestWithURL方法;
最後,將該請求傳遞給Web 視圖的loadRequest 方法,該方法將接管工做,並處理加載過程,在Web 視圖中呈現網頁內容。