日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

WKWebView與JS交互實(shí)戰(zhàn)技巧之API介紹

 Han_Count 2016-09-06

前言

前一章我給大家介紹了iOS與HTML5的交互,用的是UIWebView,今天給大家介紹另外一種基于 iOS 8 新推出的 WKWebView 組件,構(gòu)建出自己的混合開(kāi)發(fā)框架。

WKWebView 簡(jiǎn)介

WKWebView 是蘋(píng)果在 iOS 8 中引入的新組件,目的是給出一個(gè)新的高性能的 Web View 解決方案,擺脫過(guò)去 UIWebView 的老舊笨重特別是內(nèi)存占用量巨大的問(wèn)題。

蘋(píng)果將 UIWebViewDelegate 與 UIWebView 重構(gòu)成了 14 個(gè)類和 3 個(gè)協(xié)議,引入了不少新的功能和接口,這可以在一定程度上看做蘋(píng)果對(duì)其封鎖 Web View 內(nèi)核的行為作出的補(bǔ)償:既然你們都說(shuō) UIWebView 太渣,那我就造一個(gè)不渣的給你們用唄~~ 眾所周知,連 Chrome 的 iOS 版用的也是 UIWebView 的內(nèi)核。

WKWebView 有哪些進(jìn)步呢?

1.將瀏覽器內(nèi)核渲染進(jìn)程提取出 App,由系統(tǒng)進(jìn)行統(tǒng)一管理,這減少了相當(dāng)一部分的性能損失。

2.js 可以直接使用已經(jīng)事先注入 js runtime 的 js 接口給 Native 層傳值,不必再通過(guò)苦逼的 iframe 制造頁(yè)面刷新再解析自定義協(xié)議的奇怪方式。

3.支持高達(dá) 60 fps 的滾動(dòng)刷新率,內(nèi)置了手勢(shì)探測(cè)。

WKWebView API介紹

WKWebView的頭文件聲明

// webview 配置,具體看下面
@property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;

// 導(dǎo)航代理 
@property (nullable, nonatomic, weak) id <WKNavigationDelegate> navigationDelegate;

// 用戶交互代理
@property (nullable, nonatomic, weak) id <WKUIDelegate> UIDelegate;

// 頁(yè)面前進(jìn)、后退列表
@property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;

// 默認(rèn)構(gòu)造器
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;

// 已不再使用
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;

// 與UIWebView一樣的加載請(qǐng)求API
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;

// 加載URL
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL NS_AVAILABLE(10_11, 9_0);

// 直接加載HTML
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;

// 直接加載data
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL NS_AVAILABLE(10_11, 9_0);

// 前進(jìn)或者后退到某一頁(yè)面
- (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;

// 頁(yè)面的標(biāo)題,這昆支持KVO的
@property (nullable, nonatomic, readonly, copy) NSString *title;

// 當(dāng)前請(qǐng)求的URL,它是支持KVO的
@property (nullable, nonatomic, readonly, copy) NSURL *URL;

// 標(biāo)識(shí)當(dāng)前是否正在加載內(nèi)容中,它是支持KVO的
@property (nonatomic, readonly, getter=isLoading) BOOL loading;

// 當(dāng)前加載的進(jìn)度,范圍為[0, 1]
@property (nonatomic, readonly) double estimatedProgress;

// 標(biāo)識(shí)頁(yè)面中的所有資源是否通過(guò)安全加密連接來(lái)加載,它是支持KVO的
@property (nonatomic, readonly) BOOL hasOnlySecureContent;

// 當(dāng)前導(dǎo)航的證書(shū)鏈,支持KVO
@property (nonatomic, readonly, copy) NSArray *certificateChain NS_AVAILABLE(10_11, 9_0);

// 是否可以招待goback操作,它是支持KVO的
@property (nonatomic, readonly) BOOL canGoBack;

// 是否可以執(zhí)行g(shù)ofarward操作,支持KVO
@property (nonatomic, readonly) BOOL canGoForward;

// 返回上一頁(yè)面,如果不能返回,則什么也不干
- (nullable WKNavigation *)goBack;

// 進(jìn)入下一頁(yè)面,如果不能前進(jìn),則什么也不干
- (nullable WKNavigation *)goForward;

// 重新載入頁(yè)面
- (nullable WKNavigation *)reload;

// 重新從原始URL載入
- (nullable WKNavigation *)reloadFromOrigin;

// 停止加載數(shù)據(jù)
- (void)stopLoading;

// 執(zhí)行JS代碼
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;

// 標(biāo)識(shí)是否支持左、右swipe手勢(shì)是否可以前進(jìn)、后退
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;

// 自定義user agent,如果沒(méi)有則為nil
@property (nullable, nonatomic, copy) NSString *customUserAgent NS_AVAILABLE(10_11, 9_0);

// 在iOS上默認(rèn)為NO,標(biāo)識(shí)不允許鏈接預(yù)覽
@property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE(10_11, 9_0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

WKWebViewConfiguration配置

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  • 1
  • 1

WKPreferences偏好設(shè)置

// 設(shè)置偏好設(shè)置
config.preferences = [[WKPreferences alloc] init];
// 默認(rèn)為0
config.preferences.minimumFontSize = 10;
// 默認(rèn)認(rèn)為YES
config.preferences.javaScriptEnabled = YES;
// 在iOS上默認(rèn)為NO,表示不能自動(dòng)通過(guò)窗口打開(kāi)
config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

WKProcessPool內(nèi)容處理池

WKProcessPool并沒(méi)有公開(kāi)任何的屬性或者方法,不需要配置:

config.processPool = [[WKProcessPool alloc] init];
  • 1
  • 1

WKUserContentController內(nèi)容交互控制器

我們要通過(guò)JS與webview內(nèi)容交互,就需要到這個(gè)類了,它的所有屬性及方法說(shuō)明如下:

// 只讀屬性,所有添加的WKUserScript都在這里可以獲取到
@property (nonatomic, readonly, copy) NSArray<WKUserScript *> *userScripts;

// 注入JS
- (void)addUserScript:(WKUserScript *)userScript;

// 移除所有注入的JS
- (void)removeAllUserScripts;

// 添加scriptMessageHandler到所有的frames中,則都可以通過(guò)
// window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
// 發(fā)送消息
// 比如,JS要調(diào)用我們?cè)姆椒ǎ涂梢酝ㄟ^(guò)這種方式了
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;

// 根據(jù)name移除所注入的scriptMessageHandler
- (void)removeScriptMessageHandlerForName:(NSString *)name;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

WKUserScript

在WKUserContentController中,所有使用到WKUserScript。WKUserContentController是用于與JS交互的類,而所注入的JS是WKUserScript對(duì)象。它的所有屬性和方法如下:

// JS源代碼
@property (nonatomic, readonly, copy) NSString *source;

// JS注入時(shí)間
@property (nonatomic, readonly) WKUserScriptInjectionTime injectionTime;

// 只讀屬性,表示JS是否應(yīng)該注入到所有的frames中還是只有main frame.
@property (nonatomic, readonly, getter=isForMainFrameOnly) BOOL forMainFrameOnly;

// 初始化方法,用于創(chuàng)建WKUserScript對(duì)象
// source:JS源代碼
// injectionTime:JS注入的時(shí)間
// forMainFrameOnly:是否只注入main frame
- (instancetype)initWithSource:(NSString *)source injectionTime:(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

WKUserScriptInjectionTime

它是一個(gè)枚舉類型,只有在文檔開(kāi)始加載時(shí)注入和加載結(jié)束時(shí)注入。

typedef NS_ENUM(NSInteger, WKUserScriptInjectionTime) {
    WKUserScriptInjectionTimeAtDocumentStart,
    WKUserScriptInjectionTimeAtDocumentEnd
} NS_ENUM_AVAILABLE(10_10, 8_0);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

WKWebsiteDataStore存儲(chǔ)的Web內(nèi)容

iOS9.0以后才能使用這個(gè)類。它是代表webview不同的數(shù)據(jù)類型,包括cookies、disk、memory caches、WebSQL、IndexedDB數(shù)據(jù)庫(kù)和本地存儲(chǔ)。
從這里看,要優(yōu)化Webview好像可以通過(guò)它來(lái)實(shí)現(xiàn),不過(guò)要求iOS9.0以上才能使用?,F(xiàn)在6.0都沒(méi)有拋棄的我,從來(lái)不能考慮這種最新的。
WKProcessPool并沒(méi)有公開(kāi)任何的屬性或者方法,不需要配置:

// 默認(rèn)數(shù)據(jù)存儲(chǔ)
+ (WKWebsiteDataStore *)defaultDataStore;

// 返回非持久化存儲(chǔ),數(shù)據(jù)不會(huì)寫(xiě)入文件系統(tǒng)
+ (WKWebsiteDataStore *)nonPersistentDataStore;

// 已經(jīng)不可用
- (instancetype)init NS_UNAVAILABLE;

// 只讀屬性,表示是否是持久化存儲(chǔ)
@property (nonatomic, readonly, getter=isPersistent) BOOL persistent;

// 獲取所有web內(nèi)容的數(shù)據(jù)存儲(chǔ)類型集,比如cookies、disk等
+ (NSSet<NSString *> *)allWebsiteDataTypes;

// 獲取某些指定數(shù)據(jù)存儲(chǔ)類型的數(shù)據(jù)
- (void)fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler;

// 刪除某些指定類型的數(shù)據(jù)
- (void)removeDataOfTypes:(NSSet<NSString *> *)dataTypes forDataRecords:(NSArray<WKWebsiteDataRecord *> *)dataRecords completionHandler:(void (^)(void))completionHandler;

// 刪除某些指定類型的數(shù)據(jù)且修改日期是指定的日期
- (void)removeDataOfTypes:(NSSet<NSString *> *)websiteDataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

所有的dataTypes是下面這些系統(tǒng)所定義的:

WK_EXTERN NSString * const WKWebsiteDataTypeDiskCache NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeMemoryCache NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeOfflineWebApplicationCache NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeCookies NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeSessionStorage NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeLocalStorage NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeWebSQLDatabases NS_AVAILABLE(10_11, 9_0);

WK_EXTERN NSString * const WKWebsiteDataTypeIndexedDBDatabases NS_AVAILABLE(10_11, 9_0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

WKWebsiteDataRecord

iOS9.0以后才可用。
website的數(shù)據(jù)存儲(chǔ)記錄類型,它只有兩個(gè)屬性:

// 通常是域名
@property (nonatomic, readonly, copy) NSString *displayName;

// 存儲(chǔ)的數(shù)據(jù)類型集
@property (nonatomic, readonly, copy) NSSet<NSString *> *dataTypes;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

WKSelectionGranularity選擇粒度

它表示在webview上選擇內(nèi)容的粒度,只有下面這兩種類型:

typedef NS_ENUM(NSInteger, WKSelectionGranularity) {
    WKSelectionGranularityDynamic,
    WKSelectionGranularityCharacter,
} NS_ENUM_AVAILABLE_IOS(8_0);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

它是用于webview內(nèi)容交互時(shí)選擇內(nèi)容的粒度類型設(shè)置。比如說(shuō),當(dāng)使用WKSelectionGranularityDynamic時(shí),而所選擇的內(nèi)容是單個(gè)塊,這時(shí)候granularity可能會(huì)是單個(gè)字符;當(dāng)所選擇的web內(nèi)容不限制于某個(gè)塊時(shí),granularity可能會(huì)是單個(gè)塊。

WKNavigationDelegate

@protocol WKNavigationDelegate <NSObject>

@optional

// 決定導(dǎo)航的動(dòng)作,通常用于處理跨域的鏈接能否導(dǎo)航。WebKit對(duì)跨域進(jìn)行了安全檢查限制,不允許跨域,因此我們要對(duì)不能跨域的鏈接
// 單獨(dú)處理。但是,對(duì)于Safari是允許跨域的,不用這么處理。
// 這個(gè)是決定是否Request
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

// 決定是否接收響應(yīng)
// 這個(gè)是決定是否接收response
// 要獲取response,通過(guò)WKNavigationResponse對(duì)象獲取
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;

// 當(dāng)main frame的導(dǎo)航開(kāi)始請(qǐng)求時(shí),會(huì)調(diào)用此方法
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;

// 當(dāng)main frame接收到服務(wù)重定向時(shí),會(huì)回調(diào)此方法
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;

// 當(dāng)main frame開(kāi)始加載數(shù)據(jù)失敗時(shí),會(huì)回調(diào)
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;

// 當(dāng)main frame的web內(nèi)容開(kāi)始到達(dá)時(shí),會(huì)回調(diào)
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;

// 當(dāng)main frame導(dǎo)航完成時(shí),會(huì)回調(diào)
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;

// 當(dāng)main frame最后下載數(shù)據(jù)失敗時(shí),會(huì)回調(diào)
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;

// 這與用于授權(quán)驗(yàn)證的API,與AFN、UIWebView的授權(quán)驗(yàn)證API是一樣的
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;

// 當(dāng)web content處理完成時(shí),會(huì)回調(diào)
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

WKNavigationActionPolicy

導(dǎo)航動(dòng)作決定策略:

typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
    WKNavigationActionPolicyCancel,
    WKNavigationActionPolicyAllow,
} NS_ENUM_AVAILABLE(10_10, 8_0);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

它是枚舉類型,只有Cancel和Allow這兩種。設(shè)置為Cancel就是不允許導(dǎo)航,就不會(huì)跳轉(zhuǎn)鏈接。

WKNavigationResponse

WKNavigationResponse是導(dǎo)航響應(yīng)類,通過(guò)它可以獲取相關(guān)響應(yīng)的信息:

NS_CLASS_AVAILABLE(10_10, 8_0)
@interface WKNavigationResponse : NSObject

// 是否是main frame
@property (nonatomic, readonly, getter=isForMainFrame) BOOL forMainFrame;

// 獲取響應(yīng)response
@property (nonatomic, readonly, copy) NSURLResponse *response;

// 是否顯示MIMEType
@property (nonatomic, readonly) BOOL canShowMIMEType;

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

只有接收響應(yīng)與不接收響應(yīng)兩種。

WKNavigationAction

WKNavigationAction對(duì)象包含關(guān)于導(dǎo)航的action的信息,用于make policy decisions。它只有以下幾個(gè)屬性:

// 正在請(qǐng)求的導(dǎo)航的frame
@property (nonatomic, readonly, copy) WKFrameInfo *sourceFrame;

// 目標(biāo)frame,如果這是新的window,它會(huì)是nil
@property (nullable, nonatomic, readonly, copy) WKFrameInfo *targetFrame;

// 導(dǎo)航類型,如下面的小標(biāo)題WKNavigationType
@property (nonatomic, readonly) WKNavigationType navigationType;

// 導(dǎo)航的請(qǐng)求
@property (nonatomic, readonly, copy) NSURLRequest *request;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

WKNavigationType

WKNavigationType類型是枚舉類型,它的可選值如下:

typedef NS_ENUM(NSInteger, WKNavigationType) {
// 鏈接已經(jīng)點(diǎn)擊
    WKNavigationTypeLinkActivated,
    // 表單提交
    WKNavigationTypeFormSubmitted,
    // 前進(jìn)、后退
    WKNavigationTypeBackForward,
    // 重新載入
    WKNavigationTypeReload,
    // 表單重新提交
    WKNavigationTypeFormResubmitted,
    // 其它
    WKNavigationTypeOther = -1,
} NS_ENUM_AVAILABLE(10_10, 8_0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

WKUIDelegate

@protocol WKUIDelegate <NSObject>

@optional

// 創(chuàng)建新的webview
// 可以指定配置對(duì)象、導(dǎo)航動(dòng)作對(duì)象、window特性
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;

// webview關(guān)閉時(shí)回調(diào)
- (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);

// 調(diào)用JS的alert()方法
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;

// 調(diào)用JS的confirm()方法
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;

// 調(diào)用JS的prompt()方法
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler;

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

WKBackForwardList

WKBackForwardList表示webview中可以前進(jìn)或者后退的頁(yè)面列表。其聲明如下:

NS_CLASS_AVAILABLE(10_10, 8_0)
@interface WKBackForwardList : NSObject

// 當(dāng)前正在顯示的item(頁(yè)面)
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *currentItem;

// 后一頁(yè),如果沒(méi)有就是nil
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *backItem;

// 前一頁(yè),如果沒(méi)有就是nil
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *forwardItem;

// 根據(jù)下標(biāo)獲取某一個(gè)頁(yè)面的item
- (nullable WKBackForwardListItem *)itemAtIndex:(NSInteger)index;

// 可以進(jìn)行g(shù)oback操作的頁(yè)面列表
@property (nonatomic, readonly, copy) NSArray<WKBackForwardListItem *> *backList;

// 可以進(jìn)行g(shù)oforward操作的頁(yè)面列表
@property (nonatomic, readonly, copy) NSArray *forwardList;

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

WKBackForwardListItem

頁(yè)面導(dǎo)航前進(jìn)、后退列表項(xiàng):

@interface WKBackForwardListItem : NSObject

// 該頁(yè)面的URL
@property (readonly, copy) NSURL *URL;

// 該頁(yè)面的title
@property (nullable, readonly, copy) NSString *title;

// 初始請(qǐng)求該item的請(qǐng)求的URL
@property (readonly, copy) NSURL *initialURL;

@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

結(jié)束

這樣這個(gè)框架基本介紹完了,實(shí)戰(zhàn)下一篇會(huì)給大家著重介紹如何使用.

下一篇實(shí)戰(zhàn)開(kāi)發(fā):
http://blog.csdn.net/baihuaxiu123/article/details/51286954

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多