Authored by 戴强

Merge branch '1.3.0' into 'master'

1.3.0



See merge request !5
Pod::Spec.new do |s|
s.name = "YHExplorer"
s.version = "1.3.2"
s.version = "1.4.0"
s.summary = "Integrated WebView with Cordova"
s.description = <<-DESC
An WebView Kit integrated Cordova.
... ...
... ... @@ -78,6 +78,7 @@
#import "CDVWebViewDelegate.h"
#import "CDVAvailability.h"
#import "YHInjectJsCordovaManager.h"
// #define VerboseLog NSLog
#define VerboseLog(...) do {} while (0)
... ... @@ -275,6 +276,8 @@ static NSString *stripFragment(NSString* url)
- (void)webViewDidStartLoad:(UIWebView*)webView
{
VerboseLog(@"webView didStartLoad (before). state=%d loadCount=%d", _state, _loadCount);
[[YHInjectJsCordovaManager sharedInstance]cycleInject:webView];
BOOL fireCallback = NO;
switch (_state) {
case STATE_IDLE:
... ... @@ -334,6 +337,8 @@ static NSString *stripFragment(NSString* url)
- (void)webViewDidFinishLoad:(UIWebView*)webView
{
VerboseLog(@"webView didFinishLoad (before). state=%d loadCount=%d", _state, _loadCount);
[[YHInjectJsCordovaManager sharedInstance]cycleInject:webView];
BOOL fireCallback = NO;
switch (_state) {
case STATE_IDLE:
... ...
... ... @@ -251,50 +251,6 @@ static float kprogressViewRadius = 2.0f;
}
#pragma mark - Private
/**
* 动态注入Js
*
* @param resource js的名称
*/
- (BOOL)injectJavascript:(NSString *)resource {
NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@"js"];
NSString *js = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
[self.webView stringByEvaluatingJavaScriptFromString:js];
//用cordova.version判断cordova是否注入成功
NSString* jsCordovaVersion = [NSString stringWithFormat:
@"cordova.version"];
NSString* res = [self.webView stringByEvaluatingJavaScriptFromString:jsCordovaVersion];
if (res != nil && res.length > 0) {
return TRUE;
}
return FALSE;
}
/**
@brief 循环注入直到成功
@since 1.0.2
*/
- (void)cycleInject {
BOOL bSuc = NO;
// 向网页中注入cordova
bSuc = [self injectJavascript:@"cordova"];
if (!bSuc) {
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf cycleInject];
});
}
}
- (void)parseFinishedWithResult:(NSDictionary *)result
{
if ([self.delegate respondsToSelector:@selector(explorerDidDetectParameters:)])
... ... @@ -320,8 +276,6 @@ static float kprogressViewRadius = 2.0f;
[super webViewDidFinishLoad:theWebView];
[self cycleInject];
if (self.delegate && [self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[self.delegate webViewDidFinishLoad:theWebView];
}
... ... @@ -346,8 +300,6 @@ static float kprogressViewRadius = 2.0f;
if (self.progressBar) {
[self.progressBar webViewDidStartLoad:theWebView];
}
[self cycleInject];
}
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
... ...
//
// YHInjectJsCordovaManager.h
// YH_Explorer
//
// Created by 孙凯 on 2018/5/15.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface YHInjectJsCordovaManager : NSObject
+ (YHInjectJsCordovaManager *)sharedInstance;
- (void)cycleInject:(UIWebView*)webView;
@end
... ...
//
// YHInjectJsCordovaManager.m
// YH_Explorer
//
// Created by 孙凯 on 2018/5/15.
//
#import "YHInjectJsCordovaManager.h"
@interface YHInjectJsCordovaManager()
@property (nonatomic,copy)NSString *jsCordova;
@end
@implementation YHInjectJsCordovaManager
/**
* @abstract
* 返回初始化好的单例
*
* @return 返回的单例
*/
+ (instancetype)sharedInstance{
static YHInjectJsCordovaManager *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[YHInjectJsCordovaManager alloc] init];
});
return sharedInstance;
}
#pragma mark - Private
/**
* 动态注入Js
*
* @param resource js的名称
*/
- (BOOL)injectJavascript:(NSString *)resource webView:(UIWebView*)webView{
if(!self.jsCordova) {
NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@"js"];
self.jsCordova = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
}
[webView stringByEvaluatingJavaScriptFromString:self.jsCordova];
//用cordova.version判断cordova是否注入成功
NSString* jsCordovaVersion = [NSString stringWithFormat:
@"cordova.version"];
NSString* res = [webView stringByEvaluatingJavaScriptFromString:jsCordovaVersion];
if (res != nil && res.length > 0) {
return TRUE;
}
return FALSE;
}
/**
@brief 循环注入直到成功
@since 1.0.2
*/
- (void)cycleInject:(UIWebView*)webView {
BOOL bSuc = NO;
// 向网页中注入cordova
bSuc = [self injectJavascript:@"cordova" webView:webView];
if (!bSuc) {
__weak typeof(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf cycleInject:webView];
});
}
}
@end
... ...
... ... @@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
04FE0E1F20AAFCE40010E653 /* YHInjectJsCordovaManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FE0E1E20AAFCE40010E653 /* YHInjectJsCordovaManager.m */; };
500053DE1AFB164500AF7D48 /* untitled.html in Resources */ = {isa = PBXBuildFile; fileRef = 500053DD1AFB164500AF7D48 /* untitled.html */; };
5015722D1CC8B1E900EE6B13 /* WebP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50A0C9BC1CC8A98000D1AF73 /* WebP.framework */; };
50386F0E1B6F3E43000F62A8 /* YHURLCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 50386EA61B6F3E43000F62A8 /* YHURLCache.m */; };
... ... @@ -58,6 +59,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
04FE0E1D20AAFCE40010E653 /* YHInjectJsCordovaManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = YHInjectJsCordovaManager.h; sourceTree = "<group>"; };
04FE0E1E20AAFCE40010E653 /* YHInjectJsCordovaManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = YHInjectJsCordovaManager.m; sourceTree = "<group>"; };
500053DD1AFB164500AF7D48 /* untitled.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = untitled.html; sourceTree = "<group>"; };
50386EA51B6F3E43000F62A8 /* YHURLCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHURLCache.h; sourceTree = "<group>"; };
50386EA61B6F3E43000F62A8 /* YHURLCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YHURLCache.m; sourceTree = "<group>"; };
... ... @@ -185,6 +188,8 @@
50386F071B6F3E43000F62A8 /* YHExplorerViewController.h */,
50386F081B6F3E43000F62A8 /* YHExplorerViewController.m */,
50386F091B6F3E43000F62A8 /* YHLinkParser */,
04FE0E1D20AAFCE40010E653 /* YHInjectJsCordovaManager.h */,
04FE0E1E20AAFCE40010E653 /* YHInjectJsCordovaManager.m */,
);
name = YHExplorer;
path = ../YHExplorer;
... ... @@ -484,6 +489,7 @@
buildActionMask = 2147483647;
files = (
50386F171B6F3E43000F62A8 /* CDVJSON.m in Sources */,
04FE0E1F20AAFCE40010E653 /* YHInjectJsCordovaManager.m in Sources */,
50A0C9C41CC8A98000D1AF73 /* WebPProtocol.m in Sources */,
50386F241B6F3E43000F62A8 /* NSMutableArray+QueueAdditions.m in Sources */,
50386F361B6F3E43000F62A8 /* YHExplorerView.m in Sources */,
... ... @@ -629,7 +635,7 @@
GCC_PREFIX_HEADER = YohoExplorerDemo/PrefixHeader.pch;
HEADER_SEARCH_PATHS = "";
INFOPLIST_FILE = YohoExplorerDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
... ... @@ -652,7 +658,7 @@
GCC_PREFIX_HEADER = YohoExplorerDemo/PrefixHeader.pch;
HEADER_SEARCH_PATHS = "";
INFOPLIST_FILE = YohoExplorerDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
... ...