Sharing bundled assets between app and extension

后端 未结 5 2018
迷失自我
迷失自我 2021-01-30 23:40

My Photo Sharing extension plans to use the same design assets (for navigation and for adding \'stamps / stickers\' to the photo).

As explained in App Sa

5条回答
  •  感动是毒
    2021-01-31 00:12

    Regarding sharing data:

    If you want to share files between your iOS8 extension and your Containing app, you want to write files to a shared container or use the shared user defaults.

    From Apple's iOS8 current documentation:

    By default, your containing app and its extensions have no direct access to each other’s containers

    You want to create an App Group, and add your containing app and its extensions to this group. This is pretty simple using Xcode 6, here's how to do it in xcode:

    how to create an app group using xcode 6

    In the app group name, input a constant like:

    group.com.bundle.app.soething
    

    This creates a shared container for your containing app and extension, which allows:

    • Share NSUserDefaults:

      [[NSUserDefaults alloc] initWithSuiteName:@"group.com.bundle.app.soething"];
      
    • Share a directory on the filesystem:

      NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.bundle.app.soething"];
      

    EDIT This will print out all the path combinations we have:

    // App group's path
    
    NSURL  *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.test.app"];
    
    NSLog(@"App group: %@", containerURL.path);
    
    
    
    // Bundle path
    
    NSLog (@"Bundle: %@", [[NSBundle mainBundle] bundlePath]);
    
    
    
    // Good old Documents path
    
    NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *path = [Paths objectAtIndex:0];
    
    NSLog(@"Documents: %@ ", path);
    

    EDIT - and for Swift:

    • Share NSUserDefaults:

      var sharedDefaults = NSUserDefaults(suiteName: "group.com.bundle.app.soething")
      
    • Share a directory on the filesystem:

      var containerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.bundle.app.soething")!
      

    I hope this helps you :)

    Resources: Apple's iOS8.0 Extensions Development Guide - https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

    Apple's iOS8.0 NSFileManager Class Reference - https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati

提交回复
热议问题