iOS Web Page(Basics)
iOS has UI component which display Web page. That is UIWebView
iOS has one more feature to show web site. Call Safari application directly.
WebView also safari feature but, it doesn’t open Safari as an new application.
The easy use is to show it as HELP page.
Create HELP pages in your web server and show them as help or instruction. You can change them any time.
UIWebView
Properties
Name | Description |
---|---|
scalePageToFit | Enable pinch action or not |
loading | While loading or not |
canGoBack | Whether you can go back the previous page or not |
canGoForward | Whether you can go forward or not |
Methods
Method | Description |
---|---|
-(void)loadRequest: (NSURLRequest *)request |
Load the page |
-(void)goBack | Go back the previous page |
-(void)goForward | Go forward the next page |
-(void)reload | Reload the current page |
-(void)stopLoading | Stop loading |
We can manage web view by using these methods. go, back, etc…
Delegate
UIWebViewDelegate is delegate for UIWebView.
These are delegation methods
Method | Description |
---|---|
-(void)webViewDidStartLoad:(UIWebView*)webView | when the web page starts to load |
-(void)webViewDidFinishLoad:(UIWebView*)webView | when the web page finish up loading |
And more methods,
Method | Description |
---|---|
– (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error | Failed to load web page |
– (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | Sent before a web view begins loading web page |
If shouldStartLoadWithRequest returns NO, never load web page.
It also can do custom operation like hook.(with javascript)
Sample
This sample shows just basic use of UIWebView
Display the specific web page. While loading, the indicator of the top is shown.
WebViewSampleViewController.h
@interface WebViewSampleViewController : UINavigationController<UIWebViewDelegate> @end
WebViewSampleViewController.m
@interface WebViewSampleViewController () @property (nonatomic) UIWebView *web; @end @implementation WebViewSampleViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Web view self.web = [[UIWebView alloc] initWithFrame:self.view.bounds]; self.web.scalesPageToFit = YES; // Enable pinch out or not self.web.delegate = self; [self.view addSubview:self.web]; // Load web page NSURL *url = [NSURL URLWithString:@"http://www.rakuten.com/"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; [self.web loadRequest:req]; } #pragma mark - UIWebViewDelegate -(void)webViewDidStartLoad:(UIWebView*)webView { // Start Load NSLog(@"Start load"); [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; // Indicator on } -(void)webViewDidFinishLoad:(UIWebView*)webView { // Finish Load NSLog(@"Finish load"); [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // Indicator off } @end
Result
Stop Loading
Use stopLoading method
[webView stopLoading];