• 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];
    }

     

     

  • 相关阅读:
    CBR(基于案例的推理)的产生和Roger Schank=Schank教授在他的著作中提出了以“记忆组织包"
    php 设计模式
    php 常用资源
    自然语言处理著作或期刊名称2
    北京师范大学语言学及应用语言学研究生培养方案
    !!! Analysis & Design 很好的汇总+zcl的 UML 体会
    睡眠的方法
    !!!【php100教程】
    机器翻译和自然语言信息处理专业硕士研究生培养方案
    愧薪
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/LoadWebPage.html
Copyright © 2020-2023  润新知