WKWebView - 概念介绍1

2018-11-12  本文已影响22人  edison0428

WKWebView是显示交互式web内容的对象,比如在应用程序浏览器中

iOS系统里WKWebView : UIView
macOS系统里WKWebView : UIView

概述

从iOS8.0 和 OS X 10.10开始,在app里用WKWebView去夹在web内容,不要再去用UIWebViewWebView

你可以用WKWebView类去嵌入web内容在app里,创建一个WKWebView对象,可以把这个对象看成一个View,然后发请求request去加载web内容

也可以创建一个POST请求,httpBody

创建一个WKWebView的使用

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myURL = URL(string:"https://www.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}

允许用户通过网页历史来回移动,用goBackgoForward去返回或者往前查看web内容,但是得先用canGoBackcanGoForward来判断当前是否有返回或者往前的web历史

你可以用setMagnification:centeredAtPoint:方法缩放webview通过手势,但是这只是`macOS的方法

#if !TARGET_OS_IPHONE
/* @abstract A Boolean value indicating whether magnify gestures will
 change the web view's magnification.
 @discussion It is possible to set the magnification property even if
 allowsMagnification is set to NO.
 The default value is NO.
 */
@property (nonatomic) BOOL allowsMagnification;

/* @abstract The factor by which the page content is currently scaled.
 @discussion The default value is 1.0.
 */
@property (nonatomic) CGFloat magnification;

/* @abstract Scales the page content by a specified factor and centers the
 result on a specified point.
 * @param magnification The factor by which to scale the content.
 * @param point The point (in view space) on which to center magnification.
 */
- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;

#endif
上一篇 下一篇

猜你喜欢

热点阅读