【问题标题】:Objective C: Write a file programmatically to the Main Bundle [duplicate]目标C:以编程方式将文件写入主包[重复]
【发布时间】:2011-06-30 01:51:27
【问题描述】:

可能重复:
Writing into a file objective c

如何创建文件并将其写入捆绑包?我有这个:

//make path to file
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]];
//write file
[textContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

但它会将其写入文档文件夹并在应用程序关闭时被删除。

【问题讨论】:

    标签: cocoa-touch ios file bundle


    【解决方案1】:

    我很确定你可以。不禁止写入资源文件夹。

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"];
    

    但据我所知,根据 Apple 文档,这不是一个好的行为

    【讨论】:

      【解决方案2】:

      您无法在 iOS 上写入主包。作为 App Store 提交过程的一部分,其内容经过加密签名;修改其内容会阻止应用程序运行。

      不应在应用关闭时删除文档。尝试传入NSError 以接收写入时的错误消息。另外,您确定文件正在写入您认为的位置吗?你用这个作为文件名:

      [NSString stringWithFormat:@"usnews.txt", [sharedManager.articleNumber intValue] + 1]
      

      但是你没有 %d 格式说明符,所以这个数字永远不会插入到文件名中。

      【讨论】:

      • 或者没有无用的否定:设备越狱时有可能
      猜你喜欢
      • 2017-02-01
      • 2013-04-19
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 2019-02-12
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多