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 使用要注意的几点的更多相关文章

  1. AFNetworking 3.0 源码解读(十一)之 UIButton/UIProgressView/UIWebView + AFNetworking

    AFNetworking的源码解读马上就结束了,这一篇应该算是倒数第二篇,下一篇会是对AFNetworking中的技术点进行总结. 前言 上一篇我们总结了 UIActivityIndicatorVie ...

  2. ios UIWebView自定义Alert风格的弹框

    之前开发过一个App,因为公司之前写好了网页版的内容和安卓版本的App,我进去后老板要求我ios直接用网页的内容,而不需要自己再搭建框架.我一听,偷笑了,这不就是一个UIWebView吗?简单! 但是 ...

  3. iOS网络3—UIWebView与WKWebView使用详解

    一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...

  4. IOS 网络浅析-(十二 UIWebView简介)

    在这篇随笔里,我们只要知道UIWebView是什么就可以了. UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView ...

  5. UIWebView获取网页点击事件

    //接收web事件 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request nav ...

  6. IOS UIWebView 下拉刷新功能的简单实现

    1.运行效果图 2.swift 代码的实现 import UIKit class RefreshWebViewController: UIViewController,UIScrollViewDele ...

  7. UI控件(UIWebView)

    本文主要记录UIWebView三方面内容: 1.基本的加载网页链接或文件: 2.网页js调用原生,也就是Cordova混合架构的原理: 3.原生调用js程序: 原生部分主要代码: @implement ...

  8. UIwebView 和 H5交互详情

    背景: 最近公司准备上一个只有原生登录界面 + H5网页 ,并且支持ios7.0 以上系统的混合app;这可把我难住了,原生的UI界面我可以正写反写各种style把界面搭建起来.而要这个app的难点在 ...

  9. ios UIWebView 在开发中加载文件

    UIWebView 在实际应用中加载文件的时候,有两种情况, 1. 实行在线预览 , 2. 下载到本地,再查看 如果是第一种情况: NSURL *url = [NSURL URLWithString: ...

  10. iOS UIWebView 拦截点击事件(双击缩放)

    在平时的开发中,要使用到webview,但类似微信的webview在数据没有加载完成的时候 双击屏幕,webview不会缩放,其实实现这个功能很简单 代码是用swift写的 class YYSimpl ...

随机推荐

  1. org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别

    相信很多java程序员在写代码的时候遇到判断某字符串是否为空的时候会用到StringUtils类中isBlank和isEmpty方法,这两个方法到底有什么区别呢?我们用一段代码来阐述这个区别吧: @T ...

  2. MVC3+EF4.1学习系列(一)-------创建EF4.1 code first的第一个实例

    基于EF4.1 code first 简单的CRUD  园子中已经有很多了 ~~ 真不想再写这个了 可是为了做一个完整的小demo 从开始 到后面的一些简单重构 还是决定认真把这个写出来 争取写些别人 ...

  3. Openjudge-计算概论(A)-年龄与疾病

    描述: 某医院想统计一下某项疾病的获得与否与年龄是否有关,需要对以前的诊断记录进行整理. 输入共2行,第一行为过往病人的数目n(0 < n <= 100),第二行为每个病人患病时的年龄.输 ...

  4. iOS 面试大全从简单到复杂(简单篇)

    1.UIWindow和UIView和 CALayer 的联系和区别? 答:UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应 ...

  5. css格式布局

    一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例 : 二.position:absolute 1.外层没有position:absolute(或 ...

  6. 关于dubbo服务产生异常之:Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer.

    简单来说就是dubbo超时,因为dubbo默认的时间是500ms,超过这个时间它会重新访问service层,最多尝试三次. 所以我在测试的时候日志显示出来的异常为……timeout……. 开始设置开始 ...

  7. 关于oracle数据库(10)函数

    分析函数,用于统计排名 语法:函数名() over(order by 排序字段 asc | desc) row_number() 无论值是否相等,生成连续的行号 -- 1,2,3,4, select ...

  8. MySQL 多表查询分页

    SELECT v.*, vt.id vid, vt.vote_id, vt. option, vt.poll FROM vote v JOIN vote_option vt ON v.id = vt. ...

  9. 3g 上网卡使用

    控制面板\网络和 Internet\网络连接每个USB孔都会分配网路名 --cisco vpn 连接问题解决 启动(以管理员身份运行) sc stop SharedAccess ---完美解决win7 ...

  10. 三、oracle 用户管理(user)

    一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码;  ...