首页 技术 正文
技术 2022年11月18日
0 收藏 532 点赞 4,804 浏览 3794 个字

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898