美文网首页
网页的跳转

网页的跳转

作者: 陈水寒 | 来源:发表于2017-01-10 14:10 被阅读19次

跳转web界面有4种方式
1、 直接跳转Safari浏览器:有刷新、后退、进度条等;
2、 UIWebView:没有控制按钮,需要自己增加;
3、 UISafariViewController:在IOS9 中苹果增加了UISafariViewController,效果同Safari;
4、 WKWebView:在IOS8中 UIWebView的升级版;

UISafariViewController的使用

需要在控制器中导入头文件<SafariServices/SafariServices.h>,然后在需要跳转的代码中实现以下代码即可:

NSURL *url = [NSURL URLWithString:urlStr];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:urlStr];
// 官方推荐使用modal方式跳转,返回就不需要自己处理,如果使用push还需要自己添加代理处理返回按钮
[self presentViewController:safariVC animated:YES completion:nil];

效果如下:

safariViewController跳转效果.gif

WKWebView的使用:

  1. 在控制器中导入头文件<WebKit/WebKit.h>
  2. 在以下位置添加库文件


    图片.png

需要自己添加几个前进、后退、刷新按钮,然后通过KVO读取WKWebView相关属性信息即可,关键代码如下:

a) 在控制器中添加观察者,观察webview的键值变化

    WKWebView *webView = [[WKWebView alloc] init];
    _webView = webView;
    [self.contentView addSubview:webView];
    
    // 发送请求
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url];
    [webView loadRequest:request];
    
    /*
     KVO参数说明
     Observer:观察者
     KeyPath:观察webView哪个属性
     options:NSKeyValueObservingOptionNew 观察新值改变
     
     KVO注意点,一定要记得移除
     */
    [webView addObserver:self forKeyPath:@"canGoBack" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"canGoForward" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];

b) KVO事件处理

\\ 移除观察者
- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"canGoBack"];
    [self.webView removeObserver:self forKeyPath:@"canGoForward"];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeObserver:self forKeyPath:@"title"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    self.goBack.enabled = self.webView.canGoBack;// 后退按钮
    self.forword.enabled = self.webView.canGoForward;// 前进按钮
    self.progressView.progress = self.webView.estimatedProgress; // 进度条
    self.progressView.hidden = self.webView.estimatedProgress >= 1; // 加载完成后隐藏进度条
    self.title = self.webView.title; // 网页标题
}

c) 按钮点击时调用

- (IBAction)goBackClick:(id)sender {
    [self.webView goBack];
}
- (IBAction)forwordClick:(id)sender {
    [self.webView goForward];
}
- (IBAction)refreshClick:(id)sender {
    [self.webView reload];
}

最终效果如下:

WKWebView效果.gif

相关文章

  • 关于前端跳转到原生页面

    网页或者是移动app跳转都是内部跳转,即网页跳转网页或者是原生页面之间的跳转。之所以有前端和原生之间的交互,是因为...

  • 网页跳转

  • 网页的跳转

    跳转web界面有4种方式1、 直接跳转Safari浏览器:有刷新、后退、进度条等;2、 UIWebView:没有控...

  • js截取url字符串

    1.网页跳转 2.网页参数获取

  • vue中网页跳转进度条的实现🖖

    网页跳转时显示进度条,可以有效避免用户的焦躁心理。所以咱们有必要在网页跳转时加一个进度条 业务场景 网页跳转(可能...

  • C#如果通过反射获取私有字段的值?

    使用WebClient获取网页内容时,由于网页存在自动跳转,这个时候我已经不知道我需要获取到跳转网页的完整Url,...

  • html-图片/表格/表单

    a标签的认识 ’#‘号跳转页面内锚点跳转外部网页跳转内部网页 a标签的伪协议 img的认识 img是一个单标签 ...

  • 2019-02-27

    爬虫遇到的问题:关于网页需要跳转后才能访问的问题 有时候访问一些网页,显示网页需要等待5s才能跳转到所需要的网页:...

  • 链接分析算法总结

    两个模型 随机游走模型:网页节点通过链接进行跳转,对应跳转的概率 子集传播模型:网页划分子集,给予特殊子集内网页初...

  • 跳转网页的方法介绍

    1.为什么要跳转网页?点击UICollectionView的cell我们发现跳转到了网页 2.在以后开发中,怎么判...

网友评论

      本文标题:网页的跳转

      本文链接:https://www.haomeiwen.com/subject/uavkbttx.html