Skip to content

Commit

Permalink
Merge pull request #129 from msmollin/app-group-support
Browse files Browse the repository at this point in the history
Support app groups
  • Loading branch information
Bryan Clark authored Jan 13, 2021
2 parents 87d01f1 + c684bab commit 72e9519
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion SwiftTweaks.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pod::Spec.new do |s|
s.authors = "Khan Academy", "Bryan Clark"
s.social_media_url = "https://twitter.com/khanacademy"

s.platform = :ios, "8.0"
s.platform = :ios, "9.0"

s.source = { :git => "https://github.com/Khan/SwiftTweaks.git", :tag => "v4.0.2", :submodules => true }
s.source_files = "SwiftTweaks/**/*.swift"
Expand Down
18 changes: 12 additions & 6 deletions SwiftTweaks/TweakPersistency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ internal final class TweakPersistency {

private var tweakCache: TweakCache = [:]

init(identifier: String) {
self.diskPersistency = TweakDiskPersistency(identifier: identifier)
init(identifier: String, appGroup: String?) {
self.diskPersistency = TweakDiskPersistency(identifier: identifier, appGroup: appGroup)
self.tweakCache = self.diskPersistency.loadFromDisk()
}

Expand Down Expand Up @@ -62,8 +62,14 @@ internal final class TweakPersistency {
private final class TweakDiskPersistency {
private let fileURL: URL

private static func fileURLForIdentifier(_ identifier: String) -> URL {
return try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
private static func fileURLForIdentifier(_ identifier: String, appGroup: String?) -> URL {
guard let appGroupName = appGroup else {
return try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("SwiftTweaks")
.appendingPathComponent("\(identifier)")
.appendingPathExtension("db")
}
return FileManager().containerURL(forSecurityApplicationGroupIdentifier: appGroupName)!
.appendingPathComponent("SwiftTweaks")
.appendingPathComponent("\(identifier)")
.appendingPathExtension("db")
Expand All @@ -73,11 +79,11 @@ private final class TweakDiskPersistency {

private static let dataClassName = "TweakDiskPersistency.Data"

init(identifier: String) {
init(identifier: String, appGroup: String?) {
NSKeyedUnarchiver.setClass(TweakDiskPersistency.Data.self, forClassName: TweakDiskPersistency.dataClassName)
NSKeyedArchiver.setClassName(TweakDiskPersistency.dataClassName, for: TweakDiskPersistency.Data.self)

self.fileURL = TweakDiskPersistency.fileURLForIdentifier(identifier)
self.fileURL = TweakDiskPersistency.fileURLForIdentifier(identifier, appGroup: appGroup)
self.ensureDirectoryExists()
}

Expand Down
4 changes: 2 additions & 2 deletions SwiftTweaks/TweakStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public final class TweakStore {

/// Creates a TweakStore, with information persisted on-disk.
/// If you want to have multiple TweakStores in your app, you can pass in a unique storeName to keep it separate from others on disk.
public init(tweaks: [TweakClusterType], storeName: String = "Tweaks", enabled: Bool) {
self.persistence = TweakPersistency(identifier: storeName)
public init(tweaks: [TweakClusterType], storeName: String = "Tweaks", enabled: Bool, appGroup: String? = nil) {
self.persistence = TweakPersistency(identifier: storeName, appGroup: appGroup)
self.storeName = storeName
self.enabled = enabled
self.allTweaks = Set(tweaks.reduce([]) { $0 + $1.tweakCluster })
Expand Down

0 comments on commit 72e9519

Please sign in to comment.