iPhone App 不再有写权限
Posted
技术标签:
【中文标题】iPhone App 不再有写权限【英文标题】:iPhone App no longer has write permissions 【发布时间】:2012-06-20 17:11:49 【问题描述】:我有一个需要能够写入 /var/mobile/Applications/.../Documents 目录的应用程序。这工作正常,但由于某种原因,现在当我运行我的代码时,NSFileManager createFileAtPath 每次都返回 NO。我不知道为什么会发生这种情况,还有其他人吗?我想知道我是否不小心更改了一些目标设置...
感谢您的帮助,
罗宾
注意:这发生在我尝试写入的任何数据(NSString、NSDictionary 等)
NSDictionary *dataToWrite = [[NSDictionary alloc] initWithObjectsAndKeys:@"1234", @"id", @"hfjk", @"password",nil];
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
filePath = [docDirectory stringByAppendingString:@"/sample-file-name"];
NSFileManager *fileMgr;
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:dataToWrite forKey:@"any-key"];
[archiver finishEncoding];
BOOL status = [fileMgr createFileAtPath:path contents:data attributes:nil];
if (status)
NSLog(@"Created file at path: %@",path);
else
NSLog(@"Failed to create file at path: %@",path);
【问题讨论】:
【参考方案1】:不写作,
filePath = [docDirectory stringByAppendingString:@"/sample-file-name"];
请检查一下
filePath = [docDirectory stringByAppendingPathComponent:@"sample-file-name"];
请查看NSFileManager Class Reference
【讨论】:
以上是关于iPhone App 不再有写权限的主要内容,如果未能解决你的问题,请参考以下文章