WKWebView 是IOS8新增的 Web瀏覽視圖java
優勢: 加載速度 比UIWebView提高差很少一倍的, 內存使用上面,反而還少了一半。 git
缺點: WKWebView 不支持緩存 和 NSURLProtocol 攔截了github
我建議若是對緩存不高的頁面能夠使用,用戶體驗會提升不少。 web
因爲項目中之前都是用 UIWebView 並且還要兼容 IOS8 以前的機子。 因此 我建立了一個新類 IMYWebView 你只要全局替換 UIWebView 就能無縫升級到 WKWebView 啦緩存
IMYWebView.h 中的API 會在內部自動支持 UIWebView 和 WKWebView,app
title,estimatedProgress 是我認爲 WKWebView 中比較有用的新增APIoop
- @interface IMYVKWebView : UIView
-
- - (instancetype)initWithFrame:(CGRect)frame usingUIWebView:(BOOL)usingUIWebView;
-
- @property(weak,nonatomic)id<IMYVKWebViewDelegate> delegate;
-
- @property (nonatomic, readonly) id realWebView;
- @property (nonatomic, readonly) BOOL usingUIWebView;
- @property (nonatomic, readonly) double estimatedProgress;
-
- @property (nonatomic, readonly) NSURLRequest *originRequest;
-
- - (NSInteger)countOfHistory;
- - (void)gobackWithStep:(NSInteger)step;
-
- @property (nonatomic, readonly) UIScrollView *scrollView;
-
- - (id)loadRequest:(NSURLRequest *)request;
- - (id)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
-
- @property (nonatomic, readonly, copy) NSString *title;
- @property (nonatomic, readonly) NSURLRequest *currentRequest;
- @property (nonatomic, readonly) NSURL *URL;
-
- @property (nonatomic, readonly, getter=isLoading) BOOL loading;
- @property (nonatomic, readonly) BOOL canGoBack;
- @property (nonatomic, readonly) BOOL canGoForward;
-
- - (id)goBack;
- - (id)goForward;
- - (id)reload;
- - (id)reloadFromOrigin;
- - (void)stopLoading;
-
- - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *))completionHandler;
- - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javaScriptString __deprecated_msg("Method deprecated. Use [evaluateJavaScript:completionHandler:]");
-
- @property (nonatomic) BOOL scalesPageToFit;
-
- @end
代碼地址 https://github.com/wangyangcc/IMYWebViewatom