UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];[self.view addSubview:webView];

报错:NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802) 或者 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

其中在iOS 9 新建的项目中这种加载 要注意plist的配置 App Transport Security Settings 中添加Allow Arbitrary Loads 改 NO 为 YES。

加载本地HTML

    // 加载本地文件//1NSString* path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"html" inDirectory:@"library"];//library是根目录,name是文件名称,html是文件类型[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];//2NSString * filePath = [[NSBundle mainBundle] pathForResource:@"agreement" ofType:@"html"];NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];//3NSString *resourcePath = [[NSBundle mainBundle] resourcePath];NSString *filePath = [resourcePath stringByAppendingPathComponent:@"name.html"];NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];[webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

一些属性的设置:

webView.scalespageToFit = YES; // 对页面进行缩放以适应屏幕

webView.detectsPhoneNumbers = YES; //检测网页上的电话号码,单击可以拨打

//为UIWebview添加背景图片

webView.backgroundColor=[UIColor clearColor];

webView.opaque=NO;//这句话很重要,webView是否是不透明的,no为透明 在webView下添加个imageView展示图片就可以了

    [webView goBack];      //后退[webView goForward];   //前进[webView reload];      //重载[webView stopLoading]; //取消载入内容

一些代理方法:

#pragma mark ---Delegate-(void) webViewDidStartLoad:(UIWebView *)webView{NSLog(@"开始加载---") ;<pre name="code" class="objc">   // starting the load, show the activity indicator in the status bar    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;}- (void) webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"加载完成---");// finished loading, hide the activity indicator in the status bar[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;//获取当前页面的title        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];    NSLog(@"title====%@",title);        //获取当前URL        NSString *URL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];    NSLog(@"URL===%@",URL);        //得到网页代码        NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML" ];     NSLog(@"html====%@",html);// document.documentElement.textContent        //拼接字符串 根据网页name找到控价并赋值        NSString *str = @"随_的简书";    NSString *JSStr = [NSString stringWithFormat: @"document.getElementsByName('q')[0].value = ('%@');",str];    [webView stringByEvaluatingJavaScriptFromString:JSStr];
}- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"加载失败===%@",error);// report the error inside the webviewNSString* errorString = [NSString stringWithFormat:@"An error occurred:%@",error.localizedDescription];[self.myWebView loadHTMLString:errorString baseURL:nil];}

其中:-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType: (UIWebViewNavigationType)navigationType;//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值:

UIWebViewNavigationTypeLinkClicked

UIWebViewNavigationTypeFormSubmitted

UIWebViewNavigationTypeBackForward

UIWebViewNavigationTypeReload

UIWebViewNavigationTypeFormResubmitted

UIWebViewNavigationTypeOther

scrollview的代理方法

//当网页位置为顶部 不允许继续下拉- (void) scrollViewDidScroll:(UIScrollView *)scrollView {if (self.webView.frame.origin.y == 0) {self.webView.scrollView.bounces = NO;return;}
}

UIWebView的小知识点最下面的知识点

1.

//Stopping a load request when the web view is to disappear
- (void)viewWillDisappear:(BOOL)animated
{if ( [self.myWebView loading] ) {[self.myWebView stopLoading];}self.myWebView.delegate = nil; // disconnect the delegate as the webview is hidden[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

2.加载txt文件中文显示乱码问题

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeigth + 64)];webView.scalesPageToFit = YES;[self.view addSubview: webView];//将带有中文的URL进行UTF8编码NSString *urlString = [self.attachUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];///编码可以解决中文显示乱码问题NSStringEncoding *useEncodeing = nil;//带编码头的如utf-8等,这里会识别出来NSString *body = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.attachUrl] usedEncoding:useEncodeing error:nil];if ([_attachType isEqualToString:@"text/plain"] || [_attachType isEqualToString:@"application/msword"]) {//识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug。if (!body) {body = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:0x80000632 error:nil];}//还是识别不到,按GB18030编码再解码一次.if (!body) {body = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:0x80000631 error:nil];}//展现if (body) {[webView loadHTMLString:body baseURL: nil];}}else {NSURL *requestUrl = [NSURL URLWithString:urlString];NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl];[webView loadRequest:request];}

iOS UIWebView的基本用法相关推荐

  1. android webview ios uiwebview和wkwebview的交互以及本地缓存

    android webview js调用java的方式 1.java通过注解@JavascriptInterface导出方法, js通过window.别名.方法名调用方法 2.拦截url方式,通过sh ...

  2. IOS中NSUserDefaults的用法

    2019独角兽企业重金招聘Python工程师标准>>> IOS中NSUserDefaults的用法(轻量级本地数据存储) 分类: IOS开发 Object-C编程语言2012-09- ...

  3. IOS UIWebView用法

    转自猫猫小屋 IOS webview控件使用简介(一) IOS webview控件使用简介(二)–加载本地html 转载于:https://www.cnblogs.com/lairui1232000/ ...

  4. ios UIWebView调用本地html和javascript,并且和ios通讯

    ios和android都提供了有关webview和javascript通讯的功能,这就使开发者根据手机的系统展示适合手机的界面,是界面开发更加简单. 我的原型主要实现通过UIWebView展示本地的h ...

  5. html5长按保存,iOS UIWebView仿微信H5页面实现长按保存图片功能

    最终实现效果图 选择放这张效果图的时候很是忐忑啊,不知道会不会被和谐掉. 拿到需求之后分析了一下,其实主要功能点就是如何才能通过手指按压位置获取到相应的图片资源.是不是很抓狂,如果考虑到设备适配,谁知 ...

  6. iOS UIWebView URL拦截

    http://www.cocoachina.com/ios/20150626/12161.html 本文译者:candeladiao,原文:URL filtering for UIWebView on ...

  7. iOS UIWebView清除缓存

    为什么80%的码农都做不了架构师?>>>    使用iOS的UIWebView会自动进行缓存,我们在开发的时候要记得清除Cookie和缓存. 在webview的关闭按钮中添加两个方法 ...

  8. iOS开发宝典:String用法大全

    本文转载至 http://mobile.51cto.com/iphone-395171.htm 新手们还在等什么?这是一本属于你的iOS开发"字典",在这里你可以查到字符串.数组. ...

  9. ios 拦截html请求参数,iOS UIWebView URL拦截

    本文译者:candeladiao,原文:URL filtering for UIWebView on the iPhone 说明:译者在做app开发时,因为页面的javascript文件比较大导致加载 ...

  10. ios html5图片适配,ios UIWebView加载HTMLStr图文,关于图片宽高设置,webView内容实际高度的踩坑问题...

    一.关于UIWebView 与 WKWebView 选取问题 从发布时间看:javascript 2008年7月11日,在新一代iPhone3G正式发售当天,iPhone OS 2.0(iOS 2.0 ...

最新文章

  1. CodeForces 157A Game Outcome
  2. MySql分表、分库、分片和分区知识(转载)
  3. c语言调用oracle函数返回值吗,C语言通过值和引用函数
  4. c 文件操作_你电脑用久了,会有多少重复文件?快用它来整理一下吧
  5. 一文足以了解什么是 Java 中的锁
  6. python开发环境及网络基础
  7. MySQL Data目录查找并迁移到data文件夹中
  8. [Windows] 【强力推荐】可以将任何格式的文档免费转换为高质量PDF文件的软件,珍藏宝贝!!!
  9. ssh框架原理及工作流程
  10. 一寸照纯红色底图片_纯红色背景
  11. gps l1带宽_请问GPS带宽是多少?
  12. TM4C123GLaunchPad教程四_时钟配置
  13. 完全不懂SEO怎样入手做网站优化
  14. FPGA读取ADXL345
  15. AMD define函数
  16. Nexys2七段LED显示操作
  17. Linux C编程下没有 itoa()函数的问题
  18. liferay 7.0开发到部署
  19. python傻瓜瓜入门
  20. Photoshop 环境以人物素描效果

热门文章

  1. 概述HTTPS,简单了解对称性加密算法、非对称性加密算法方式
  2. HLA RTI(Run-time Infrastructure)
  3. goldendict for linux,GoldenDict(for Linux)配置无道词典
  4. 逻辑推理与判断(委派任务)
  5. UTC相关的时区转换
  6. linux脚本年龄计算,js+html实现周岁年龄计算器
  7. 蔚来、小鹏、理想自动驾驶能力的纵向演进与横向比较
  8. Git vs GitHub –什么是版本控制及其工作方式?
  9. 湍流公式推导系列——(一) 不可压湍动能方程的推导与含义
  10. 人民币对美元汇率中间价报6.7774元 下调109个基点