Just as with iPhone’s native Safari application, you can display web-based content in your own applications by using the UIWebView class. (In fact, Safari on the iPhone uses a UIWebView for display.)
Web views provide touch-based control for zooming in and out of pages, panning, and scrolling. Tapping links can load pages, and tapping in text controls will open a keyboard for data entry.
To display a web page in your application:
- Just as with other views, you can add web views to your interface in the usual way:
CGRect webRect = CGRectMake (10,10,300,400); UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect]; myWebView.scalesPageToFit = YES;
The scalesPageToFit property ensures that larger pages are zoomed out or in enough to fit correctly in the current frame as well as letting you zoom in and out in response to pinch gestures.
- Use the loadRequest: method to load content into the web view, which takes an NSURLRequest object as its only parameter:
NSURL *url = [NSURL URLWithString: @"http://www.google.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [myWebView loadRequest:request]; [self.view addSubview:myWebView];
This would load the Google homepage .
- If your page is taking a long time or you want to cancel loading, use the stopLoading method. You can also check the loading property to make sure the page is actually in the process of loading.
If you are building a web browser–type interface with Forward and Backward buttons, you can use the canGoForward and canGoBackward properties to determine whether your buttons should be enabled, and you can use the goForward and goBackward methods to navigate through the web view’s page history.
Although there is no direct access to the page history, you can easily maintain history via the delegate methods mentioned in the “What’s the status?” sidebar. Code Listing 4.27 shows the code updated to include an activity indicator when the page is loading.
Code Listing 4.27 Implementing a web view.
Loading local content and handling hyperlinks
You can also use web views to display local content, such as an .html file that ships in your application bundle. This makes web views handy for displaying content that mixes graphics with text or requires multiple text styles. (Remember that UILabels and UITextViews are restricted to only a single style per control.)
To load content from a local file:
- Use the loadHTMLString:baseURL: method to load content contained in the resources folder of the application bundle:
myWebView.scalesPageToFit = NO; NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"myPage" ofType:@"html"]; NSString *htmlContent = [NSString stringWithContents OfFile:htmlPath encoding: NSUTF8StringEncoding error:nil]; [myWebView loadHTMLString: htmlContent baseURL:nil];
This time, scalePageToFit has been set to NO because you don’t want the user to be able to zoom in and out of the web view as if it were a web page .
- To prevent a user from tapping any hyperlinks in the document, you can set userInteractionEnabled to NO for the web view. This also disables the ability to scroll content that may be longer than the control can fit on the screen
To disable links entirely, return NO from the webView:shouldStartLoadWith Request:navigationType: delegate method.
- To open a link in the native Safari application, you could write the following in the webView:shouldStartLoadWithRequest: navigationType: delegate method (Code Listing 4.28):
NSURL *pageURL = [request URL]; if ( ([[pageURL scheme] isEqualToString: @"http"]) && (navigationType == UIWebView NavigationTypeLinkClicked )) [[UIApplication sharedApplication] openURL:pageURL]; return NO;
Here you are trapping only http: links. Other link types, such as https:, would have no effect.
Code Listing 4.28 Capturing clicks on a web view.