...
|
...
|
@@ -205,11 +205,47 @@ |
|
|
*
|
|
|
* @param resource js的名称
|
|
|
*/
|
|
|
- (void)injectJavascript:(NSString *)resource {
|
|
|
- (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];
|
|
|
return ([self.webView stringByEvaluatingJavaScriptFromString:js] != nil);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@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];
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
for (NSString *ability in self.nativeAbilities) {
|
|
|
if ([ability isEqualToString:YHNative_Camera]) {
|
|
|
[self injectJavascript:@"native_camera"];
|
|
|
} else if ([ability isEqualToString:YHNative_LBS]) {
|
|
|
[self injectJavascript:@"native_LBS"];
|
|
|
} else if ([ability isEqualToString:YHNative_Device]) {
|
|
|
[self injectJavascript:@"native_device"];
|
|
|
} else if ([ability isEqualToString:YHNative_Motion]) {
|
|
|
[self injectJavascript:@"native_motion"];
|
|
|
} else if ([ability isEqualToString:YHNative_FS]) {
|
|
|
[self injectJavascript:@"native_FS"];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)parseFinishedWithResult:(NSDictionary *)result
|
...
|
...
|
@@ -231,23 +267,6 @@ |
|
|
}
|
|
|
|
|
|
[super webViewDidFinishLoad:theWebView];
|
|
|
|
|
|
// 向网页中注入cordova
|
|
|
[self injectJavascript:@"cordova"];
|
|
|
|
|
|
for (NSString *ability in self.nativeAbilities) {
|
|
|
if ([ability isEqualToString:YHNative_Camera]) {
|
|
|
[self injectJavascript:@"native_camera"];
|
|
|
} else if ([ability isEqualToString:YHNative_LBS]) {
|
|
|
[self injectJavascript:@"native_LBS"];
|
|
|
} else if ([ability isEqualToString:YHNative_Device]) {
|
|
|
[self injectJavascript:@"native_device"];
|
|
|
} else if ([ability isEqualToString:YHNative_Motion]) {
|
|
|
[self injectJavascript:@"native_motion"];
|
|
|
} else if ([ability isEqualToString:YHNative_FS]) {
|
|
|
[self injectJavascript:@"native_FS"];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
|
|
|
[self.delegate webViewDidFinishLoad:theWebView];
|
...
|
...
|
@@ -273,6 +292,8 @@ |
|
|
if (self.progressBar) {
|
|
|
[self.progressBar webViewDidStartLoad:theWebView];
|
|
|
}
|
|
|
|
|
|
[self cycleInject];
|
|
|
}
|
|
|
|
|
|
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
|
...
|
...
|
|