Authored by gaoqiang xu

提前注入cordova

... ... @@ -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
... ...
... ... @@ -27,21 +27,5 @@
</Actions>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "../YHExplorer/CordovaPlugins/CDVYHInterface.m"
timestampString = "471763039.615788"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
landmarkName = "-eventTriggered:"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
... ...
... ... @@ -73,7 +73,7 @@
} else if ([title isEqualToString:@"链接跳转"]) {
wv.url = @"http://sealedace.com/content/images/test/h5_test.html";
} else {
wv.url = @"http://yohomars.test.yoho.cn/message/index?uid=8654049&session_code=00840ce10f61844257920a986ed1b647";
wv.url = @"http://yohomars.test.yoho.cn/message/index?uid=8655817&session_code=008413c94e6ab9e0a0d4a242c50932cb";
}
[self.navigationController pushViewController:wv animated:YES];
... ...