首页 > 解决方案 > 在应用程序的主目录中保存文件

问题描述

App.app/here.text 下的 txt 文件?目前我发现NSSearchPathForDirectoriesInDomains

 NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] error:NULL];

但它放在文件下。我可以将它保存在 Appname.app/here 下吗

https://www.alphansotech.com/wp-content/uploads/2015/11/iosDirectory.png

我可以在什么级别列出读取的文件?/private/var/containers/Bundle/Application/EF4585FC-743D-409D-83A9-B8A298D44C74/App.app

NSString * path = [[NSBundle mainBundle] resourcePath];
  [path stringByDeletingLastPathComponent];
  NSArray* dirs = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[path stringByDeletingLastPathComponent] error:NULL];
  NSLog(@"sss..%@",path );

  NSLog(@"sss..%@",[path stringByDeletingLastPathComponent]);

  NSString *stringToWrite = @"1\n2\n3\n4";
  NSString *filePath = [path stringByAppendingPathComponent:@"main.jsbundle"];

  [stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  
  [dirs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
      NSLog(@"sss...%@", obj);
  }];

标签: iosxcode

解决方案


长话短说:

不,你不能。由于显而易见的原因,不可能写入应用程序包。


推荐阅读