UIWebView 的基本用法

一、创建UIWebView

CGRect bouds = [[UIScreen manScreen]applicationFrame];  

UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];

[self.view addSubview:webView]; 


二. 属性

webView.scalesPageToFit = YES ;//设置自适应屏幕


三.webView 3种加载方式

1、直接给出url地址即可将web content载入。

NSString *path = @"http://m.jumei.com/";

    NSURL *url = [[NSURL alloc] initWithString:path];

    [_webView loadRequest:[NSURLRequest requestWithURL:url]];


2、将本地html文件内容嵌入webView

(1)无脚本

    NSString *html = @"hello <h1>world<h1>";

(2)含有js脚本的

    NSString *html = @"<script language='javascript'>alert('hello');</script>'";

    [_webView loadHTMLString:html baseURL:nil];

(3)加载本地存在的html文件

    NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];

    NSString *filePath  = [resourcePath stringByAppendingPathComponent:@"my.html"];

    NSString *htmlstring =[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    [_webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];


3、将本地html文件内容嵌入webView 更详细的给出了web content的编码方式。

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *path = [bundle pathForResource:@"my" ofType:@"html"];

    //word.html存在工程的资源目录中

    NSData *data = [[NSData alloc]initWithContentsOfFile:path];

    [_webView loadData:data MIMEType:nil textEncodingName:nil baseURL:nil]


四.UIWebView 代理方法

先设置_webView.delegate = self;


当网页视图被指示载入内容而得到通知。应当返回YES

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;


加载前

- (void)webViewDidStartLoad:(UIWebView *)webView;


加载完成

- (void)webViewDidFinishLoad:(UIWebView *)webView;


加载失败

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;


五.界面动作

[webView goBack]; //返回   

[webView goForward];//向前    

[webView reload];//重载     

[webView stopLoading];//取消载入内容 


六.打开pdf、word、xls

    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 55, 320, 300)];  
    webView.delegate = self;  
    webView.multipleTouchEnabled = YES;  
    webView.scalesPageToFit = YES;  
      
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSURLRequest *request = [NSURLRequest requestWithURL:url];  
    [webView loadRequest:request];  
      
    [self.view addSubview:webView];  

七.其他设置

  • 去掉数字解析功能
webView.detectsPhoneNumbers=NO;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值