日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JS与OC中的方法相互调用

發布時間:2025/3/14 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JS与OC中的方法相互调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章主要是介紹oc如何調用js 以及js的代碼調用oc的方法?

先上代碼后做解釋

//oc的.m 的代碼

//ps接下來有js的代碼一并解析,最后會附上demo

//? ViewController.m

//? JSAndOC

//

//? Created by dongqiangfei on 16/7/21.

//? Copyright ? 2016年 dongqiangfei. All rights reserved.

//

?

#import "ViewController.h"

#import <JavaScriptCore/JavaScriptCore.h>

?

@interface ViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webView;

@end

?

@implementation ViewController

?

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? [self makeBtn];

? ? [self makeWeb];

? ? // Do any additional setup after loading the view, typically from a nib.

}

?

-(void)makeBtn

{

? ? UIButton *thisBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

? ? thisBtn.frame = CGRectMake(100, 100, 140, 40);

? ? [thisBtn addTarget:self action:@selector(ocCallJS) forControlEvents:UIControlEventTouchUpInside];

? ? [thisBtn setTitle:@"點擊oc調用js" forState:UIControlStateNormal];

? ? [self.view addSubview:thisBtn];

}

?

-(void)ocCallJS

{

? ? [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showTitleMessage('%@')",@"oc調用了js的內容"]];

}

?

-(void)makeWeb

{

? ? self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height - 200)];

? ? self.webView.backgroundColor = [UIColor whiteColor];

? ? self.webView.scalesPageToFit = YES;

? ? self.webView.delegate = self;

?? ?

? ? NSString *webPath = [[NSBundle mainBundle] pathForResource:@"ocandjs" ofType:@"html"];

? ? NSURL *webURL = [NSURL fileURLWithPath:webPath];

? ? NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:webURL];

? ? [self.webView loadRequest:URLRequest];

? ? [self.view addSubview:self.webView];

?? ?

? ? JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

? ? content[@"bdgt"] = ^() {

? ? ? ? NSLog(@"js調用oc---------begin--------");

? ? ? ? NSArray *thisArr = [JSContext currentArguments];

? ? ? ? for (JSValue *jsValue in thisArr) {

? ? ? ? ? ? NSLog(@"=======%@",jsValue);

? ? ? ? }

? ? ? ? //JSValue *this = [JSContext currentThis];

? ? ? ? //NSLog(@"this: %@",this);

? ? ? ? NSLog(@"js調用oc---------The End-------");

? ? ? ? [self.webView stringByEvaluatingJavaScriptFromString:@"show();"];

? ? };

}

?

- (void)didReceiveMemoryWarning {

? ? [super didReceiveMemoryWarning];

? ? // Dispose of any resources that can be recreated.

}

?

@end

?

//js的代碼

<html>

? ? <!--描述網頁信息-->

? ? <head>

? ? ? ? <meta charset="UTF-8"/>

? ? ? ? <title>iOS上webView與JS交互的之oc調用js的demo</title>

? ? ? ? <script>

? ? ? ? ? ? function show()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? alert('js調用了oc的代碼');

? ? ? ? ? ? }

? ? ? ? ? ? function showTitle()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? alert(document.title);

? ? ? ? ? ? }

? ? ? ? ? ? function showTitleMessage(message)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? alert(message);

? ? ? ? ? ? }

? ? ? ? ? ? function repost()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? location.href = "http://www.iosxxx.com";

? ? ? ? ? ? }

? ? ? ? ? ? function sum()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return 1 + 1;

? ? ? ? ? ? }

? ? ? ? ? ? function btnClick()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? bdgt("hello world");

<!--xmg://sendMessageWithNumber_andContent_?10086&love-->

? ? ? ? ? ? ? ? location.href = "xmg://callWithNumber_?15830654880";

? ? ? ? ? ? }

? ? ? ? </script>

? ? </head>

? ? <!--網頁具體內容-->

? ? <body>

? ? ? ? <br>下面是網頁</br><br/>

? ? ? ? <button style = "background: yellow; height: 150px; width: 350px;" onclick = "btnClick();">點擊按鈕js調用oc</button>

? ? </body>

?

</html>

?

解析oc調用js的方法

知道的這么一種方法如下:

[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"showTitleMessage('%@')",@"oc調用了js的內容"]];

對應

function showTitleMessage(message)

或者

[self.webView stringByEvaluatingJavaScriptFromString:@"show();"];

對應

function show()? ? ? ? ? ?

這個方法都對應js的相應的方法.如上

使用oc調動js 注意寫法如:

[NSString stringWithFormat:@"showTitleMessage('%@')",@"oc調用了js的內容"]不能寫成

[NSString stringWithFormat:@"showTitleMessage(%@)",@"oc調用了js的內容"],否則調不起.

?

js調用 ?oc 的方法

這個主要是指的點擊網頁上的特定的方法調用原生的的特定方法 這個有一種簡單粗暴地方法,不懂得名字, 就是直接吊起的如

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

{

? ? if ([[NSString stringWithFormat:@"%@",request.URL] isEqualToString:@"特定的連接"]) {

? ? ? ? NSLog(@"吊起oc的方法");

? ? }

? ? return NO;//不加載網頁

? ? return YES;//加載網頁

}

?

直接在點擊網頁的某個方法加載網頁之前,在代理中拿到將要加載的網頁,去做相應的判斷.最早就是這樣做,也有局限性吧,但是這樣感覺比單純的js調用原生效率要高點.

?

?

接下來言歸正傳介紹正宗的js如何調用oc

如下核心方法也就這么點

JSContext *content = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

? ? content[@"bdgt"] = ^() {

? ? ? ? NSLog(@"js調用oc---------begin--------");

? ? ? ? NSArray *thisArr = [JSContext currentArguments];

? ? ? ? for (JSValue *jsValue in thisArr) {

? ? ? ? ? ? NSLog(@"=======%@",jsValue);

? ? ? ? }

? ? ? ? //JSValue *this = [JSContext currentThis];

? ? ? ? //NSLog(@"this: %@",this);

? ? ? ? NSLog(@"js調用oc---------The End-------");

? ? ? ? [self.webView stringByEvaluatingJavaScriptFromString:@"show();"];

? ? };

?本文摘自iOS攻城獅:http://www.cnblogs.com/godlovexq/p/5691942.html

轉載于:https://www.cnblogs.com/pengoeng/p/6073179.html

總結

以上是生活随笔為你收集整理的JS与OC中的方法相互调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。