在我的应用程序中,我需要将一个数组从 Objective C 发送到 javascript。我在网上看到我可以使用这个指令:stringByEvaluatingJavaScriptFromString
,我也这样做了:
目标 C 代码片段
NSMutableArray *names = [[NSMutableArray alloc]init];
NSMutableArray *srcs = [[NSMutableArray alloc]init];
for (int i = 0; i < [site count]; i++) {
NSDictionary *dictData = [site objectAtIndex:i];
[names addObject:[dictData objectForKey:@"name"]];
[srcs addObject:[dictData objectForKey:@"src"]];
}
// UPDATE
NSData *jsonArray = [self arrayToJson:nameSrc];
NSString *jsonString = [[NSString alloc]initWithData:jsonArray encoding:NSUTF8StringEncoding];
NSString *econdedString = [self base64String:jsonString];
NSString *jsCall = [NSString stringWithFormat:@"dataFromObjC(\"%@\")", econdedString];
[self.webView stringByEvaluatingJavaScriptFromString:jsCall];
因此,在 javascript 中,我创建了一个具有此名称的函数dataFromObjC(names, srcs)
,但它没有显示我发出的警报。我将在此处发布我的 html 的完整代码,以便您可以帮助我解决此问题。
HTML 代码
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<title>Lista coupon</title>
<script src="../js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="../js/memoria.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/base64.js"></script>
<script type="text/javascript">
function dataFromObjC(encodedArray) {
var jsonString = Base64.decode(encodedArray);
var arrayFromiOS = JSON.parse(jsonString);
alert(jsonString);
}
</script>
<style type="text/css">
body {
background-color: #000000;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
ul {
list-style-type: none;
padding: 5px;
}
li {
color: #FFFFFF;
font-family: sans-serif;
padding-bottom: 5px;
}
p {
color: #FFFFFF;
font-family: sans-serif;
padding: 5px;
text-align: center;
}
a {
text-decoration: none;
color: #FFFFFF;
}
</style>
</head>
<body onload="loadJson();">
<div id="header">
</div>
<div id="content">
<p>Di seguito trovi tutte le promozioni salvate</p>
<div id="list">
</div>
</div>
<div id="footer">
</div>
</body>
</html>
我希望你能帮助我。
谢谢