UIWebView 使用要注意的几点
UIWebView 使用要注意的几点
最近有客户希望将移动端统一使用HTML5来完成,在iOS端就要用到UIWebView。遇到了以下三个主要问题:
加载HTTPS页面
不像Safari可以弹出弹框问用户是否忽略证书,在UIWebView中只会得到一个空白页。由于UIWebView并没有提供HTTPS相关的接口,所以不能直接在UIWebView中进行操作。经过stackoverflow知道,原来可以先通过NSURLConnection通过HTTPS验证,然后再用UIWebView加载页面,这样就达到了想要的目的。
#pragma mark - Webview delegate - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated); if (!_authenticated) {
_authenticated = NO; _urlConnection = [[NSURLCoNnection alloc] initWithRequest:_request delegate:self]; [_urlConnection start]; return NO;
} return YES;
} #pragma mark - NURLConnection delegate - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection"); if ([challenge previousFailureCount] == 0)
{
_authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
} - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection"); // remake a webview call now that authentication has passed ok.
_authenticated = YES; [_web loadRequest:_request]; // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
} // We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
中文乱码
由于服务器端使用的编码是GBK,UIWebView默认的是UTF-8编码,导致加载时出现乱码。将UIWebView的loadRequest方法用loadData方法来代替,具体代码如下:
NSURL *url = [NSURL URLWithString:_address.text];
NSData *data = [NSData dataWithContentsOfURL:url];
[_web loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:url];
旋转自适应
当iOS设备从Portrait旋转为Landscape时,UIWebView的宽度只占屏幕的一半。发现是autoResize的相关参数没有进行设置,应该设置如下:
webview.scalesPageToFit = YES;
webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
又由于不同的Orientation,其缩放参数不一样,所以在旋转时应对scrollView的zoomScale进行设置,如下:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"willRotateToInterfaceOrientation");
CGFloat ratioAspect = _web.bounds.size.width/_web.bounds.size.height;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortraitUpsideDown:
case UIInterfaceOrientationPortrait:
// Going to Portrait mode
NSLog(@"Portrait mode");
for (UIScrollView *scroll in [_web subviews]) { //we get the scrollview
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:@selector(setZoomScale:)]){
scroll.minimumZoomScale = scroll.minimumZoomScale/ratioAspect;
scroll.maximumZoomScale = scroll.maximumZoomScale/ratioAspect;
[scroll setZoomScale:(scroll.zoomScale/ratioAspect) animated:YES];
}
}
break;
default:
// Going to Landscape mode
NSLog(@"Landscape mode");
for (UIScrollView *scroll in [_web subviews]) { //we get the scrollview
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:@selector(setZoomScale:)]){
scroll.minimumZoomScale = scroll.minimumZoomScale *ratioAspect;
scroll.maximumZoomScale = scroll.maximumZoomScale *ratioAspect;
[scroll setZoomScale:(scroll.zoomScale*ratioAspect) animated:YES];
}
}
break;
}
}
UIWebView 使用要注意的几点的更多相关文章
- AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking
AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...
- ios UIWebView自定义Alert风格的弹框
之前开发过一个App,因为公司之前写好了网页版的内容和安卓版本的App,我进去后老板要求我ios直接用网页的内容,而不需要自己再搭建框架.我一听,偷笑了,这不就是一个UIWebView吗?简单! 但是 ...
- iOS网络3—UIWebView与WKWebView使用详解
一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...
- IOS 网络浅析-(十二 UIWebView简介)
在这篇随笔里,我们只要知道UIWebView是什么就可以了. UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView ...
- UIWebView获取网页点击事件
//接收web事件 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request nav ...
- IOS UIWebView 下拉刷新功能的简单实现
1.运行效果图 2.swift 代码的实现 import UIKit class RefreshWebViewController: UIViewController,UIScrollViewDele ...
- UI控件(UIWebView)
本文主要记录UIWebView三方面内容: 1.基本的加载网页链接或文件: 2.网页js调用原生,也就是Cordova混合架构的原理: 3.原生调用js程序: 原生部分主要代码: @implement ...
- UIwebView 和 H5交互详情
背景: 最近公司准备上一个只有原生登录界面 + H5网页 ,并且支持ios7.0 以上系统的混合app;这可把我难住了,原生的UI界面我可以正写反写各种style把界面搭建起来.而要这个app的难点在 ...
- ios UIWebView 在开发中加载文件
UIWebView 在实际应用中加载文件的时候,有两种情况, 1. 实行在线预览 , 2. 下载到本地,再查看 如果是第一种情况: NSURL *url = [NSURL URLWithString: ...
- iOS UIWebView 拦截点击事件(双击缩放)
在平时的开发中,要使用到webview,但类似微信的webview在数据没有加载完成的时候 双击屏幕,webview不会缩放,其实实现这个功能很简单 代码是用swift写的 class YYSimpl ...
随机推荐
- 【转】ActiveMQ与虚拟通道
郑重提示,本文转载自http://shift-alt-ctrl.iteye.com/blog/2065436 ActiveMQ提供了虚拟通道的特性(Virtual Destination),它允许一个 ...
- iOS之文件解析
JSON JSON – OC 转换对照表 JSON OC {} NSDictonary [] NSArray "" NSString 数字 10,10.5 NSNumber NSJ ...
- 【Linux】zookeeper构造伪集群
1.在一台机器装安装3个zk server,构建伪集群模式安装步骤如下:1.下载zookeeper,下载地址:http://mirror.bit.edu.cn/apache/zookeeper/zoo ...
- 【转载】将python脚本打包成exe文件
exe文件也就是可以直接执行的文件.通常我们编好的带py后缀的脚本文件都是需要在有python的环境下执 行,每次通过Win + R打开运行窗口再输入powershell打开控制台,再千辛万苦地cd ...
- iOS自定义转场动画的实现
iOS中熟悉的是导航栏中的push和pop这两种动画效果,在这里我们可以自己实现自己想要的一些转场动画 下面是我自己创建转场动画的过程 1.新建一个文件继承自NSObject ,遵循协议UIViewC ...
- android Tweened Animations
Android提供了两种类型的动画: 一类是Tween动画:提供了旋转.移动.伸展和淡出等效果: 第二类是Frame-by-frame动画:这一类Animations可以创建一个Drawable序列, ...
- docker network
前言:前面的部分一直都是单机跑docker,但实际生产环境不可能只用一台来跑.肯定会用到多台,因为他们都是内部私有ip,那么多台主机之间的容器如何通信?这个是个很头疼的问题!目前主流几种方法如下: 1 ...
- ios NSComparator 三种枚举类型
NSComparator有3种枚举类型 NSOrderedDescending 降序,但是用他可以实现升序或者降序都没问题. NSOrderedAscending 升序,但是目前没有使用出任何效果.. ...
- Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
http://c.biancheng.net/cpp/shell/ 1.打印 2.运算符
- Chapter 2 Open Book——17
The rain stayed soft over the weekend, quiet, so I was able to sleep well. 这周末雨一直下的很柔很安静,所以我能睡的很好. P ...