将NSArray发送到javascript [英] Send NSArray to javascript

查看:85
本文介绍了将NSArray发送到javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要从Objective C发送一个数组到javascript。我在网上看到我可以使用这个指令: stringByEvaluatingJavaScriptFromString ,我这样做了:

In my app I need to send an array from Objective C to javascript. I read on the web that I can use this instruction: stringByEvaluatingJavaScriptFromString, also i made so:

Objective 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的函数(名称, srcs),但它没有显示我发出的警报。
我会在这里发布我的html的完整代码,以便您可以帮我解决这个问题。

So in javascript I made a function that has this name dataFromObjC(names, srcs), but it doesn't shows me the alert I made. I will post here the complete code of my html so you can help me to solve this problem.

HTML code

<!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>

我希望你能帮助我。

谢谢

推荐答案


  • 首先,将数据编码为JSON字符串而不是一个普通的字符串,然后发送给JS。您可以使用 NSJSONSerialization

    - (NSData *) arrayToJSON:(NSArray *) inputArray
    {
        NSError *error = nil;
        id result = [NSJSONSerialization dataWithJSONObject:inputArray 
                                                    options:kNilOptions error:&error];
        if (error != nil) return nil;
        return result;    
    }
    


  • 使用 stringByEvaluatingJavaScriptFromString将其发送给JS: 。或者,我建议您使用 Base64 对字符串进行编码,以避免出现特殊字符问题。

  • Send it to JS with stringByEvaluatingJavaScriptFromString:. Optionally, I would recommend you to encode the string with Base64, to avoid problems with special characters.

    // Convert your array to JSON data
    NSData *jsonArray = [self arrayToJSON: yourArray];
    // Pass the JSON to an UTF8 string
    NSString *jsonString = [[NSString alloc] initWithData:jsonArray                         
                                                  encoding:NSUTF8StringEncoding];
    // Base64 encode the string to avoid problems
    NSString *encodedString = [Base64 encode:jsonString];
    // Evaluate your JavaScript function with the encoded string as input
    NSString *jsCall = [NSString stringWithFormat:@"yourJsFunction(\"%@\")", encodedString];
    [self.webView stringByEvaluatingJavaScriptFromString:jsCall];
    


  • 进入JS后,将JSON字符串解析为对象(可选,请记住解码它)如果你先编码)。

  • Once in JS, parse the JSON string to an object (optionally, remember to decode it if you encode first).

    function yourJsFunction(encodedArray)
    {
        // Decode and parse the received input
        var jsonString = Base64.decode(encodedArray);
        var arrayFromiOS = JSON.parse(jsonString);
    }
    


  • 更新:关于iOS和JS的Base64实用程序,Internet有很多函数和库的例子,比如这些适用于iOS的 其他 。你可以选择你喜欢的那个。

    UPDATE: About Base64 utilities for iOS and JS, Internet is plenty of examples of functions and libraries, like these ones for iOS and these other ones for JS. You can choose the one you prefer.

    这篇关于将NSArray发送到javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆