美文网首页iOS技术点
iOS-WKWebView的API注释

iOS-WKWebView的API注释

作者: 时米高的人生笔记 | 来源:发表于2017-05-02 11:26 被阅读102次

    前言:

    WKWebView属性介绍

    // 1.初始化webview时的配置属性
    @property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
    
    // 2.导航代理
    @property (nullable, nonatomic, weak) id <WKNavigationDelegate> navigationDelegate
    
    // 3.用户交互代理
    @property (nullable, nonatomic, weak) id <WKUIDelegate> UIDelegate;
    
    // 4.前进后退列表
    @property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;
    
    // 5.网页title, 可以用kvo监听
    @property (nullable, nonatomic, readonly, copy) NSString *title;
    
    // 6.请求的URL, 可以用kvo监听
    @property (nullable, nonatomic, readonly, copy) NSURL *URL;
    
    // 7.当前是否正在加载网页, 可以用kvo监听
    @property (nonatomic, readonly, getter=isLoading) BOOL loading;
    
    // 8.加载进度, 值范围0~1, 可以用kvo监听
    @property (nonatomic, readonly) double estimatedProgress;
    
    // 9.标识页面中所有的资源是否通过安全加密连接来加载, 可以用kvo监听
    @property (nonatomic, readonly) BOOL hasOnlySecureContent;
    
    // 10.当前导航的证书链, 可用kvo监听(iOS10.0新增api, 在这之前用的是certificateChain)
    @property (nonatomic, readonly, nullable) SecTrustRef serverTrust API_AVAILABLE(macosx(10.12), ios(10.0));
    
    // 11.是否可以返回上一页, 可用kvo监听
    @property (nonatomic, readonly) BOOL canGoBack;
    
    // 12.是否可以进入下一页, 可用kvo监听
    @property (nonatomic, readonly) BOOL canGoForward;
    
    // 13.是否支持左右的swipe手势前进或后退, 默认为NO
    @property (nonatomic) BOOL allowsBackForwardNavigationGestures;
    
    // 14.用户自定义的user agent, 没有则为nil
    @property (nullable, nonatomic, copy) NSString *customUserAgent API_AVAILABLE(macosx(10.11), ios(9.0));
    
    // 15.是否允许链接预览, 默认为YES
    @property (nonatomic) BOOL allowsLinkPreview API_AVAILABLE(macosx(10.11), ios(9.0));
    
    // 16.展示网页内容的scrollView
    @property (nonatomic, readonly, strong) UIScrollView *scrollView;
    
    // 17.是否允许放大网页(手势),默认为NO, NO的时候也可以设置magnification属性
    @property (nonatomic) BOOL allowsMagnification;
    
    // 18.放大引子,默认为1.0
    @property (nonatomic) CGFloat magnification;
    

    WKWebView方法介绍

    • 构造方法
    // 1. 指定构造器
    - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
    
    • 加载网页(文件)的方法
    // 1.通过request对象加载数据(加载网络上的网页)
    - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
    
    // 2. 根据fileurl加载本地文件
    @param URL  要操作的本地文件的路径
    @param readAccessURL 允许访问的url
    - (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(macosx(10.11), ios(9.0))
    
    // 3. 加载htmlString
    @param string 网页的内容
    @param baseURL 用于解决文档内的相对url
    - (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
    
    // 4. 加载data, 此方法可以加载大部分别的格式
    @param MIMEType  data的MIMET类型
    @param encodingName 数据的编码方式
    - (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macosx(10.11), ios(9.0));
    

    以上几种方法的使用案例

    // 1.加载网络地址
     [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    
    // 2. 根据fileUrl加载本地文件
     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()]];// NSTemporaryDirectory()是沙盒的tmp文件夹路径
    [self.webView loadFileURL:url allowingReadAccessToURL:url];
    
    // 3. 加载htmlString
    NSString *html = [NSString stringWithFormat:
    @"<html><head lang='en'><meta charset='UTF-8'></head><body>   
    <div style='margin-top: 100px'><h1>图片显示测试</h1><p style='width:580px;height: 539px;background: blue' onclick='picCallback()'>[站外图片上传中……(2)]</p><input type='button' value='CallCamera' onclick='OCModel.showAlertMsg(1,2)'></div>
    <script type='text/javascript'>var picCallback = function(photos) {alert('photos');}var shareCallback = function(){alert('success');}</script></body></html>"];
    // 用到的图片资源所在的路径
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",NSTemporaryDirectory()]];
    [self.webView loadHTMLString:html baseURL:url];
    
    // 4. 加载data
    NSString *path = [NSString stringWithFormat:@"%@test.html",NSTemporaryDirectory()];
    NSData *Data = [NSData dataWithContentsOfFile:path];
    [self.webView loadData:Data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
    

    WKWebView的其他方法

    // 1. 前进或后退到某页
    @param item 到达的那一页, 必须在webView的前进后退列表中
    @result 如果已经是当前页或者不在前进后退列表, 返回是nil
    - (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;
    
    // 2. 返回上一页, 如果不能返回, 则什么都不做
    - (nullable WKNavigation *)goBack;
    
    // 3. 前进一页, 如果不能前进, 则什么都不做
    - (nullable WKNavigation *)goForward;
    
    // 4. 重新加载当前页面
    - (nullable WKNavigation *)reload;
    
    // 5. 从原始url 重新加载当前页面
    - (nullable WKNavigation *)reloadFromOrigin;
    
    // 6. 停止加载当前页面的所有数据
    - (void)stopLoading;
    
    // 7. 执行JS代码
    @param javaScriptString 要执行的JS语句
    @param completionHandler 脚本执行成功或失败的回调
    - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
    
    // 8. 根据设置的缩放因子来缩放页面, 并居中显示结果在指定的点
    - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
    

    参考文章:

    相关文章

      网友评论

      本文标题:iOS-WKWebView的API注释

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