標籤:end appear 綁定 file art nis argument top 一個
本文包括JS調用OC方法並傳值,OC調用JS方法並傳值
本來想把html放進伺服器裡面,然後訪問,但是覺得如果html在本地載入更有助於理解,特把html放進項目裡
HTML代碼
<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><div style="margin-top: 20px"><h2>JS與OC互動</h2><input type="button" value="喚起本地方法(call)" onclick="tianbai.call()"></div><div><input type="button" value="喚起getCall:(NSString *)callString傳值" onclick="call()"></div><script>var call = function(){ var callInfo = JSON.stringify({"jianshu": "http://www.jianshu.com/users/55c8fdc3c6e7/latest_articles"}); tianbai.getCall(callInfo);}var Callback = function(str){ alert(str);}var alerCallback = function(){ alert(‘成功‘);}</script></body></html>
上面html的代碼:建立了兩個button
第一個button綁定了 tianbai.call()
方法,這裡 tianbai
是一個對象,這個對象的作用下面OC代碼中會說明, tianbai.call()
代表 tianbai
對象調用 call()
方法
第二個button綁定了 call()
的方法,調用的是下面JavaScript中的 call()
方法,在 JavaScript 的 call()
裡面,定義一個 callInfo
參數,方法中 tianbai.getCall(callInfo)
代表 tianbai
對象調用 getCall
方法並傳參數 callInfo
,下面兩個方法是OC調用JavaScript方法,其中Callback傳回str,alerCallback為OC僅調用JavaScript方法!
OC代碼
demo採用原生的JavaScriptCore類
引入三個名詞:
- JSContext:給JavaScript提供啟動並執行上下文環境
- JSValue:JavaScript和Objective-C資料和方法的橋樑
- JSExport:這是一個協議,如果採用協議的方法互動,自己定義的協議必須遵守此協議
ViewController.h中的代碼
//// ViewController.h// JavaScript//// Created by tianbai on 16/6/8.// Copyright ? 2016年 廈門乙科網路公司. All rights reserved.//#import <UIKit/UIKit.h>#import <JavaScriptCore/JavaScriptCore.h>@protocol JSObjcDelegate <JSExport>
- (void)call;- (void)getCall:(NSString *)callString;@end
@interface ViewController : UIViewController<UIWebViewDelegate, JSObjcDelegate>
@property (nonatomic, strong) JSContext *jsContext;@property (strong, nonatomic) UIWebView *webView;@end
ViewController.m中的代碼
//// ViewController.m// JavaScript//// Created by tianbai on 16/6/8.// Copyright ? 2016年 廈門乙科網路公司. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; }
- (void)viewDidAppear:(BOOL)animated {
self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; self.webView.delegate = self; NSString* path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSURL* url = [NSURL fileURLWithPath:path]; NSURLRequest* request = [NSURLRequest requestWithURL:url] ; [self.webView loadRequest:request]; [self.view addSubview:self.webView];}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; self.jsContext[@"tianbai"] = self; self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue; NSLog(@"異常資訊:%@", exceptionValue); };}- (void)call {
NSLog(@"call"); // 之後在回調js的方法Callback把內容傳出去 JSValue *Callback = self.jsContext[@"Callback"]; //傳值給web端 [Callback callWithArguments:@[@"喚起本地OC回調完成"]];}- (void)getCall:(NSString *)callString {
NSLog(@"Get:%@", callString); // 成功回調js的方法Callback JSValue *Callback = self.jsContext[@"alerCallback"]; [Callback callWithArguments:nil]; // 直接添加提示框// NSString *str = @"alert(‘OC添加JS提示成功‘)";// [self.jsContext evaluateScript:str];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
iOS OC與JS的互動(JavaScriptCore實現)