zoukankan      html  css  js  c++  java
  • iOS加载网页的方式-4种

    ①Safari openURL

    自带很多功能(进度条,刷新,前进,倒退等等功能),必须要跳出当前应用

    ②UIWebView

    UIWebView (没有功能) ,在当前应用打开网页,并且有safari,自己实现,UIWebView不能实现进度条

    ③WKWebView

    WKWebView:iOS8 (UIWebView升级版本,添加功能 1.监听进度 2.缓存)

     1.导入

    #import <WebKit/WebKit.h>

    代码:

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface XBWebViewController : UIViewController
    /** 链接URL */
    @property (nonatomic, strong) NSURL * url ;
    @end
    
    NS_ASSUME_NONNULL_END
    #import "XBWebViewController.h"
    #import <WebKit/WebKit.h>
    @interface XBWebViewController ()
    @property (weak, nonatomic) IBOutlet UIView *contentView;
    /** WKWebView */
    @property (nonatomic, strong) WKWebView * webView ;
    /** 返回按钮 */
    @property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
    /** 向前按钮 */
    @property (weak, nonatomic) IBOutlet UIBarButtonItem *forwardItem;
    /** 进度条 */
    @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
    
    @end
    
    @implementation XBWebViewController
    - (IBAction)goBack:(id)sender {
        [self.webView goBack];
    }
    - (IBAction)goForward:(id)sender {
        [self.webView goForward];
    }
    - (IBAction)reload:(id)sender {
        [self.webView reload];
    }
    
    #pragma mark - 生命周期方法
    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        _webView.frame = self.contentView.bounds;
    }
    
    -(void)awakeFromNib {
        [super awakeFromNib];
        self.backItem.image = [self.backItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.forwardItem.image = [UIImage noRenderingImageName:@"Yellow_3D_arrow_right"];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setupUI];
    }
    - (void)setupUI {
        // 添加webView
        WKWebView *webView = [[WKWebView alloc] init];
        _webView = webView;
        [self.contentView addSubview:webView];
        
        // 展示网页
        NSURLRequest *request = [NSURLRequest requestWithURL:_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:@"title" options:NSKeyValueObservingOptionNew context:nil];
        
        // 进度条
        [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    }
    // 只要观察对象属性有新值就会调用
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
    {
        self.backItem.enabled = self.webView.canGoBack;
        self.forwardItem.enabled = self.webView.canGoForward;
        self.title = self.webView.title;
        self.progressView.progress = self.webView.estimatedProgress;
        self.progressView.hidden = self.webView.estimatedProgress >= 1;
    }
    
    #pragma mark - 对象被销毁
    - (void)dealloc
    {
        [self.webView removeObserver:self forKeyPath:@"canGoBack"];
        [self.webView removeObserver:self forKeyPath:@"title"];
        [self.webView removeObserver:self forKeyPath:@"canGoForward"];
        [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    }
    
    @end

    ④Safari浏览器(SFSafariViewController)

    SFSafariViewController:专门用来展示网页 需求:即想要在当前应用展示网页,又想要safari功能 iOS9才能使用

     1.导入#import <SafariServices/SafariServices.h>

    #import <SafariServices/SafariServices.h>
    @interface UIViewController ()<SFSafariViewControllerDelegate>
    @end
    
    // SFSafariViewController使用Modal(使用Present方式模态跳转)
    SFSafariViewController *safariVc = [[SFSafariViewController alloc] initWithURL:url];
    //safariVc.delegate = self;
    //self.navigationController.navigationBarHidden = YES;
    //[self.navigationController pushViewController:safariVc animated:YES];
    [self presentViewController:safariVc animated:YES completion:nil];
    #pragma mark - SFSafariViewControllerDelegate
    - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller
    {
        [self.navigationController popViewControllerAnimated:YES];
    }

     

     

  • 相关阅读:
    e-icon-picker 基于element-ui图标和fontawesome图标选择器组件
    js 前端将平级数据转为树形数据的方法
    发送邮件报User does not have send-as privilege for错误的解决办法
    Dynamics 365利用email实体的DeliverIncomingEmail来做抓取邮件的进一步处理
    Dynamics 365中邮件模板的使用
    导入解决方案报错:Unable to retrieve customActivityInfo using RetrieveCustomActivityInfoWithSandboxPlugin
    Dynamics 365组织服务使用Query Expression查询数据时候请谨慎使用ConditionOperator.Contains
    【代码审计】ESPCMSP8(易思企业建站管理系统)漏洞报告
    MS16-072域内中间人攻击
    域控权限提升PTH攻击
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/LoadWebPage.html
Copyright © 2011-2022 走看看