IOS:iOS和javaScript相互調用

文章來自: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和javaScript的交互
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章