在应用和扩展程序之间共享捆绑资产 [英] Sharing bundled assets between app and extension

查看:165
本文介绍了在应用和扩展程序之间共享捆绑资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的照片分享扩展程序计划使用相同的设计资源(用于导航以及为照片添加图章/贴纸)。


如App Sandbox设计指南中所述,需要共享文件和其他信息的沙盒应用
组可以请求容器
目录作为其权利的一部分。这些目录在〜/ Library / Group Containers /中存储



Apple的iOS8.0 NSFileManager类参考 -

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 Sandbox Design Guide, groups of sandboxed apps that need to share files and other information can request a container directory as part of their entitlements. These directories are stored in ~/Library/Group Containers/. https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati

Currently the assets are bundled in or downloaded and added to the Documents folder. If I want to use the assets in the Photo Sharing extension, would it make sense to put everything in the ~/Library/Group Containers/ and both the container app and the extension get the assets from there?

解决方案

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:

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

这篇关于在应用和扩展程序之间共享捆绑资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆