2009-07-14 9 views
0

J'ai des problèmes pour créer des répertoires et des fichiers avec NSFileManager sur le périphérique iPhone. Mon code, montré ci-dessous, fonctionne bien sur le simulateur, mais pas sur l'appareil, pourriez-vous s'il vous plaît m'aider? Donne-moi des directions où le problème peut être, merci pour chaque réponse ..NSFileManager fonctionne correctement sur le simulateur mais pas sur le périphérique

Je suis d'abord la création de répertoires de cette façon:

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]; 

et je suis en train de créer un nouveau fichier dans ce répertoire ainsi :

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."); 
} 

Il semble qu'il y ait quelque chose de mal avec tout NSFileManager, parce que quand j'utilise fileExistAtPath avec un chemin donné par ce

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

il ne fonctionne pas trop, j'ai essayé de changer de répertoire à NSDocumentsDirectory mais il n'a pas aidé

+0

Vous avez beaucoup de NSStrings. Ne faites pas inutile [[NSString alloc] init]; '. – kennytm

Répondre

1

peut-être parce que l'utilisation de la méthode « createDirectoryAtPath: attrubutes: » est dépréciée par Apple .....

0

Vous pouvez uniquement créer un fichier sous le répertoire sandbox de votre application. Apple ne vous permettra pas de créer un fichier à partir de cela.

Est-ce la raison pour laquelle vous avez échoué sur votre téléphone?

0
[[NSFileManager defaultManager] createDirectoryAtPath:strPath withIntermediateDirectories:YES attributes:nil error:nil]; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *strFile = [documentsDirectory stringByAppendingPathComponent:@"hello/config.plist"]; 
     NSLog(@"strFile: %@", strFile); 

     NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"hello"]; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:strPath]) { 
      NSLog(@"there is no Directory: %@",strPath); 
      [[NSFileManager defaultManager] createDirectoryAtPath:strPath withIntermediateDirectories:YES attributes:nil error:nil]; 
     } 

     NSArray *keys = [NSArray arrayWithObjects: @"username", @"password", @"serverName", @"serverPort", @"autoSave", nil]; 
     NSArray *values = [NSArray arrayWithObjects: @"11", @"11", @"11", @"11", @"11", nil]; 

     NSDictionary *configInfo = [[NSDictionary alloc] initWithObjects: values forKeys:keys]; 
     if (![configInfo writeToFile: strFile atomically: YES]) { 
      NSLog(@"write file error"); 
     } 
Questions connexes