2014-09-10 1 views
16

J'utilise le code suivant pour créer un dossier/fichier sous le chemin de conteneur partagé . Ce qui aidera à la fois l'extension de l'application et l'application contenant l'extension peut accéder aux données.iOS8 Impossible de créer un dossier/fichier sous l'emplacement du conteneur partagé du groupe d'applications

Code

pour obtenir l'emplacement conteneur url commun:

+(NSURL*)getSharedContainerURLPath 
{ 
    NSFileManager *fm = [NSFileManager defaultManager]; 

    NSString *appGroupName = APP_EXTENSION_GROUP_NAME; /* For example */ 

    NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName]; 

    return groupContainerURL; 
} 
Code

pour créer un répertoire

+(void)createDirAtSharedContainerPath 
{ 
    NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];  
    NSString *directoryToCreate = @"user_abc"; 
    //basically this is <shared_container_file_path>/user_abc 
    NSString *dirPath = [sharedContainerPathLocation stringByAppendingPathComponent:directoryToCreate]; 

    BOOL isdir; 
    NSError *error = nil; 

    NSFileManager *mgr = [[NSFileManager alloc]init]; 

    if (![mgr fileExistsAtPath:dirPath isDirectory:&isdir]) { //create a dir only that does not exists 
     if (![mgr createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]) { 
       NSLog(@"error while creating dir: %@", error.localizedDescription);     
     } else { 
       NSLog(@"dir was created...."); 
     } 
    } 
} 

le code ci-dessus ne soulever aucune erreur, il dit succès, mais je ne suis pas en mesure de trouver la dossier sous le chemin du conteneur partagé. Toute idée qui pourrait être apprécié

Répondre

29

Je viens de faire mon travail de code en modifiant le code suivant

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString]; 

à

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];  
+1

Une fois que votre groupe d'applications est configuré comme indiqué dans le lien suivant. http://blog.parse.com/announcements/introducing-local-data-sharing-for-apple-watch-and-app-extensions/ – Thiru

+0

Veuillez marquer la question comme acceptée, pour que tout le monde sache que c'était la solution @loganathan –

+0

Ça marche pour moi. Thanx – SPatel

2

Pour Swift

func createProjectDirectoryPath(path:String) -> String 
    { 
     let containerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.abc") 
     let logsPath = containerURL!.URLByAppendingPathComponent(path) 
     //print(logsPath.path); 

     do { 
      try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil) 
     } catch let error as NSError { 
      NSLog("Unable to create directory \(error.debugDescription)") 
     } 
     return logsPath.path! 
    } 

Utilisation

var strSavePath : String = self.createProjectDirectoryPath("Large") 

Remarque: Une fois votre groupe d'applications est configuré ce code ci-dessus est utile pour créer un dossier.

Questions connexes