Create directory in app group container swift
containerURLForSecurityApplicationGroupIdentifier
returns the URL to the group container.
To create a directory append the new directory name as path component
let fileManager = NSFileManager.defaultManager()
if let directory = fileManager.containerURLForSecurityApplicationGroupIdentifier("APP_GROUP_IDENTIFIER") {
let newDirectory = directory.URLByAppendingPathComponent("MyDirectory")
try? fileManager.createDirectoryAtURL(newDirectory, withIntermediateDirectories: false, attributes: nil)
}
Swift 3:
let fileManager = FileManager.default
if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
let newDirectory = directory.appendingPathComponent("MyDirectory")
try? fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
}
how can I create and access share app group document directory?
Solved
let fileManager = FileManager.default
let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.companyname.appname")?.appendingPathComponent("logo.png")
// store image in group container
if !fileManager.fileExists(atPath: url!.path) {
let image = UIImage(data: data)
let imageData : Data = UIImagePNGRepresentation(image!) as! Data
fileManager.createFile(atPath: url!.path as String, contents: imageData, attributes: nil)
}
//ACCESS THIS or get store image
let Image1 = UIImage(contentsOfFile: (url?.path)!)!
How is it possible to create directories inside your application directory in iOS?
You can try creating a folder inside the .documentDirectory
like following.
do {
let fileManager = FileManager.default
let documentsDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let someFolderURL = documentsDirectory.appendingPathComponent("Some")
try fileManager.createDirectory(at: someFolderURL, withIntermediateDirectories: true, attributes: nil)
print(someFolderURL)
} catch {
print(error)
}
Documents directory URL has this format -
"file:///var/mobile/Containers/Data/Application/FB12D808-9054-4799-9C69-115ACD805A24/Documents/"
how to assign an ios swift project to a specific App Group
View your target in Xcode (usually by clicking on the top level element in the project navigator)
Go to "Signing and Capabilities"
Click the "+ Capability" button.
Choose "App Groups"
Add the appropriate identifiers.
How to find Apple App Group shared directory
EDIT on 2018-10-12: Updated code for Swift 4.x (Xcode 10). (Older version retained for reference.)
In Swift 4.x:
let sharedContainerURL :URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.etc.etc")
// replace "group.etc.etc" above with your App Group's identifier
NSLog("sharedContainerURL = \(String(describing: sharedContainerURL))")
if let sourceURL :URL = sharedContainerURL?.appendingPathComponent("store.sqlite") {
if let destinationURL :URL = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("copyOfStore.sqlite") {
try! FileManager().copyItem(at: sourceURL, to: destinationURL)
}
}
In older version of Swift (probably Swift 2.x):
let sharedContainerURL :NSURL? = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.etc.etc") // replace "group.etc.etc" with your App Group's identifier
NSLog("sharedContainerURL = \(sharedContainerURL)")
if let sourceURL :NSURL = sharedContainerURL?.URLByAppendingPathComponent("store.sqlite")
{
if let destinationURL :NSURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0].URLByAppendingPathComponent("copyOfStore.sqlite")
{
try! NSFileManager().copyItemAtURL(sourceURL, toURL: destinationURL)
}
}
Something like the above will get a file from the app group's shared container to the app's Documents directory. From there, you could use Xcode > Window > Devices to get it to your computer.
You could also use iTunes file sharing to retrieve the file from the app's Documents directory after setting UIFileSharingEnabled
to YES
in the Info.plist
file, but bear in mind that this will expose the directory's contents to the user as well. Should be okay for development/debugging purposes, though.
Access App Groups container directory from test runner
Actually, you can!
Simulator:
Write / duplicate your shared data to this location; it should be accessible to both the UITestRunner and your application. Remember to have a mechanism to ensure this does not make it to production code.
NSString * pathAsString = [NSProcessInfo processInfo].environment[@"SIMULATOR_SHARED_RESOURCES_DIRECTORY"];
Device:
Your UITestRunner should share the same Group Identifier as the app under test. Then you can access the shared resources folder. This does not work on Simulator because the UITestRunner is not signed for simulator targets, and it must be signed and part of the Shared Group in order to access the group.
NSURL *containerURL = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:groupIdentifier];
Paulo Andrade has a nice blog post on how to do this:
https://pfandrade.me/blog/managing-ios-ui-testing-fixtures/
Related Topics
How to Disable Qlpreviewcontroller Print Button
Hide Status Bar and Increase the Height of Uinavigationbar
iOS Fix Search Bar on Top of the Uitableviewcontroller
Getting the Action of Uigesturerecognizer in iOS
Using Wcsession with More Than One Viewcontroller
Odd Property Declaration Syntax Containing Angular Brackets <>
Ciimage Display Mtkview VS Glkview Performance
Ble Device Keep Connect with iPhone When Application Terminate
Using Uisheetpresentationcontroller in Swiftui
Uiapplicationlaunchoptionsremotenotificationkey Not Getting Userinfo
Download PDF and Save to the "Files" in Iphone, Not to the App Data, Swift
Storing [Nsnull Null] Values in Nsuserdefaults, from JSON Serialization, Causes Unwanted Exceptions
Convert Spelled Out Number to Number
iOS - Using Uisearchdisplaycontroller with Uisearchbar That Is Uibarbuttonitem in Uitoolbar
Why Is a Https Nsurlsession Connection Only Challenged Once Per Domain