gpt4 book ai didi

javascript - 调用 Objective-C 函数时将值返回给 JavaScript

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:25 24 4
gpt4 key购买 nike

我一直在使用 WebViewJavascriptBridge在 iOS 应用程序中桥接 Objective-C 和 JavaScript。它工作得很好,但它不支持从被调用的 native Objective-C 函数返回值到调用 JavaScript 代码。

我很确定 Cordova (PhoneGap) 通过回调以某种方式做到了这一点,但我一直很难理解底层机制的工作原理。

有没有人遇到同样的问题并设法找到解决方案?

最佳答案

现在,我从未使用过 WebViewJavascriptBridge,但我已经在 objective-c 中使用一个简单的委托(delegate)完成了此操作,所以也许这会对您有所帮助:

MyScript.js

// requestFromObjc
// functionName (required):
// the name of the function to pass along to objc
// returnedResult (not used by caller):
// the result given by objc to be passed along
// callback (not used by caller):
// sent by objc, name of the function to execute
function requestFromObjc(functionName, objcResult, callback)
{
if (!objcResult)
{
window.location = "myapp://objcRequest?function=" + functionName + "&callback=" + arguments.callee.name + "&callbackFunc=" + arguments.callee.caller.name;
}
else
{
window[callback](objcResult);
}
}

function buttonClick(objcResult)
{
if (!objcResult)
{
// ask for the color from objc
requestFromObjc("buttonColor&someParam=1");
}
else
{
// do something with result (in this case, set the background color of my div)
var div = document.getElementById("someDiv");

div.style.background = objcResult;
}
}

MyPage.html

<html>
<head>
<script type="text/javascript" src="MyScript.js"></script>
</head>
<body>
<div id="someDiv">
This is my div! Do not make fun of it, though.
</div>

<button onClick="buttonClick(undefined)">
Click Me!
</button>
</body>
</html>

ViewController.m

-(NSString *) javaScriptResultForRequest:(NSDictionary *) params
{
if ([params[@"function"] isEqualToString:@"buttonColor"])
{
NSArray *colors = @[ @"red", @"yellow", @"blue", @"green", @"purple" ];
return colors[arc4random_uniform(colors.count)];
}
else
{
return @"undefined";
}
}

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] scheme] isEqualToString:@"myapp"])
{
// parse the URL here
NSURL *URL = [request URL];

if ([URL.host isEqualToString:@"objcRequest"])
{
NSMutableDictionary *queryParams = [NSMutableDictionary dictionary];
NSArray *split = [URL.query componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"&="]];

for (int i = 0; i < split.count; i += 2)
{
queryParams[split[i]] = split[i + 1];
}

NSString *result = [self javaScriptResultForRequest:queryParams];
NSString *jsRequest = [NSString stringWithFormat:@"%@(\"%@\", \"%@\", \"%@\")", queryParams[@"callback"], queryParams[@"function"], result, queryParams[@"callbackFunc"]];

// now we send this to the target
[self.webView stringByEvaluatingJavaScriptFromString:jsRequest];
return NO;
}
}

return YES;
}

显然,这比尝试在纯 JS 中执行等效功能要慢得多,因为它必须跳过所有循环。但是,如果您需要在 JS 代码中使用的某些内容已经在您的 ObjC 代码中,那么这可能适合您。

关于javascript - 调用 Objective-C 函数时将值返回给 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com