IOS UIWebView Usage

Source: Internet
Author: User

First create the UIWebView, this is not difficult, set the frame, and then add to the Self.view:

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕[self.view addSubview:webView];
UIWebView Load Network Address

UIWebView to load the network address, that my blog: http://blog.csdn.net/dylan_lwb_ for example. The code is as follows:

NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blog.csdn.net/dylan_lwb_"]];[self.view addSubview:webView];[webView loadRequest:request];
UIWebView loading local resources via Nsurlrequest
NSString *path = [[NSBundle mainBundle] pathForResource:@"swift" ofType:@"html"];NSURL* url = [NSURL   fileURLWithPath:path];//创建URLNSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest[webView loadRequest:request];//加载
Load via NSString

uiwebview also supports loading a NSString object as a source. You can provide it with a base URL to instruct the UIWebView object how to follow the link and load the remote resource:

NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:path]];
Automatic phone detection

Set this property, if you encounter a phone number in the loaded Web page, you can dial it directly by clicking it, it is very convenient:

webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;//自动检测网页上的电话号码,单击可以拨打
Set Proxy and Proxy methods

uiwebview has five proxy methods, we can complete different requirements in different proxy methods.

//设置代理webView.delegate = self;//代理方法- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ //返回YES,进行加载。通过UIWebViewNavigationType可以得到请求发起的原因 return YES;}- (void)webViewDidStartLoad:(UIWebView *)webView{ //开始加载,可以加上风火轮(也叫菊花)}- (void)webViewDidFinishLoad:(UIWebView *)webView{ //完成加载}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ //加载出错}
Create load him, stop, rewind, forward button

Create four buttons, namely Load button, stop button, Back button, Forward button:

nsarray *array = [Nsarray arraywithobjects:@" Load "@" Stop "@" back " , @ "forward", nil]; int far = (screen.width-200)/5; for (int i = 0; i < Array.count; i++) {UIButton *button = [[UIButton alloc] init]; Button.frame = CGRectMake ((i + 1) * far + * I, screen.height-60, 50, 40); Button.tag = + i; [Button addtarget:self action: @selector (Actionclick:) forcontrolevents:uicontroleventtouchupinside]; [Button Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal]; [Button Setbackgroundcolor:[uicolor Orangecolor]; [Button settitle:array[i] forstate:uicontrolstatenormal]; [Self.view Addsubview:button]; }
Forward and backward

To bind events to four buttons, distinguish each click event according to the tag value:

- (void)actionClick:(UIButton *)button{ switch (button.tag) { case 200: { [self.webView reload];//加载 } break; case 201: { [self.webView stopLoading];//停止加载 } break; case 202: { [self.webView goBack];//返回 } break; case 203: { [self.webView goForward];//前进 } break; default: break; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS UIWebView Usage

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.