iOS8 WKWebView(一) API詳解及功能介紹
1、 只讀的配置屬性 WKWebViewConfiguration
2、 導航代理 navigationDelegate
3、 使用者互動代理UIDelegate
4、 前進後退列表 backForwardList
5、 網頁標題可以用kvo監聽title
6、 請求的url 可用kvo監聽 url
7、 當前是否正在載入網頁可用kvo監聽 loading
8、 載入進度範圍0~1可用kvo監聽 estimatedProgress
9、 標識頁面中的所有資源是否通過安全加密連線來載入,可用kvo監聽hasOnlySecureContent
10、當前導航的證書鏈 可用kvo監聽 certificateChain
11、是否可以返回上一頁可用kvo監聽canGoBack
12、是否可以進入下一頁可用kvo監聽 canGoForward
13、是否支援左右的swipe手勢是否可以前進、後退allowsBackForwardNavigationGestures
14、使用者自定義的user agent 沒有則為nil customUserAgent
15、在iOS上預設為NO,標識不允許連結預覽allowsLinkPreview
16、展示網頁內容的scrollview
17、是否可以放大預設為no allowsMagnification
18、放大因子預設1 magnification
三、WKWebView 方法:
1、 預設構造器
-(instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration*)configuration NS_DESIGNATED_INITIALIZER;
2、構造器<棄用>
-(instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
3、通過request物件載入資料
-(nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
4、通過url載入 iOS9+啟用
-(nullable WKNavigation *)loadFileURL:(NSURL *)URLallowingReadAccessToURL:(NSURL *)readAccessURL NS_AVAILABLE(10_11, 9_0);
5、直接載入HTML
-(nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullableNSURL *)baseURL;
6、直接載入data
-(nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMETypecharacterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL*)baseURL NS_AVAILABLE(10_11, 9_0);
7、前進或者後退到某頁
-(nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;
8、返回上一頁 如果不能返回則什麼都不做
-(nullable WKNavigation *)goBack;
9、前進一頁 如果不能前進則什麼都不做
-(nullable WKNavigation *)goForward;
10、重新載入頁面
-(nullable WKNavigation *)reload;
11、重新載入原始URL
-(nullable WKNavigation *)reloadFromOrigin;
12、停止載入
-(void)stopLoading;
13、執行JS程式碼
-(void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void(^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
14、根據設定的縮放因子來縮放頁面,並居中顯示結果在指定的點
-(void)setMagnification:(CGFloat)magnificationcenteredAtPoint:(CGPoint)point;
四、WKWebViewConfiguration 簡介:
1、內容處理池
內容處理池WKProcessPool並沒有公開任何的屬性或者方法,不需要配置:
WKProcessPool 繼承自NSObject 無其他屬性與方法
@property (nonatomic, strong) WKProcessPool *processPool;
2、WKPreferences WebView偏好配置
webView的偏好設定 preferences物件關聯到一個webView是由其指定webView的配置
WKPreferences繼承自 NSObject
WKPreferences:
屬性
minimumFontSize 最小點的字型尺寸 預設0
javaScriptEnabled 指示是否啟用JavaScript 預設YES
javaScriptCanOpenWindowsAutomatically 在iOS上預設為NO,JavaScript能否自動通過視窗開啟
javaEnabled 是否啟用Java 預設為NO
plugInsEnabled 是否啟用外掛 預設NO
@property (nonatomic, strong) WKPreferences *preferences
3、WKUserContentController 通過JS與webView互動
要通過JS與webview內容互動,就需要到這個類
WKUserContentController繼承自NSObject
/WKUserContentController:
屬性
userScripts 只讀陣列內不儲存所有新增的WKUserScript物件
方法
addUserScript: 新增WKUserScript物件
removeAllUserScripts 移除所有WKUserScript物件
addScriptMessageHandler: 新增scriptMessageHandler到所有的frames中,如JS要呼叫我們原生的方法,就可以通過這種方式了
removeScriptMessageHandlerForName: 根據name移除所注入的scriptMessageHandler
@property (nonatomic, strong) WKUserContentController *userContentController;
4、WebView內容儲存
iOS9.0以後才能使用這個類。它是代表webview不同的資料型別,包括cookies、disk、memory caches、WebSQL、IndexedDB資料庫和本地儲存
@property (nonatomic, strong) WKWebsiteDataStore *websiteDataStore
5、指示是否web檢視內容呈現,直到它完全被載入到記憶體中預設NO
@property (nonatomic) BOOL suppressesIncrementalRendering;
6、應用程式的名稱中使用的使用者代理字串 iOS9+
@property (nullable, nonatomic, copy) NSString *applicationNameForUserAgent;
7、是否允許播放媒體 預設yes iOS9+
@property (nonatomic) BOOL allowsAirPlayForMediaPlayback;
7、此屬性有兩個列舉值可選WKSelectionGranularityDynamic(預設) 和 WKSelectionGranularityCharacter
WKSelectionGranularityCharacter : 使用者長按複製文字的選擇區域是非使用者自定義
WKSelectionGranularityDynamic: 使用者長按複製文字的選擇區域是使用者自定義
@property (nonatomic) WKSelectionGranularity selectionGranularity;