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 :



目录
相关文章
|
Android开发 iOS开发
iOS 替换WebView网页图片为本地图片
iOS 替换WebView网页图片为本地图片
251 0
|
3月前
|
IDE API Android开发
安卓与iOS开发环境的差异及适配策略
在移动应用开发的广阔舞台上,Android和iOS两大操作系统各据一方,各自拥有独特的开发环境和工具集。本文旨在深入探讨这两个平台在开发环境上的关键差异,并提供有效的适配策略,帮助开发者优化跨平台开发流程。通过比较Android的Java/Kotlin和iOS的Swift/Objective-C语言特性、IDE的选择、以及API和系统服务的访问方式,本文揭示了两个操作系统在开发实践中的主要分歧点,并提出了一套实用的适配方法,以期为移动开发者提供指导和启示。
|
5月前
|
iOS开发
SwiftUI适配iOS16导航控制器引起的闪退
SwiftUI适配iOS16导航控制器引起的闪退
58 0
|
5月前
|
监控 iOS开发
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
iOS15适配问题:viewForSupplementaryElementOfKind表头和表尾复用闪退,UITableView section header多22像素等问题
53 0
|
5月前
|
小程序 Android开发
Appium微信小程序自动化之开启webview调试功能方法封装
Appium微信小程序自动化之开启webview调试功能方法封装
185 0
|
11月前
|
小程序 开发工具 Android开发
Donut多端框架小程序打包适配ios和安卓app
腾讯新出了一个 Donut 多端框架,可以直接将微信小程序转成 ios 和 安卓 app,小程序开发者工具里也集成了 app 相关升级、调试和打包的功能,终于可以一套代码开发出3个客户端了!
225 0
Donut多端框架小程序打包适配ios和安卓app
|
12月前
|
安全 前端开发 开发工具
iOS12、iOS11、iOS10、iOS9常见适配
iOS12、iOS11、iOS10、iOS9常见适配
219 0
|
小程序 iOS开发
uniapp中IOS端小程序底部黑线适配的方法(整理)
uniapp中IOS端小程序底部黑线适配的方法(整理)
|
Shell iOS开发
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
iOS 逆向编程(九 - 2)将端口映射、USB连接手机封装成 .sh 脚本
130 0
|
前端开发 JavaScript 定位技术
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
iOS 逆向编程(十六)DZMCycript 脚本使用(封装了常用的快捷函数,后续会继续添加)
129 0