美文网首页
iOS中JavaScript和OC交互

iOS中JavaScript和OC交互

作者: Crazy2015 | 来源:发表于2017-08-13 10:39 被阅读70次
OC调用JS

Objective-C语言调用JavaScript语言,是通过UIWebView的 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;的方法来实现的。该方法向UIWebView传递一段需要执行的JavaScript代码最后获取执行结果。

需要注入的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 {
//执行JS注入的函数
    [self.webView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];
}

JS调用OC

JavaScript语言调用Objective-C语言,并没有现成的API,但是有些方法可以达到相应的效果。具体是利用UIWebView的特性:在UIWebView的内发起的所有网络请求,都可以通过delegate函数得到通知。

- (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调用JavaScript代码的时候是同步的

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

(2)JavaScript调用Objective-C代码的时候是异步的

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

参考:
http://blog.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html

相关文章

网友评论

      本文标题:iOS中JavaScript和OC交互

      本文链接:https://www.haomeiwen.com/subject/ilisrxtx.html