标签:javascript ios objective-c stringbyev
文章来自:http://blog.csdn.net/intbird
相比android和js进行交互,ios和js进行交互是比较方便的;
http://blog.csdn.net/intbird/article/details/42295453
http://blog.csdn.net/intbird/article/details/46461203
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:javascript ios objective-c stringbyev
原文地址:http://blog.csdn.net/intbird/article/details/48139623