UIWebView 無縫切換到 WKWebView

來源:互聯網
上載者:User

標籤:api   中比   history   lan   nsurl   origin   pre   mic   sans   


WKWebView 是IOS8新增的 Web瀏覽視圖


長處:   載入速度  比UIWebView提升差點兒相同一倍的, 記憶體使用量上面,反而還少了一半。

 

缺點:   WKWebView 不支援緩衝 和   NSURLProtocol 攔截了


我建議假設對緩衝不高的頁面能夠使用。使用者體驗會提高非常多。  


因為項目中曾經都是用 UIWebView  並且還要相容 IOS8  之前的機子。  所以  我建立了一個新類  IMYWebView   你僅僅要全域替換 UIWebView 就能無縫升級到 WKWebView 啦


IMYWebView.h 中的API 會在內部自己主動支援 UIWebView 和 WKWebView。

title,estimatedProgress是我覺得 WKWebView 中比較實用的新增API

@interface IMYVKWebView : UIView///使用UIWebView- (instancetype)initWithFrame:(CGRect)frame usingUIWebView:(BOOL)usingUIWebView;@property(weak,nonatomic)id<IMYVKWebViewDelegate> delegate;///內部使用的webView@property (nonatomic, readonly) id realWebView;///是否正在使用 UIWebView@property (nonatomic, readonly) BOOL usingUIWebView;///預估網頁載入進度@property (nonatomic, readonly) double estimatedProgress;@property (nonatomic, readonly) NSURLRequest *originRequest;///back 層數- (NSInteger)countOfHistory;- (void)gobackWithStep:(NSInteger)step;///---- UI 或者 WK 的API@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;///不建議使用這個辦法  由於會在內部等待webView 的運行結果- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javaScriptString __deprecated_msg("Method deprecated. Use [evaluateJavaScript:completionHandler:]");///是否依據視圖大小來縮放頁面  默覺得YES@property (nonatomic) BOOL scalesPageToFit;@end


代碼地址

UIWebView 無縫切換到 WKWebView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.