iOS OC與JS的互動(JavaScriptCore實現)

來源:互聯網
上載者:User

標籤: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類

引入三個名詞:

  1. JSContext:給JavaScript提供啟動並執行上下文環境
  2. JSValue:JavaScript和Objective-C資料和方法的橋樑
  3. 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實現)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.