从 PHP 或 ASP 中的动态数据库 URL 加载 iPhone 上的 NSArray

发布于 2024-09-01 10:49:17 字数 183 浏览 1 评论 0原文

我有一个在线数据库,我希望能够将其加载到我的应用程序中的 NSArray 中。我可以将 arrayWithContentsOfURL 与静态文件一起使用,但我确实需要转到从数据库生成 plist 文件并将其加载到数组中的 url。我可以使用 ASP 或 PHP。我尝试将响应类型设置为“text/xml”,但这没有帮助。

有什么想法吗?

I have a database online that I would like to be able to load into an NSArray in my app. I can use arrayWithContentsOfURL with a static file, but I really need to go to a url that generates a plist file from the database and loading it into the array. I can use ASP or PHP. I tried setting the response type to "text/xml", but that doesn't help.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-09-08 10:49:17

Apple 的 plist 格式不仅仅是任何 xml 模式。您必须在服务器上生成有效的 plist 文件。

例如使用 CFPropertyList

Apple's plist format is not just any xml schema. You have to generate a valid plist file on your server.

Use for example CFPropertyList.

只有影子陪我不离不弃 2024-09-08 10:49:17

最好的办法是使用 json。格式良好,代码非常短,轻量级,快速且易于使用。

例如,使用 php 生成数组,即您想要在 obj-c 代码中获得的内容。

$arr = array("foo" => "bar", "foobar" => "barfoo");

然后将您的数组编码为 json 对象,并将

echo '{"myarray":'.json_encode($arr).'}';

json 框架 复制到您的app 并使用:

NSString *resultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://foo.bar/yourfile.php"]];
NSDictionary *resultJson =  [resultString JSONValue];
NSArray *resultSet = [resultJson objectForKey:@"comments"];

现在你的 iphone 代码中得到了与 php 文件中完全相同的数组。

//编辑:

您可以使用 NSURLRequest 获得更好的性能和其他很酷的东西。一探究竟。

the best thing is using json. well formatted, very short code, lightweight, fast and easy to use.

for example generate your array with php, what you want to get in your obj-c code.

$arr = array("foo" => "bar", "foobar" => "barfoo");

then encode your array to a json object and echo

echo '{"myarray":'.json_encode($arr).'}';

copy the json framework to your app and just use:

NSString *resultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://foo.bar/yourfile.php"]];
NSDictionary *resultJson =  [resultString JSONValue];
NSArray *resultSet = [resultJson objectForKey:@"comments"];

now you got exactly the same array in your iphone code, as in your php file.

//edit:

you can use NSURLRequest for better performance and other cool things. check it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文