Authored by 孟顺

先实现corodova的UIWebview方法

review by 枪兵
... ... @@ -35,7 +35,7 @@
@property (strong, nonatomic) NSArray<NSString*> *constraintClassesForPictureTapGesture;
@property (weak, nonatomic) UINavigationBar * currentNaviBar;
- (UIWebView *)webView;
- (UIView *)webView;
- (instancetype)initWithFrame:(CGRect)frame withNaviBar:(UINavigationBar *)naviBar;
/**
... ...
... ... @@ -141,7 +141,7 @@
return _webViewController;
}
- (UIWebView *)webView
- (UIView *)webView
{
return self.webViewController.webView;
}
... ... @@ -248,7 +248,7 @@
- (void)hanldeTapGesture:(UITapGestureRecognizer *)gesture {
if (gesture == _tapGesture) {
CGFloat displayWidth = [[self.webView stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] floatValue];
CGFloat displayWidth = [[self.webViewController stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] floatValue];
CGFloat scale = self.webView.frame.size.width / displayWidth;
CGPoint touchPoint = [gesture locationInView:self.webView];
// 如果webview是zoom状态,把对应的比例除去
... ... @@ -261,7 +261,7 @@
if (self.constraintIdsForPictureTapGesture.count) {
validIdArea = NO;
NSString *frameArrayString = [self.webView stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaById]];
NSString *frameArrayString = [self.webViewController stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaById]];
if (frameArrayString.length) {
NSData *resultData = [frameArrayString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:resultData
... ... @@ -285,7 +285,7 @@
if (self.constraintClassesForPictureTapGesture.count) {
validClassArea = NO;
NSString *frameArrayString = [self.webView stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaByClass]];
NSString *frameArrayString = [self.webViewController stringByEvaluatingJavaScriptFromString:[self _getJsStringToValidateGestureAreaByClass]];
if (frameArrayString.length) {
NSData *resultData = [frameArrayString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:resultData
... ... @@ -319,7 +319,7 @@
}
NSString *imgURL = [self _getImageScriptStringWithPoint:touchPoint];
NSString *resultString = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
NSString *resultString = [self.webViewController stringByEvaluatingJavaScriptFromString:imgURL];
if (resultString.length) {
... ...
... ... @@ -12,50 +12,10 @@
#import "CDVYHInterfaceDelegate.h"
#import "YHURLCache.h"
#import "YHLinkDefaultParser.h"
#import "YHExplorerWebViewDelegate.h"
@protocol YHExplorerDelegate;
@protocol YHExplorer <NSObject>
/**
* 加载url
*
* @param url 地址
*/
- (void)loadWebUrl:(NSString * _Nonnull)url;
/**
* 加载html内容
*
* @param html 网页内容
*/
- (void)loadHTMLString:(NSString * _Nonnull)html;
- (void)loadHTMLString:(NSString *)html baseURL:(nullable NSURL *)baseURL;
/**
* 设置进度条的颜色
*
* @param color 颜色
*/
- (void)setProgressBarColor:(UIColor * _Nonnull)color;
@optional
/**
* 注册一个链接解析器
*
* @param parser 链接解析器
*/
- (void)registerLinkParser:(id <YHLinkParserDelegate> _Nonnull)parser;
/**
* 禁用链接解析
*
* @param parser 链接解析器
*/
- (void)unRegisterLinkParser:(id <YHLinkParserDelegate> _Nonnull)parser;
/**
* 移除滚动条
*/
- (void)removeProgress;
@end
@interface YHExplorerViewController : CDVViewController
<CDVYHInterfaceDelegate, YHExplorer>
... ...
... ... @@ -13,7 +13,7 @@ static float kprogressViewRadius = 2.0f;
#import "YHWebViewProgress.h"
#import "YHWebViewProgressView.h"
@interface YHExplorerViewController ()
@interface YHExplorerViewController ()<UIWebViewDelegate>
@property (strong, nonatomic) YHWebViewProgress *progressBar;
@property (strong, nonatomic) YHWebViewProgressView *progressView;
... ... @@ -29,21 +29,20 @@ static float kprogressViewRadius = 2.0f;
- (void)dealloc
{
[self.webView stopLoading];
self.webView.delegate = nil;
[self.webView removeFromSuperview];
[self stopLoading];
self.progressBar = nil;
[self removeProgress];
self.progressView = nil;
self.webView = nil;
if ([[NSURLCache sharedURLCache] isKindOfClass:[YHURLCache class]]) {
YHURLCache *cache = (YHURLCache *)[YHURLCache sharedURLCache];
[cache removeUrlsForKey:self.pointerValue];
}
if ([self.webViewEngine isKindOfClass:NSClassFromString(@"CDVWKWebViewEngine").class]) {
[self.webView removeObserver:self forKeyPath:@"title"];
}
}
- (void)initializeExplorer
... ... @@ -84,6 +83,11 @@ static float kprogressViewRadius = 2.0f;
if (self.progressBar && !self.progressBar.progressView.superview) {
[self.currentNaviBar addSubview:self.progressBar.progressView];
}
if ([self.webViewEngine isKindOfClass:NSClassFromString(@"CDVWKWebViewEngine")]) {
[self.progressView useWkWebView:(WKWebView *)self.webView];
[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
... ... @@ -203,24 +207,38 @@ static float kprogressViewRadius = 2.0f;
return self;
}
- (void)loadWebUrl:(NSString *)url
- (void)loadWebUrl:(NSString *)urlStr
{
self.startPage = url;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURL *URL = nil;
if (urlStr.length > 0) {
URL = [NSURL URLWithString:urlStr];
if (URL == nil) {
// 如果url里面含有需要转码的字符,则这里进行一次编码
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
URL = [NSURL URLWithString:urlStr];
}
}
[self loadURL:URL];
}
- (void)loadURL:(NSURL *)URL
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
if ([self.delegate respondsToSelector:@selector(explorerWillLoadRequest:)]) {
[self.delegate explorerWillLoadRequest:request];
}
[self.webView loadRequest:request];
self.startPage = request.URL.absoluteString;
[self.webViewEngine loadRequest:request];
}
- (void)loadHTMLString:(NSString *)html
{
[self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
[self.webViewEngine loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
}
- (void)loadHTMLString:(NSString *)html baseURL:(nullable NSURL *)baseURL
{
[self.webView loadHTMLString:html baseURL:baseURL];
[self.webViewEngine loadHTMLString:html baseURL:baseURL];
}
- (void)setProgressBarColor:(UIColor *)color
... ... @@ -265,6 +283,17 @@ static float kprogressViewRadius = 2.0f;
#pragma mark - UIWebDelegate
- (void)webViewDidStartLoad:(UIWebView*)theWebView
{
if (self.progressBar) {
[self.progressBar webViewDidStartLoad:theWebView];
}
if (self.delegate && [self.delegate respondsToSelector:@selector(yhExplorer_webViewDidStartLoad:)]) {
[self.delegate yhExplorer_webViewDidStartLoad:self];
}
}
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
NSString *title = [theWebView stringByEvaluatingJavaScriptFromString: @"document.title"];
... ... @@ -274,10 +303,8 @@ static float kprogressViewRadius = 2.0f;
[self.delegate pageTitleUpdated:title];
}
[super webViewDidFinishLoad:theWebView];
if (self.delegate && [self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[self.delegate webViewDidFinishLoad:theWebView];
if (self.delegate && [self.delegate respondsToSelector:@selector(yhExplorer_webViewDidFinishLoad:)]) {
[self.delegate yhExplorer_webViewDidFinishLoad:self];
}
if (self.progressBar) {
... ... @@ -289,25 +316,10 @@ static float kprogressViewRadius = 2.0f;
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) webViewDidStartLoad:(UIWebView*)theWebView
- (void)webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
[super webViewDidStartLoad:theWebView];
if (self.delegate && [self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[self.delegate webViewDidStartLoad:theWebView];
}
if (self.progressBar) {
[self.progressBar webViewDidStartLoad:theWebView];
}
}
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
[super webView:theWebView didFailLoadWithError:error];
if (self.delegate && [self.delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
[self.delegate webView:theWebView didFailLoadWithError:error];
if (self.delegate && [self.delegate respondsToSelector:@selector(yhExplorer_webView:didFailLoadWithError:)]) {
[self.delegate yhExplorer_webView:self didFailLoadWithError:error];
}
if (self.progressBar) {
... ... @@ -315,7 +327,7 @@ static float kprogressViewRadius = 2.0f;
}
}
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
- (BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if (self.progressBar && [self.progressBar checkIfRPCURL:request]) {
return NO;
... ... @@ -325,17 +337,17 @@ static float kprogressViewRadius = 2.0f;
[self.delegate webView:theWebView didCatchRequest:request navigationType:navigationType];
}
BOOL b = [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
BOOL currentShouldStartLoadWithRequest = YES;
if (self.delegate && [self.delegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
b = [self.delegate webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
if (self.delegate && [self.delegate respondsToSelector:@selector(yhExplorer_webView:shouldStartLoadWithRequest:navigationType:)]) {
currentShouldStartLoadWithRequest = [self.delegate yhExplorer_webView:self shouldStartLoadWithRequest:request navigationType:navigationType];
}
if (self.progressBar && b) {
b = [self.progressBar webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
if (self.progressBar && currentShouldStartLoadWithRequest) {
currentShouldStartLoadWithRequest = [self.progressBar webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
if (self.automaticallySchemeHandle && b) {
if (self.automaticallySchemeHandle && currentShouldStartLoadWithRequest) {
if ([[UIApplication sharedApplication] canOpenURL:request.URL]) {
NSArray *array = [self.autoSchemes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF = %@", request.URL.scheme]];
if (array.count > 0) {
... ... @@ -345,12 +357,57 @@ static float kprogressViewRadius = 2.0f;
}
// 尝试把当前url添加至缓存
if (b && [[request.URL scheme] hasPrefix:@"http"] && [[NSURLCache sharedURLCache] isKindOfClass:[YHURLCache class]]) {
if (currentShouldStartLoadWithRequest && [[request.URL scheme] hasPrefix:@"http"] && [[NSURLCache sharedURLCache] isKindOfClass:[YHURLCache class]]) {
YHURLCache *cache = (YHURLCache *)[YHURLCache sharedURLCache];
[cache addCacheUrl:request.URL.absoluteString forKey:self.pointerValue];
}
return b;
return currentShouldStartLoadWithRequest;
}
#pragma mark - WebViewProtocol
- (void)stopLoading
{
if ([self.webView isKindOfClass:UIWebView.class]) {
[(UIWebView *)self.webView stopLoading];
} else if ([self.webView isKindOfClass:WKWebView.class]) {
[(WKWebView *)self.webView stopLoading];
}
}
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javaScriptString
{
if ([self.webView isKindOfClass:UIWebView.class]) {
NSString *result = [(UIWebView *)self.webView stringByEvaluatingJavaScriptFromString:javaScriptString];
return result;
} else if ([self.webView isKindOfClass:WKWebView.class]) {
__block NSString *result = nil;
__block BOOL isExecuted = NO;
[(WKWebView*)self.webView evaluateJavaScript:javaScriptString completionHandler:^(id obj, NSError* error) {
result = obj;
isExecuted = YES;
}];
while (isExecuted == NO) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return result;
}
return nil;
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"title"])
{
NSString *title = change[NSKeyValueChangeNewKey];
_pageTitle = title;
if (self.delegate && [self.delegate respondsToSelector:@selector(pageTitleUpdated:)]) {
[self.delegate pageTitleUpdated:title];
}
}
}
#pragma mark - CDVYHInterfaceDelegate
... ...
//
// YHExplorerWebViewDelegate.h
// ALWlibwebp
//
// Created by 孟顺 on 2019/3/5.
//
#import <Foundation/Foundation.h>
#import <Cordova.h>
@class YHExplorerViewController;
@protocol YHExplorerWebViewDelegate <NSObject>
@optional
- (void)yhExplorer_webViewDidStartLoad:(nonnull YHExplorerViewController *)explorer;
- (void)yhExplorer_webViewDidFinishLoad:(nonnull YHExplorerViewController *)explorer;
- (void)yhExplorer_webView:(nonnull YHExplorerViewController *)explorer didFailLoadWithError:(nullable NSError *)error;
- (BOOL)yhExplorer_webView:(nonnull YHExplorerViewController *)explorer shouldStartLoadWithRequest:(nullable NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
@end
#pragma mark - Protocol:YHExplorerDelegate
@protocol YHExplorerDelegate <YHExplorerWebViewDelegate>
@optional
/**
* 获取页面的title
*
* @param title 页面的title
*/
- (void)pageTitleUpdated:(NSString * _Nullable)title;
/**
* webView探测到含有指定参数的回调
*
* @param params 参数
*/
- (void)explorerDidDetectParameters:(NSDictionary * _Nonnull)params __attribute__((deprecated("Use `- explorer:didDetectParameters:` instead.")));
/**
@brief 获取页面的title
@param explorer explorer对象
@param params 参数
@since 1.1.0
*/
- (void)explorer:(YHExplorerViewController * _Nonnull)explorer didDetectParameters:(NSDictionary * _Nonnull)params;
/**
@brief 将要加载请求前的回调
@param request 请求
@since 1.0.7
*/
- (void)explorerWillLoadRequest:(NSMutableURLRequest * _Nonnull)request;
/**
@brief 补充userAgent
@return 需要添加的UserAgent
@since 1.1.0
*/
- (NSString * _Nullable)extraUserAgent;
/**
@brief 点击图片事件
注:需要开启 `YHExplorerView`属性`enablePictureTapGesture`
@param explorer explorer对象
@param params 图片的一些信息
@since 1.1.3
*/
- (void)explorer:(YHExplorerViewController * _Nonnull)explorer didClickPicture:(NSDictionary * _Nonnull)pictureInfo;
#pragma mark Native
/**
@brief H5端触发Native事件
@param params 事件参数
@param completion 处理完后的回调(务必回调!!!)
@since 1.1.0
*/
- (void)didGetMessageFromWeb:(NSDictionary * _Nonnull)params completion:(void (^ _Nonnull) (NSDictionary * _Nullable, BOOL))completion;
/**
由于shouldStartLoadWithRequest这个代理方法不适合对外暴露,此代理将暴露webview拦截到的请求
@param webView UIWebView
@param request 网页请求
@param navigationType 请求类型
*/
- (void)webView:(UIWebView *)webView didCatchRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
@required
@end
@protocol YHExplorerWebViewActionProtocol <NSObject>
@optional
- (nullable UIScrollView *)scrollView;
- (nullable NSURL *)requestURL;
- (BOOL)isLoading;
- (BOOL)canGoBack;
- (BOOL)canGoForward;
- (nullable id)goBack;
- (nullable id)goForward;
- (nullable id)reload;
- (nullable id)reloadFromOrigin;
- (void)stopLoading;
@property (nonatomic) BOOL scalesPageToFit;
- (void)evaluateJavaScript:(nonnull NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(nonnull NSString *)javaScriptString;
- (void)yh_setScrollEnabled:(BOOL)enabled;
@end
#pragma mark - Protocol:YHExplorer
@protocol YHExplorer <YHExplorerWebViewActionProtocol>
/**
* 加载url
*
* @param url 地址
*/
- (void)loadWebUrl:(NSString * _Nonnull)url;
/**
* 加载URL
*
* @param URL
*/
- (void)loadURL:(NSURL * _Nonnull)URL;
/**
* 加载html内容
*
* @param html 网页内容
*/
- (void)loadHTMLString:(NSString * _Nonnull)html;
/**
* 加载html内容
*
* @param html 网页内容
* @param baseURL URL
*/
- (void)loadHTMLString:(NSString * _Nonnull)html baseURL:(NSURL * __nullable)baseURL;
/**
* 设置进度条的颜色
*
* @param color 颜色
*/
- (void)setProgressBarColor:(UIColor * _Nonnull)color;
@optional
/**
* 注册一个链接解析器
*
* @param parser 链接解析器
*/
- (void)registerLinkParser:(id <YHLinkParserDelegate> _Nonnull)parser;
/**
* 禁用链接解析
*
* @param parser 链接解析器
*/
- (void)unRegisterLinkParser:(id <YHLinkParserDelegate> _Nonnull)parser;
/**
* 移除滚动条
*/
- (void)removeProgress;
@end
... ...
#import "CDVYHInterface.h"
#import "NSDictionary+Extensions.h"
#import "CDVYHInterfaceDelegate.h"
#import <objc/runtime.h>
#import "WebPProtocol.h"
//#import "WebPProtocol.h"
@interface CDVYHInterface ()
@property (strong, nonatomic) NSMutableDictionary *abilities;
... ... @@ -18,13 +17,13 @@
- (void)pluginInitialize {
[NSURLProtocol registerClass:[WebPProtocol class]];
// [NSURLProtocol registerClass:[WebPProtocol class]];
}
- (void)dispose {
[super dispose];
[NSURLProtocol unregisterClass:[WebPProtocol class]];
// [NSURLProtocol unregisterClass:[WebPProtocol class]];
}
- (void)dealloc
... ...