简体   繁体   English

iPhone writeToFile不起作用

[英]iPhone writeToFile doesn't work

I have written one application that save txt file into device. 我编写了一个将txt文件保存到设备中的应用程序。 When I test on simulator, I am able to read and write file. 在模拟器上测试时,我可以读写文件。 After I test on device, read is working fine but it doesn't write anything on that text file. 在设备上进行测试后,读取工作正常,但在该文本文件上未写入任何内容。 I have searched for the solution but it seems like it has problem with right access. 我已经搜索了解决方案,但似乎访问权限有问题。 Anyone can suggest me how to make this work? 任何人都可以建议我如何进行这项工作?

What path are you using to save the file to? 您使用什么路径将文件保存到?

I'm using 我正在使用

- (NSString *)dataFilePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return [documentsDirectory stringByAppendingPathComponent:@"myFileNameHere.txt"];

}

and then pass [self dataFilePath]; 然后传递[self dataFilePath]; into the writeToFile method as the path 进入writeToFile方法作为路径

On real device you don't have access to write everywhere. 在真实设备上,您无处无处写。 See "Commonly Used Directories" in Developers Guide - you should write to Documents or Caches directories ( how to get their paths ) 请参阅开发人员指南中的“常用目录” -您应写入Documents或Caches目录( 如何获取其路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM