1.①像普通controller那样实现跳转到webview的效果,而不是直接加到当前controller
②隐藏webview的某些元素
③webview跳往原生app
④给webview添加进度条

解决方法如下:
①使用webview的基本步骤

NSURL *url = [NSURL URLWithString:self.urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request]; -(UIWebView *)webView{
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_webView.delegate = self;
[self.view addSubview:_webView];
}
return _webView;
}

但这样webview是直接加在当前controller的,要实现跳转效果,应当写个controller,里边再添加webview
跳转是这样的

MyWebViewController *web = [[MyWebViewController alloc]initWithUrl:urlGo];
[self.navigationController pushViewController:web animated:YES];

MyWebViewController是这样定义的
.h文件

@interface MyWebViewController : UIViewController<UIWebViewDelegate>

@property(nonatomic,copy) NSString *urlStr;
@end

.m内

@interface MyWebViewController ()

@property(nonatomic,strong) UIWebView *webView;

@end

@implementation MyWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"]; NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
} -(UIWebView *)webView{
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_webView.delegate = self;
[self.view addSubview:_webView];
}
return _webView;
}
@end

这样就能实现webview的跳转和基本使用

②隐藏webview的元素
这需要在webview的代理方法中实现,并且用js来操作。先在chrome上用开发者工具,获取要隐藏的元素的class,然后用

document.documentElement.getElementsByClassName('元素的class')[].style.display = 'none'
//网页加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//去除某个按钮
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName('ibar-toggle')[0].style.display = 'none'"]; //将controller标题改为webview标题
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
if (title.length) {
self.title = title;
}
}

③webview跳往原生app
这同样需要在webview的代理方法中实现

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

在需要跳往webview的地方返回YES,跳往原生app的地方返回NO,再添加跳原生app的方法。这需要拦截url内的字符串判断比较。如跳往商品详情页
http://xxx.com/wap/getItemDetail/146762587051761769.htm

//网页加载之前执行该方法
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *resuestStr = request.URL.absoluteString; if ([resuestStr rangeOfString:@"getItemDetail"].location != NSNotFound) {
NSArray *tempArr1 = [resuestStr componentsSeparatedByString:@"getItemDetail/"];
NSString *tempStr1 = [tempArr1 lastObject];
NSString *itemCode = [tempStr1 stringByReplacingOccurrencesOfString:@".htm" withString:@""]; ProductDetailViewController *vc = [[ProductDetailViewController alloc]initWithItemCode:itemCode];
[self.navigationController pushViewController:vc animated:YES]; return NO;
} return YES;
}

④给webview添加进度条
这需要用到第三方库NJKWebViewProgress,github地址为https://github.com/ninjinkun/NJKWebViewProgress
上边写的已经很清楚
要改变进度条的颜色,需要在NJKWebViewProgressView.m里configureViews方法修改

UIColor *tintColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];

2.修改user agent

修改user agent,主要是用来区分由哪个设备来访问wap页
这需要在初始化webview时处理,并用到registerDefaults方法。如在webview的controller中

-(UIWebView *)webView{
if (!_webView) {
_webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_webView.delegate = self;
[self.view addSubview:_webView];
}
return _webView;
}

初始化该controller时,应在如init的方法中添加

-(instancetype)init{
if (self = [super init]) {
// 往user-agent里增加SHP-APP字段
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
if ([secretAgent rangeOfString:@"SHP-APP"].location == NSNotFound) {
secretAgent = [NSString stringWithFormat:@"%@ SHP-APP",secretAgent]; NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:secretAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
}
return self;
}

UIWebView使用时的问题,包含修改user agent的更多相关文章

  1. time.h文件中包含的几个函数使用时须注意事项

    time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...

  2. 【WPF】当 ItemsSource 正在使用时操作无效。改用 ItemsControl.ItemsSource 访问和修改元素

    问题: 中文版报错:Additional information: 当 ItemsSource 正在使用时操作无效.改用 ItemsControl.ItemsSource 访问和修改元素. 英文版报错 ...

  3. ubuntu修改capslock键,单独使用为esc,组合使用时为ctrl+

    一.下面这部分可以将capslock与ctrl互换 将下面的代码放入-/.Xmodmap中, remove Lock = Caps_Lock remove Control = Control_L ke ...

  4. MySQL数据库使用时注意事项

    MySQL数据库使用时注意事项 建表的角度上 1.合理安排表关系 2.尽量把固定长度的字段放在前面 3.尽量使用char 代替varchar 4.分表:水平分和垂直分 在使用sql语句的时候 1.尽量 ...

  5. Android插件化(三):OpenAtlas的插件重建以及使用时安装

    Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html    核心提示:在上一篇博客 An ...

  6. EntityFrameWork 使用时碰到的小问题

    EntityFrameWork 使用时碰到的小问题 1,在使用orm访问数据库的相目里,也要引用EntityFrameWork.dll,否则无法使用orm 否则,编译错误 错误 5 "Sys ...

  7. MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”

    MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...

  8. Apache下开启SSI配置使html支持include包含

    写页面的同学通常会遇到这样的烦恼,就是页面上的 html 标签越来越多的时候,寻找指定的部分就会很困难,那么能不能像 javascript 一样写在不同的文件中引入呢?答案是有的,apache 能做到 ...

  9. [开发笔记]-sqlite数据库在使用时遇到的奇葩问题记录

    有时候做些简单的项目一般都会选择sqlite数据库,优点有很多,这里就不详细说了. 在此主要记录一些平时在使用时遇到的问题及解决方法.希望能对大家有所帮助. --------------------- ...

随机推荐

  1. 【linux】三十分钟学会AWK

    本文大部分内容翻译自我开始学习AWK时看到的一篇英文文章 AWK Tutorial ,觉得对AWK入门非常有帮助,所以对其进行了粗略的翻译,并对其中部分内容进行了删减或者补充,希望能为对AWK感兴趣的 ...

  2. NSIS使用记录

    ; 该脚本使用 HM VNISEdit 脚本编辑器向导产生 ; 安装程序初始定义常量 !define PRODUCT_NAME "" !define PRODUCT_VERSION ...

  3. HOG matlab练习

    matlab练习程序(HOG方向梯度直方图) HOG(Histogram of Oriented Gradient)方向梯度直方图,主要用来提取图像特征,最常用的是结合svm进行行人检测. 算法流程图 ...

  4. FreeMarker模板引擎

    现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...

  5. Web Api 上传图片,解决上传图片无格式

    制作这个功能时,找了很多资料,不过忘记了地址,所以就不一一放连接了, 直接上代码吧! 1. 首先新建一个上传的控制器 /// <summary> /// 上传 /// </summa ...

  6. 发送短信(string转换为JSON)

    using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.T ...

  7. sublime plugin emmet toolkit

    [简写规则]1. E 代表HTML标签.2. E#id 代表id属性.3. E.class 代表class属性.4. E[attr=foo] 代表某一个特定属性.5. E{foo} 代表标签包含的内容 ...

  8. css绝对定位问题

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. repeat语句

    一.repeat语句格式repeat语句用于"重复执行循环体,直到指定的条件为真时为止" repeat语句格式:repeat  语句1;  语句2;  --  语句n;until ...

  10. NBU AIX ORACLE10G RAC恢复到AIX单实例(表空间恢复)

    ln -s /usr/openv/netbackup/bin/libobk.a64 /oraclev3/product/10.2.0/lib/libobk.a不建软连接会报如下错误: using ta ...