-2

txt 文件在我的域中,我无法保存到其中,这是我的代码:

NSString *countstring = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.DomainName.com/MyAppName/count.txt"] encoding:NSUTF8StringEncoding error:nil];
        int countInt = [countstring intValue];
        NSLog(@"count string is:%i",countInt);
        int newCount = countInt + 1;
        NSLog(@"new count is: %i",newCount);
        NSString *newString = [NSString stringWithFormat:@"%i",newCount];
        [newString writeToURL:[NSURL URLWithString:@"http://www.DomainName.com/MyAppName/count.txt"] atomically:NO encoding:NSUTF8StringEncoding error:nil];

(我审查了我的域名和应用程序名称)
日志是:

count string is:0
new count is: 1

哪个是对的!但是当我在域上打开文件时它仍然是 0。我该如何修复它?谢谢!

4

3 回答 3

1

您无法从应用程序将某些内容保存到 Internet 上的文件中。您必须使用服务器端代码来更新文件。

尝试使用此处的答案使用 Web 服务更新文件:

iOS:如何执行 HTTP POST 请求?

这还要求您编写某种 Web 服务供您的应用程序调用,这将更新文件。这比您尝试的要复杂得多。

于 2012-11-06T19:38:22.770 回答
1

这不是这样的。您不能写入您在网络上随机找到的这个和那个 URL?通常的方法是使用数据发出 HTTP POST 请求,然后使用一些服务器端脚本来实际更新文件。

于 2012-11-06T19:38:31.487 回答
0

@H2CO3 是正确的,通常的方法是 HTTP 帖子。我还没有尝试过,但是直接将字符串写入文件应该可以,但是您的 URL 必须采用以下形式。file://在您的情况下,它是 http,它不起作用!

于 2012-11-06T19:43:53.243 回答