xcode 从站点到电话获取 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13769413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting XML File from Site to Phone
提问by CodeMoto
Ok. So I have my first ever iPhone app prototyped and it will parse a given XML file. This file is in with all of the other files that make up the app for testing purposes but eventually I want the app to pull the XML data from a web service.
好的。所以我有我的第一个 iPhone 应用程序原型,它会解析给定的 XML 文件。该文件与组成该应用程序的所有其他文件一起用于测试目的,但最终我希望该应用程序从 Web 服务中提取 XML 数据。
So I've written a PHP script that reads data and creates an XML file for me. But how do I go about getting the XML file from the web server to the phone?
所以我写了一个 PHP 脚本来读取数据并为我创建一个 XML 文件。但是如何将 XML 文件从 Web 服务器获取到手机呢?
This is what I have:
这就是我所拥有的:
- An XML file accessible via URL - i.e. //localhost/v/p.xml
- An iOS 6 app that can parse an XML file which is currently loaded in the app (put under the Supporting Files folder in my XCode project).
- 可通过 URL 访问的 XML 文件 - 即 //localhost/v/p.xml
- 一个 iOS 6 应用程序,可以解析当前加载在应用程序中的 XML 文件(放在我的 XCode 项目的 Supporting Files 文件夹下)。
So what's the best way to get the XML file to the phone? Is LazyTableImages something I should be looking at?
那么将 XML 文件传送到手机的最佳方式是什么?LazyTableImages 是我应该看的东西吗?
Should I do this:
我应该这样做吗:
NSURL *url = [[NSURL alloc] initFileURLWithPath:@"http://localhost/v/p.xml"];
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:url];
//NSString *path = [[NSBundle mainBundle] pathForResource:@"profileXML" ofType:@"xml"];
//NSData *xmlData = [[NSData alloc] initWithContentsOfFile:path];
Where to top 2 lines replace the commented out line? Is it that simple?
顶部 2 行在哪里替换注释掉的行?有那么简单吗?
采纳答案by woz
You can use stringWithContentsOfURL:encoding:error:
, documented here:
您可以使用stringWithContentsOfURL:encoding:error:
,这里记录:
For example:
例如:
[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://localhost/v/p.xml"] encoding:NSUTF8StringEncoding error:nil];
Update:
更新:
As Jim says, NSURLConnection
is non-blocking, and it allows you to submit credential if you want to protect your file from public access. If you want to use that, it looks like this:
正如吉姆所说,它NSURLConnection
是非阻塞的,如果您想保护您的文件免受公共访问,它允许您提交凭据。如果你想使用它,它看起来像这样:
NSString *authenticationURL = @"http://foo.bar";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:authenticationURL]];
NSString *escUsername = [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escPassword = [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@", escUsername, escPassword] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
#pragma mark Url connection delegate methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString *responseText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", responseText);
}
回答by Tricertops
Your example should work as expected (1), but will cause the app to freeze for a while on second line. That method downloads the file synchronouslyand this is usually not desired (unless you run it on background queue).
您的示例应该按预期工作(1),但会导致应用程序在第二行冻结一段时间。该方法同步下载文件,这通常是不需要的(除非您在后台队列上运行它)。
I would use +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
. This is the easiest asynchronousway to download something.
我会使用+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
. 这是下载东西的最简单的异步方式。
Example:
例子:
NSURL *url = [NSURL URLWithString:@"http://localhost/v/p.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// Do something with the `data` unless you have `error`.
}];
(1)I'm just not sure about the way you construct NSURL
. It says initFileURLWith
Path
, but you are definitely not passing path there.
(1)我只是不确定你构造NSURL
. 它说initFileURLWith
Path
,但你绝对不会在那里通过路径。