码迷,mamicode.com
首页 > 移动开发 > 详细

IOS:ios和javaScript相互调用 oc和js

时间:2015-08-31 21:44:41      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:javascript   ios   objective-c   stringbyev   

文章来自:http://blog.csdn.net/intbird
相比android和js进行交互,ios和js进行交互是比较方便的;

android和js进行交互,请看这里:

http://blog.csdn.net/intbird/article/details/42295453

android和js进行交互框架,看这里:

http://blog.csdn.net/intbird/article/details/46461203

这里看ios和js的交互

0.上个丑图
技术分享

1,oc调用js,超级方便,不论在代码中的任何位置,
只需调用stringByEvaluatingJavaScriptFromString即可
代码:

- (IBAction)webDemo:(id)sender {
    NSString *name = [[UIDevice currentDevice]name];
    NSString *js = [NSString stringWithFormat:@"showMessage(‘%@‘)",name];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

2,js调用oc,大题是拦截每个url,对指定的schema进行拦截做相应的本地方法
代码:

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

    NSString * url = request.URL.absoluteString;
    if([url hasPrefix:@"intbird://"]){
        NSString *urlConntet = [url substringFromIndex:@"intbird://".length];
        NSArray *urlParams = [urlConntet componentsSeparatedByString:@"/"];

        NSString* methodname = urlParams[0];
        NSString* param = urlParams[1];

        if([methodname isEqualToString:@"demoCallMethod"]){
            [self performSelector:@selector(demoCallMethod:) withObject:param];
        }

        //# pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        //SEL method = NSSelectorFromString(methodname);
        //[self performSelector:method withObject:param];
        return NO;
    }
    return YES;
}

4,重要ViewController代码:

//
//  ViewController.m
//  IntbirdJsInterface
//
//  Created by intbird on 15/8/31.
//  Copyright ? 2015年 intbird. All rights reserved.
//

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1"];
    NSURLRequest* request= [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];

    _webView.delegate = self;
}

//oc 调用 js;
- (IBAction)webDemo:(id)sender {
    NSString *name = [[UIDevice currentDevice]name];
    NSString *js = [NSString stringWithFormat:@"showMessage(‘%@‘)",name];
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

//js 调用 oc;
-(BOOL)webView:(nonnull UIWebView *)webView shouldStartLoadWithRequest:(nonnull NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString * url = request.URL.absoluteString;
    if([url hasPrefix:@"intbird://"]){
        NSString *urlConntet = [url substringFromIndex:@"intbird://".length];
        NSArray *urlParams = [urlConntet componentsSeparatedByString:@"/"];

        NSString* methodname = urlParams[0];
        NSString* param = urlParams[1];

        if([methodname isEqualToString:@"demoCallMethod"]){
            [self performSelector:@selector(demoCallMethod:) withObject:param];
        }

        //# pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        //SEL method = NSSelectorFromString(methodname);
        //[self performSelector:method withObject:param];
        return NO;
    }
    return YES;
}

-(void)demoCallMethod:(NSObject*)message{

    UIAlertController * alert  = [UIAlertController alertControllerWithTitle:@"demoCallMethod"
                                                                     message:(NSString*)message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * alertCacel = [UIAlertAction actionWithTitle:@"Cacel"
                                                          style:UIAlertActionStyleCancel
                                                        handler:nil];
    [alert addAction:alertCacel];
    [ self presentViewController:alert animated:YES completion:nil];
}

-(void)webView:(nonnull UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    //JSContext *context = [webView valueForKey:@"documentView.webView.mainFrame.javaScriptContext"];
    // NSString *alertStr = @"alert(‘alert called by oc‘)";
    //[context evaluateScript:alertStr];

}

-(void)webViewDidFinishLoad:(nonnull UIWebView *)webView{

}

-(void)webViewDidStartLoad:(nonnull UIWebView *)webView{

}

-(void)webGoback:(id)sender{
    if([_webView canGoBack]){
        [_webView goBack];
    }
}

-(void)webReload:(id)sender{
    [_webView reload];
}

@end

5:废话不多说了,直接上demo:
https://github.com/intbird/IOSIntbirdJsInterface

版权声明:本文为博主原创文章,未经博主允许不得转载。

IOS:ios和javaScript相互调用 oc和js

标签:javascript   ios   objective-c   stringbyev   

原文地址:http://blog.csdn.net/intbird/article/details/48139623

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!