转载于:http://blog.csdn.net/hdfqq188816190/article/details/52067798
WKWebView
看看WKWebView的头文件声明:
-
- @property(nonatomic,readonly, copy) WKWebViewConfiguration*configuration;
-
-
- @property (nullable, nonatomic, weak) id<WKNavigationDelegate> navigationDelegate;
-
-
- @property(nullable,nonatomic, weak) id<WKUIDelegate> UIDelegate;
-
-
- @property (nonatomic,readonly, strong) WKBackForwardList*backForwardList;
-
-
- -(instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration*)configurationNS_DESIGNATED_INITIALIZER;
-
-
- - (instancetype)initWithCoder:(NSCoder*)coder NS_UNAVAILABLE;
-
-
- -(nullable WKNavigation*)loadRequest:(NSURLRequest*)request;
-
-
- - (nullableWKNavigation *)loadFileURL:(NSURL*)URL allowingReadAccessToURL:(NSURL*)readAccessURLNS_AVAILABLE(10_11,9_0);
-
-
- -(nullable WKNavigation*)loadHTMLString:(NSString*)string baseURL:(nullableNSURL *)baseURL;
-
-
- - (nullableWKNavigation *)loadData:(NSData*)data MIMEType:(NSString*)MIMEType characterEncodingName:(NSString*)characterEncodingName baseURL:(NSURL*)baseURL NS_AVAILABLE(10_11,9_0);
-
-
- -(nullable WKNavigation*)goToBackForwardListItem:(WKBackForwardListItem*)item;
-
-
- @property (nullable, nonatomic, readonly, copy)NSString *title;
-
-
- @property(nullable,nonatomic, readonly, copy)NSURL *URL;
-
-
- @property (nonatomic,readonly, getter=isLoading)BOOL loading;
-
-
- @property(nonatomic,readonly) double estimatedProgress;
-
-
- @property (nonatomic,readonly) BOOL hasOnlySecureContent;
-
-
- @property(nonatomic,readonly, copy) NSArray*certificateChain NS_AVAILABLE(10_11,9_0);
-
-
- @property (nonatomic,readonly) BOOL canGoBack;
-
-
- @property(nonatomic,readonly) BOOL canGoForward;
-
-
- - (nullableWKNavigation *)goBack;
-
-
- -(nullable WKNavigation*)goForward;
-
-
- - (nullableWKNavigation *)reload;
-
-
- -(nullable WKNavigation*)reloadFromOrigin;
-
-
- - (void)stopLoading;
-
-
- -(void)evaluateJavaScript:(NSString*)javaScriptString completionHandler:(void(^ __nullable)(__nullableid, NSError * __nullable error))completionHandler;
-
-
- @property (nonatomic)BOOL allowsBackForwardNavigationGestures;
-
-
- @property(nullable,nonatomic, copy) NSString*customUserAgent NS_AVAILABLE(10_11,9_0);
-
-
- @property (nonatomic)BOOL allowsLinkPreviewNS_AVAILABLE(10_11,9_0);
-
- #if TARGET_OS_IPHONE
-
-
- @property(nonatomic,readonly, strong) UIScrollView*scrollView;
- #endif
-
- #if !TARGET_OS_IPHONE
-
- @property (nonatomic)BOOL allowsMagnification;
-
-
- @property(nonatomic)CGFloat magnification;
-
-
- - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
-
- #endif
WKWebViewConfiguration配置
WKWebViewConfiguration *config = [[WKWebViewConfigurationalloc] init];
WKPreferences偏好设置
目前在iOS平台上偏好设置只有三个属性可以设置,如下:
- / 设置偏好设置
- config.preferences=[[WKPreferencesalloc] init];
-
- config.preferences.minimumFontSize=10;
-
- config.preferences.javaScriptEnabled=YES;
-
- config.preferences.javaScriptCanOpenWindowsAutomatically=NO;
WKProcessPool内容处理池
WKProcessPool并没有公开任何的属性或者方法,不需要配置:
config.processPool=[[WKProcessPoolalloc] init];
其实我们也没有必须去创建它。
WKUserContentController内容交互控制器
我们要通过JS与webview内容交互,就需要到这个类了,它的所有属性及方法说明如下:
-
- @property(nonatomic,readonly,copy) NSArray<WKUserScript*>*userScripts;
-
-
- - (void)addUserScript:(WKUserScript*)userScript;
-
-
- -(void)removeAllUserScripts;
-
-
-
-
-
- -(void)addScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler name:(NSString*)name;
-
-
- - (void)removeScriptMessageHandlerForName:(NSString*)name;
WKUserScript
在WKUserContentController中,所有使用到WKUserScript。WKUserContentController是用于与JS交互的类,而所注入的JS是WKUserScript对象。它的所有属性和方法如下:
-
- @property(nonatomic,readonly,copy) NSString*source;
-
-
- @property (nonatomic,readonly)WKUserScriptInjectionTime injectionTime;
-
-
- @property(nonatomic,readonly,getter=isForMainFrameOnly)BOOLforMainFrameOnly;
-
-
-
-
-
- -(instancetype)initWithSource:(NSString*)source injectionTime:(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
WKUserScriptInjectionTime
- typedef NS_ENUM(NSInteger,WKUserScriptInjectionTime){
- WKUserScriptInjectionTimeAtDocumentStart,
- WKUserScriptInjectionTimeAtDocumentEnd
- }NS_ENUM_AVAILABLE(10_10,8_0);
它是一个枚举类型,只有在文档开始加载时注入和加载结束时注入。
WKWebsiteDataStore存储的Web内容
iOS9.0以后才能使用这个类。它是代表webview不同的数据类型,包括cookies、disk、memory caches、WebSQL、IndexedDB数据库和本地存储。
从这里看,要优化Webview好像可以通过它来实现,不过要求iOS9.0以上才能使用。现在6.0都没有抛弃的我,从来不能考虑这种最新的。
-
- +(WKWebsiteDataStore*)defaultDataStore;
-
-
- + (WKWebsiteDataStore*)nonPersistentDataStore;
-
-
- -(instancetype)initNS_UNAVAILABLE;
-
-
- @property (nonatomic,readonly,getter=isPersistent)BOOLpersistent;
-
-
- +(NSSet<NSString*>*)allWebsiteDataTypes;
-
-
- - (void)fetchDataRecordsOfTypes:(NSSet<NSString*>*)dataTypes completionHandler:(void(^)(NSArray<WKWebsiteDataRecord*>*))completionHandler;
-
-
- -(void)removeDataOfTypes:(NSSet<NSString*>*)dataTypes forDataRecords:(NSArray<WKWebsiteDataRecord*>*)dataRecords completionHandler:(void(^)(void))completionHandler;
-
-
- - (void)removeDataOfTypes:(NSSet<NSString*>*)websiteDataTypes modifiedSince:(NSDate*)date completionHandler:(void(^)(void))completionHandler;
所有的dataTypes是下面这些系统所定义的:
- WK_EXTERN NSString*constWKWebsiteDataTypeDiskCacheNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeMemoryCacheNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeOfflineWebApplicationCacheNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeCookiesNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeSessionStorageNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeLocalStorageNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeWebSQLDatabasesNS_AVAILABLE(10_11,9_0);
-
- WK_EXTERN NSString*constWKWebsiteDataTypeIndexedDBDatabasesNS_AVAILABLE(10_11,9_0);
WKWebsiteDataRecord
iOS9.0以后才可用。
website的数据存储记录类型,它只有两个属性:
-
- @property(nonatomic,readonly,copy) NSString*displayName;
-
-
- @property (nonatomic,readonly,copy) NSSet<NSString*>*dataTypes;
WKSelectionGranularity选择粒度
它表示在webview上选择内容的粒度,只有下面这两种类型:
|
typedef NS_ENUM(NSInteger,WKSelectionGranularity){
WKSelectionGranularityDynamic,
WKSelectionGranularityCharacter,
}NS_ENUM_AVAILABLE_IOS(8_0);
|
它是用于webview内容交互时选择内容的粒度类型设置。比如说,当使用WKSelectionGranularityDynamic时,而所选择的内容是单个块,这时候granularity可能会是单个字符;当所选择的web内容不限制于某个块时,granularity可能会是单个块。
WKNavigationDelegate
- @protocol WKNavigationDelegate<NSObject>
-
- @optional
- 1.导航监听
-
- #pragma mark - WKNavigationDelegate 页面跳转
- #pragma mark 在发送请求之前,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
-
- #pragma mark 身份验证
- - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
-
- #pragma mark 在收到响应后,决定是否跳转
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
-
- #pragma mark 接收到服务器跳转请求之后调用
- - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
-
- #pragma mark WKNavigation导航错误
- - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
-
- #pragma mark WKWebView终止
- - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView;
-
- 2.页面监听
-
- #pragma mark - WKNavigationDelegate 页面加载
- #pragma mark 页面开始加载时调用
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
-
- #pragma mark 当内容开始返回时调用
- - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation;
-
- #pragma mark 页面加载完成之后调用
- - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation;
-
- #pragma mark 页面加载失败时调用
- - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
-
- @end
WKNavigationActionPolicy
导航动作决定策略:
- typedef NS_ENUM(NSInteger,WKNavigationActionPolicy){
- WKNavigationActionPolicyCancel,
- WKNavigationActionPolicyAllow,
- }NS_ENUM_AVAILABLE(10_10,8_0);
它是枚举类型,只有Cancel和Allow这两种。设置为Cancel就是不允许导航,就不会跳转链接。
WKNavigationResponsePolicy
- typedef NS_ENUM(NSInteger,WKNavigationResponsePolicy){
- WKNavigationResponsePolicyCancel,
- WKNavigationResponsePolicyAllow,
- }NS_ENUM_AVAILABLE(10_10,8_0);
WKNavigationResponse
WKNavigationResponse是导航响应类,通过它可以获取相关响应的信息:
- NS_CLASS_AVAILABLE(10_10,8_0)
- @interface WKNavigationResponse: NSObject
-
-
- @property (nonatomic,readonly,getter=isForMainFrame)BOOLforMainFrame;
-
-
- @property(nonatomic,readonly,copy) NSURLResponse*response;
-
-
- @property (nonatomic,readonly)BOOL canShowMIMEType;
-
- @end
只有接收响应与不接收响应两种。
WKNavigationAction
WKNavigationAction对象包含关于导航的action的信息,用于make policy decisions。它只有以下几个属性:
-
- @property(nonatomic,readonly,copy) WKFrameInfo*sourceFrame;
-
-
- @property (nullable, nonatomic, readonly, copy)WKFrameInfo*targetFrame;
-
-
- @property(nonatomic,readonly)WKNavigationType navigationType;
-
-
- @property (nonatomic,readonly,copy) NSURLRequest*request;
WKNavigationType
WKNavigationType类型是枚举类型,它的可选值如下:
- typedef NS_ENUM(NSInteger,WKNavigationType){
-
- WKNavigationTypeLinkActivated,
-
- WKNavigationTypeFormSubmitted,
-
- WKNavigationTypeBackForward,
-
- WKNavigationTypeReload,
-
- WKNavigationTypeFormResubmitted,
-
- WKNavigationTypeOther=-1,
- }NS_ENUM_AVAILABLE(10_10,8_0);
WKUIDelegate
- @protocol WKUIDelegate<NSObject>
-
- @optional
-
-
-
- - (nullableWKWebView*)webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures;
-
-
- -(void)webViewDidClose:(WKWebView*)webViewNS_AVAILABLE(10_11,9_0);
-
-
- - (void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler;
-
-
- -(void)webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(BOOLresult))completionHandler;
-
-
- - (void)webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt defaultText:(nullableNSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(NSString*__nullableresult))completionHandler;
-
- @end
WKBackForwardList
WKBackForwardList表示webview中可以前进或者后退的页面列表。其声明如下:
- NS_CLASS_AVAILABLE(10_10,8_0)
- @interface WKBackForwardList: NSObject
-
-
- @property (nullable, nonatomic, readonly, strong)WKBackForwardListItem*currentItem;
-
-
- @property(nullable,nonatomic,readonly, strong)WKBackForwardListItem*backItem;
-
-
- @property (nullable, nonatomic, readonly, strong)WKBackForwardListItem*forwardItem;
-
-
- -(nullableWKBackForwardListItem*)itemAtIndex:(NSInteger)index;
-
-
- @property (nonatomic,readonly,copy) NSArray<WKBackForwardListItem*>*backList;
-
-
- @property(nonatomic,readonly,copy) NSArray<WKBackForwardListItem*>*forwardList;
-
- @end
WKBackForwardListItem
页面导航前进、后退列表项:
- NS_CLASS_AVAILABLE(10_10,8_0)
- @interface WKBackForwardListItem: NSObject
-
-
- @property (readonly, copy) NSURL*URL;
-
-
- @property(nullable,readonly,copy) NSString*title;
-
-
- @property (readonly, copy) NSURL*initialURL;
-
- @end
最后
本篇文章只是讲解了WKWebView所有相关的类的API,先阅读过本篇文章,再继续往下阅读实战篇文章!