OC調(diào)用JS
Objective-C語言調(diào)用JavaScript語言,是通過UIWebView的 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;的方法來實現(xiàn)的。該方法向UIWebView傳遞一段需要執(zhí)行的JavaScript代碼最后獲取執(zhí)行結(jié)果。
需要注入的js代碼
static NSString * const kTouchJavaScriptString =
@"document.ontouchstart = function (event) {\
x = event.targetTouches[0].clientX;\
y = event.targetTouches[0].clientY;\
document.location = \"myweb:touch:start:\"+x+\":\"+y;};\
document.ontouchmove = function (event) {\
x = event.targetTouches[0].clientX;\
y = event.targetTouches[0].clientY;\
document.location = \"myweb:touch:move\"+x+\":\"+y;};\
document.ontouchcancel = function (event) {\
document.location = \"myweb:touch:cancel\";};\
document.ontouchend=function(event){\
document.location=\"myweb:touch:end\";};";
//
-(void)webViewDidFinishLoad:(UIWebView *)webView {
//執(zhí)行JS注入的函數(shù)
[self.webView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];
}
JS調(diào)用OC
JavaScript語言調(diào)用Objective-C語言票从,并沒有現(xiàn)成的API,但是有些方法可以達到相應(yīng)的效果屑迂。具體是利用UIWebView的特性:在UIWebView的內(nèi)發(fā)起的所有網(wǎng)絡(luò)請求竿音,都可以通過delegate函數(shù)得到通知。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
//處理JavaScript和Objective-C交互
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0]
isEqualToString:@"myweb"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"]) {
if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"]) {
_gesState = GESTURE_STATE_START;
NSLog(@"touch start!");
float ptX = [[components objectAtIndex:3] floatValue];
float ptY = [[components objectAtIndex:4] floatValue];
NSLog(@"touch point (%f, %f)", ptX, ptY);
NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY];
NSString *tagName = [self.webView stringByEvaluatingJavaScriptFromString:js];
_imgURL = nil;
if ([tagName isEqualToString:@"IMG"]) {
_imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY];
}
if (_imgURL) {
_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(handleLongTouch) userInfo:nil repeats:NO];
}
}
else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"]) {
//**如果touch動作是滑動窄坦,則取消hanleLongTouch動作**//
_gesState = GESTURE_STATE_MOVE;
NSLog(@"you are move");
} else if ([(NSString*)[components objectAtIndex:2]isEqualToString:@"end"]) {
[_timer invalidate];
_timer = nil;
_gesState = GESTURE_STATE_END;
NSLog(@"touch end");
}
}
return NO;
}
return YES;
}
說明:
1唤反、同步和異步的問題
(1)Objective-C調(diào)用JavaScript代碼的時候是同步的
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
(2)JavaScript調(diào)用Objective-C代碼的時候是異步的
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
參考:
http://blog.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html