【问题标题】:iOS, Delete all data saved to device by app using NSFileManageriOS,使用 NSFileManager 删除应用程序保存到设备的所有数据
【发布时间】:2012-03-10 15:17:56
【问题描述】:

我希望我的应用程序删除它保存到设备的所有信息,并且有人告诉我使用 NSFile 管理器执行此操作,所以我想知道执行此操作的代码是什么?

【问题讨论】:

标签: objective-c ios xcode nsfilemanager


【解决方案1】:

简单,

folderPath 是您的文档目录的路径。

   NSString *folderPath;

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];  
        for (NSString *strName in dirContents) {                        
                [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:strName] error:&error];      
        }

希望对你有帮助

【讨论】:

  • 不要忘记在尝试使用错误对象之前检查 removeItemAtPath: 是否真的失败了。至少,您可能会报告比实际更多的错误。 NSError* 错误 = nil;布尔资源; res = [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:strName] error:&error]; if (!res && error) { NSLog(@"oops: %@", error); }
猜你喜欢
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 2014-12-22
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
相关资源
最近更新 更多