WKWebView
是显示交互式web内容的对象,比如在应用程序浏览器中
在iOS
系统里WKWebView : UIView
在macOS
系统里WKWebView : UIView
概述
从iOS8.0 和 OS X 10.10开始,在app里用
WKWebView
去夹在web内容,不要再去用UIWebView
和WebView
你可以用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)
}}
允许用户通过网页历史来回移动,用goBack
和goForward
去返回或者往前查看web内容,但是得先用canGoBack
和canGoForward
来判断当前是否有返回或者往前的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
网友评论