iOS 自动适配的 WebView 封装 (下)

简介: iOS 自动适配的 WebView 封装 (下)

⚖ 许可证


RSAutoWebView 使用 MIT 许可证,详情见 LICENSE 文件。


😬 联系方式:



EN



image.png


For different versions of iOS WebView fragmentation, sorting package, the main function is based on the system version automatically select UI / WK-WebView to load.

Package function:

  • Block Method JavaScript <-> Objective-C Interaction.
  • Swipe to the top function
  • Extended script
  • Cookies for WKWebView and UIWebView cache interoperability
  • Content rendering interventions
  • New Features: Join Tencent VAS · Sonic technology, a framework that claims WebView can be seconds off.
  • No need to set up, without plug-ins, to create a base class Inheritance Management Global WebView page. *



🎨 What does the test UI look like?


image.png


🚀 Advantage


  1. Less documents, code concise
  2. According to the system version automatically choose to use UI / WK WebView, enhance the user experience
  3. Support both WebView Delegate system callbacks and WebView JavaScriptBridge library callbacks
  4. The structure of the excellent structure, centralized management through the base class to load the page
  5. Have a higher custom



🤖 Requirements


  • iOS 7+
  • Xcode 8+


🎯 Usage Usage


1. RSAutoWebView wrapper method

1.1 Foreign methods

//Use UIWebView
- (instancetype)initWithFrame:(CGRect)frame usingUIWebView:(BOOL)usingUIWebView;
//Will be transferred WKUIDelegate, WKNavigationDelegate unimplemented callback.
@property (weak, nonatomic) id<RSAutoWebViewDelegate> delegate;
//Internal use of webView
@property (nonatomic, readonly) id realWebView;
//Whether you are using UIWebView
@property (nonatomic, readonly) BOOL usingUIWebView;
//Estimate the progress of webpage loading
@property (nonatomic, readonly) double estimatedProgress;
@property (nonatomic, readonly) NSURLRequest* originRequest;
//Only ios7 above UIWebView can be obtained, WKWebView use the following method.
@property (nonatomic, readonly) JSContext* jsContext;
//WKWebView way to interact with the web page.
- (void)addScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler name:(NSString*)name;
//back layer
- (NSInteger)countOfHistory;
- (void)gobackWithStep:(NSInteger)step;


1.2 internal methods

//---- UI or 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;
// Clean up the cache
- (void)clearCache;
- (void)evaluateJavaScript:(NSString*)javaScriptString completionHandler:(void (^)(id, NSError*))completionHandler;
// It is not recommended to use this method because it will wait for the execution of the webView internally
- (NSString*)stringByEvaluatingJavaScriptFromString:(NSString*)javaScriptString __deprecated_msg("Method deprecated. Use [evaluateJavaScript:completionHandler:]");
// Whether to scale the page by view size defaults to YES
@property (nonatomic) BOOL scalesPageToFit;


2. BaseWebViewController base class method

2.1 The basic method

/ *
* url request address
* isOpen Whether to enable automatic selection of WebView features
* Is: usingUIWebView setting is invalid
* No: usingUIWebView set whether to use UIWebView
* /
- (void)loadWebViewWithURL:(NSString *)url autoChoose:(BOOL)isOpen ifCloseAutoChooseUsingUIWebView:(BOOL)usingUIWebView;


2.2 Enhance the method

#pragma mark - Parameters, methods
// URL address
@property (nonatomic,copy) NSString *url;
// Load URL method
- (void)reloadRequest:(NSString *)url;
// Back to top
- (void)scrollToTop;
// Image adapted to JavaScript injection
- (void)imgAutoFit;
// Collapse the keyboard method in WebView
- (void)packupKeyboard;
// Return method
- (void)webViewBackAction:(UIBarButtonItem *)sender;
// Clean up the cache
- (void)clearCache;
#pragma mark - Proxy method
/**
*  Unified wk ui loading state proxy method, combo
*/
@property (nonatomic,copy) void(^startLoadBlock)(id webView);
@property (nonatomic,copy) void(^finishLoadBlock)(id webView);
@property (nonatomic,copy) void(^failLoadBlock)(id webView, NSError *error);
#pragma mark - WebViewJavascriptBridge JS interaction method
// callHandler [OC news to JS]
- (void)JSCallHandlerWithFuncName:(NSString *)name Data:(NSDictionary *)dicData;
@property (nonatomic,copy) void(^javaScriptCallReturnBlock)(id response);
// registerHandler [OC receive JS news]
- (void)JSRegisterHandlerWithFuncName:(NSString *)name;
@property (nonatomic,copy) void(^javaScriptRegisterReturnBlock)(id response, WVJBResponseCallback responseCallback);


3. Tencent VAS · Sonic seconds to open the WebView framework

3.1 Introduction

VasSonic named after the Sony animated image sound speed kid, is Tencent QQ membership VAS team developed a lightweight high-performance Hybrid framework, focusing on improving the first page of the page loading speed, the perfect support for static straight out of the page and the dynamic straight out of the page, Compatible with offline packages and other programs. Currently QQ members, QQ shopping, QQ wallet, penguins and other businesses have been using e-sports, the average daily PV in more than 120 million, and this number is still rapidly growing.


image.png


The first time you access VasSonic, you can request the page resources in parallel while initializing your app and have the ability to load side-edges. When not opening for the first time, the app can quickly load the last time the page resource was dynamically cached in the local page, and then dynamically refresh the page. Tencent mobile QQ through the VasSonic framework makes the first page of the screen consumes less than 1S average time below.

Official open source address: github.com/Tencent/vas…


3.2 The basic method

Quote the header file

// Tencent VAS framework
#import "Sonic.h"
#import "SonicWebViewController.h"
#import "SonicOfflineCacheConnection.h"

Use the jump method

SonicWebViewController *webVC = [[SonicWebViewController alloc]initWithUrl:testTencentURL useSonicMode:YES];
webVC.title = @"VAS·Sonic";
[self.navigationController pushViewController:webVC animated:YES];

Simple to use, efficient, process-safe ~~~ If you have better suggestions, I hope have enlighten me!


Your star is my motivation to keep updating!


📝 CocoaPods update log


  • ** 2017.08.22 (tag: 0.9.0): ** </ br>
  1. Sort open base class method, optimize the realization of the function; </ br>
  2. Join Tencent open source VasSonic second open WebView framework. </ br>
  • ** 2017.04.10 (tag: 0.8.0): ** </ br>
  1. Add WebViewJavascriptBridge support, add JavaScript <-> Objective-C interaction; </ br> 2.Block callback package; </ br> 3.Cookie cache synchronization mechanism. </ br>
  • ** 2016.09.18 (tag: 0.2.5): ** </ br>
  1. Automatically switch WK / UI-WebView by system version, </ br> 2 functional package. </ br>
  • ** 2016.08.26 (tag: 0.1.0): ** </ br>
  1. Create a manual WebView switch tool. </ br>



⚖ License License


RSAutoWebView uses a MIT license, as detailed in the LICENSE file.


😬 Contact :



目录
相关文章
|
8月前
|
Android开发 iOS开发
iOS 替换WebView网页图片为本地图片
iOS 替换WebView网页图片为本地图片
207 0
|
3月前
|
小程序 Android开发
Appium微信小程序自动化之开启webview调试功能方法封装
Appium微信小程序自动化之开启webview调试功能方法封装
82 0
|
9月前
|
XML Android开发 数据格式
WebView二次封装,使用简单
WebView二次封装,使用简单
300 0
|
5月前
|
小程序 开发工具 Android开发
Donut多端框架小程序打包适配ios和安卓app
腾讯新出了一个 Donut 多端框架,可以直接将微信小程序转成 ios 和 安卓 app,小程序开发者工具里也集成了 app 相关升级、调试和打包的功能,终于可以一套代码开发出3个客户端了!
129 0
Donut多端框架小程序打包适配ios和安卓app
|
7月前
|
小程序 iOS开发
uniapp中IOS端小程序底部黑线适配的方法(整理)
uniapp中IOS端小程序底部黑线适配的方法(整理)
|
8月前
|
Shell iOS开发
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
92 0
|
8月前
|
前端开发 JavaScript 定位技术
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
97 0
|
8月前
|
缓存 JavaScript iOS开发
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
iOS 逆向编程(十五)Cycript 语法进阶(封装 .cy 脚本文件)
119 0
|
9月前
|
开发工具 iOS开发 开发者
iOS 暗黑模式的适配总结
iOS 暗黑模式的适配总结
|
开发工具 iOS开发 Perl
iOS SDK封装Framework带资源文件封装(二)
iOS SDK封装Framework带资源文件封装
 iOS SDK封装Framework带资源文件封装(二)