Skip to content

Commit

Permalink
Work around deprectated interface.
Browse files Browse the repository at this point in the history
`.appendingPathComponent()` on `URL` is deprecated as of iOS 18.0, but
the replacement, `.appending(path:directoryHint:)`, is only available as
of iOS 16.0.  The code now builds with the new call if available, and the
old call if not.
  • Loading branch information
todd-spruceid committed Aug 7, 2024
1 parent 1a9e0b9 commit 1586042
Showing 1 changed file with 73 additions and 67 deletions.
140 changes: 73 additions & 67 deletions Sources/MobileSdk/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import SpruceIDMobileSdkRs
// manager is intended to link.

/*
public typealias Key = String
public typealias Value = Data

public enum StorageManagerError: Error {
case InvalidLookupKey
case CouldNotDecryptValue
case StorageFull
case CouldNotMakeKey
case InternalError
}

public protocol StorageManagerInterface: AnyObject {
func add(key: Key, value: Value) throws
func get(key: Key) throws -> Value
func list() -> [Key]
func remove(key: Key) throws
}
*/
public typealias Key = String
public typealias Value = Data

public enum StorageManagerError: Error {
case InvalidLookupKey
case CouldNotDecryptValue
case StorageFull
case CouldNotMakeKey
case InternalError
}

public protocol StorageManagerInterface: AnyObject {
func add(key: Key, value: Value) throws
func get(key: Key) throws -> Value?
func list() throws -> [Key]
func remove(key: Key) throws
}
*/

/// Store and retrieve sensitive data.
class StorageManager: NSObject, StorageManagerInterface {
Expand Down Expand Up @@ -64,7 +64,13 @@ class StorageManager: NSObject, StorageManagerInterface {
return nil
}

let datadir = asdir.appending(path: "\(appname)/sprucekit/datastore/", directoryHint: .isDirectory)
let datadir: URL

if #available(iOS 16.0, *) {
datadir = asdir.appending(path: "\(appname)/sprucekit/datastore/", directoryHint: .isDirectory)
} else {
datadir = asdir.appendingPathComponent("\(appname)/sprucekit/datastore/")
}

if !fm.fileExists(atPath: datadir.path) {
try fm.createDirectory(at: datadir, withIntermediateDirectories: true, attributes: nil)
Expand All @@ -77,11 +83,11 @@ class StorageManager: NSObject, StorageManagerInterface {
}

/// Store a value for a specified key, encrypted in place.
///
///
/// - Parameters:
/// - key: the name of the file
/// - value: the data to store
///
///
/// - Returns: a boolean indicating success

func add(key: Key, value: Value) throws {
Expand Down Expand Up @@ -152,52 +158,52 @@ class StorageManager: NSObject, StorageManagerInterface {
// Check to see if everything works.

/*
func sys_test() {
let key = "test_key"
let value = Data("Some random string of text. 😎".utf8)

do {
try add(key: key, value: value)
} catch {
print("\(classForCoder):\(#function): Failed add() value for key.")
return
}

let keys: [String]

do {
keys = try list()
} catch {
print("\(classForCoder):\(#function): Failed list().")
return
}

print("Keys:")
for k in keys {
print(" \(k)")
}

do {
let payload = try get(key: key)

if !(payload == value) {
print("\(classForCoder):\(#function): Mismatch between stored & retrieved value.")
return
}
} catch {
print("\(classForCoder):\(#function): Failed get() value for key.")
return
}

do {
try remove(key: key)
} catch {
print("\(classForCoder):\(#function): Failed remove() value for key.")
return
}

print("\(classForCoder):\(#function): Completed successfully.")
}
func sys_test() {
let key = "test_key"
let value = Data("Some random string of text. 😎".utf8)

do {
try add(key: key, value: value)
} catch {
print("\(classForCoder):\(#function): Failed add() value for key.")
return
}

let keys: [String]

do {
keys = try list()
} catch {
print("\(classForCoder):\(#function): Failed list().")
return
}

print("Keys:")
for k in keys {
print(" \(k)")
}

do {
let payload = try get(key: key)

if !(payload == value) {
print("\(classForCoder):\(#function): Mismatch between stored & retrieved value.")
return
}
} catch {
print("\(classForCoder):\(#function): Failed get() value for key.")
return
}

do {
try remove(key: key)
} catch {
print("\(classForCoder):\(#function): Failed remove() value for key.")
return
}

print("\(classForCoder):\(#function): Completed successfully.")
}
*/
}

Expand Down

0 comments on commit 1586042

Please sign in to comment.