Skip to content

Instantly share code, notes, and snippets.

@pfandrade
Last active October 22, 2017 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfandrade/21b3959f01fb63bee6a873a574f78f7f to your computer and use it in GitHub Desktop.
Save pfandrade/21b3959f01fb63bee6a873a574f78f7f to your computer and use it in GitHub Desktop.
Getting a readable/writable shared folder on iOS and Simulator
private func sharedFolderURL() -> URL {
if let simulatorSharedDir = ProcessInfo().environment["SIMULATOR_SHARED_RESOURCES_DIRECTORY"] {
// running on the simulator. We'll write to ~/Library/Caches
let simulatorHomeDirURL = URL(fileURLWithPath: simulatorSharedDir)
let cachesDirURL = simulatorHomeDirURL.appendingPathComponent("Library/Caches")
XCTAssertTrue(FileManager.default.isWritableFile(atPath: cachesDirURL.path), "Cannot write to simulator Caches directory")
let sharedFolderURL = cachesDirURL.appendingPathComponent("Secrets")
XCTAssertNoThrow( try FileManager.default.createDirectory(at: sharedFolderURL, withIntermediateDirectories: true, attributes: nil), "Failed to create shared folder \(sharedFolderURL.lastPathComponent) in simulator Caches directory at \(cachesDirURL)")
return sharedFolderURL
}
else {
// running on the device. We'll write to the AppGroup folder
guard let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppGroupName) else {
XCTFail("Failed to get URL for app group \(AppGroupName). Check your entitlements.")
fatalError()
}
return appGroupURL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment