NSFileManager ทำงานได้ดีบนเครื่องจำลอง แต่ไม่ใช่บนอุปกรณ์

ฉันมีปัญหาในการสร้างไดเรกทอรีและไฟล์ด้วย NSFileManager บนอุปกรณ์ iPhone รหัสของฉันที่แสดงด้านล่างทำงานได้ดีบนเครื่องจำลอง แต่ไม่ใช่บนอุปกรณ์ คุณช่วยฉันหน่อยได้ไหม ขอคำแนะนำว่าปัญหาอาจเกิดจากอะไร ขอบคุณสำหรับทุกคำตอบ..

ฉันกำลังสร้างไดเรกทอรีเป็นครั้งแรกด้วยวิธีนี้:

NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];

strDownloadsDir = [[NSString alloc] init];
strDownloadsDir = [[appPath stringByAppendingPathComponent:@"/Other"] copy];
if(![fileMgr fileExistsAtPath:strDownloadsDir])
    [fileMgr createDirectoryAtPath:strDownloadsDir attributes:nil];

จากนั้นฉันพยายามสร้างไฟล์ใหม่ในไดเร็กทอรีนี้ด้วยวิธีนี้:

NSString *filePath = [strDownloadsDir stringByAppendingPathComponent:strDlFileName];
//Test whether this file exists, if not, create it
NSLog(@"%@", filePath);
if(![fileMgr fileExistsAtPath:filePath])
{
    if([fileMgr createFileAtPath:filePath contents:nil attributes:nil])
        NSLog(@"Creating new file at path %@", filePath);
    else
        NSLog(@"Failed to create new file.");
}

ดูเหมือนว่ามีบางอย่างผิดปกติกับ NSFileManager ทั้งหมด เพราะเมื่อฉันใช้ fileExistAtPath กับเส้นทางที่กำหนดโดยสิ่งนี้

NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];

มันใช้งานไม่ได้เช่นกัน ฉันพยายามเปลี่ยนไดเร็กทอรีเป็น NSDocumentsDirectory แต่ก็ไม่ได้ช่วยอะไร


person Community    schedule 14.07.2009    source แหล่งที่มา
comment
คุณกำลังรั่ว NSStrings จำนวนมาก อย่าทำสิ่งที่ไม่จำเป็น [[NSString alloc] init];   -  person kennytm    schedule 11.01.2010


คำตอบ (2)


อาจเป็นเพราะการใช้เมธอด " createDirectoryAtPath: attrubutes: " ถูกเลิกใช้งานโดย apple.....

person jAmi    schedule 11.01.2010

คุณสามารถสร้างไฟล์ได้เฉพาะในไดเร็กทอรีแซนด์บ็อกซ์ของแอปพลิเคชันของคุณเท่านั้น Apple จะไม่อนุญาตให้คุณสร้างไฟล์จากนั้น

นั่นคือเหตุผลที่คุณล้มเหลวในโทรศัพท์ของคุณ?

person Forrest    schedule 29.06.2010