Using Preferences in native plugin?

Is there a native API to CapacitorJS Preferences (doc), so I can get and set values from within a native iOS plugin?

I read that silent push notifications can only trigger a native part of a CapacitorJS iOS app, so I need to move some logic to native.

Yes, you can add the preferences plugin as a dependency to your plugin and use the Preferences class directly from native.
That’s the same class PreferencesPlugin class uses.

@julio-ionic Thanks! Trying to get this to work, but I fail miserably. Too little knowledge about iOS Swift, CapacitorJS and the inter-workings, even multiple AI can’t help.

I want this function to save something to the Preferences:

import UIKit
import Capacitor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // capacitor-plugin-silent-notifications
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // debug
        print("Received by: didReceiveRemoteNotification w/ fetchCompletionHandler")

        // Perform background operation, need to create a plugin
        NotificationCenter.default.post(name: Notification.Name(rawValue: "silentNotificationReceived"), object: nil, userInfo: userInfo)

        // Save notification data using Preferences plugin
        ...

        // Give the listener a few seconds to complete, system allows for 30 - we give 25. The system will kill this after 30 seconds.
        DispatchQueue.main.asyncAfter(deadline: .now() + 25) {
            // Execute after 25 seconds
            completionHandler(.newData)
        }
    }

}

Failed attempts to import:

import @capacitor/preferences
import Preferences

This seems to work:

import CapacitorPreferences

Failed attempts on calling:

        let prefs = Preferences
        let prefs = Preferences.shared
        let prefs = CapacitorPreferences
        let prefs = CapacitorPreferences.shared
        prefs.set("receivedNotification", for: "silentNotification")

What is the right way to import and call another plugin?

There are not that many options for 3 lines of code :rofl:

Check the PreferencesPlugin class as it uses the Preferences object and use it the same way

My challenge is that the PreferencesPlugin is in the same folder. So when Swift is similar to Java, there is probably no import needed for classes in the same folder.

I want to set a value from /ios/App/App/AppDelegate.swift, so I need some kind of import. Not sure what mechanisms CapacitorJS uses with CocoaPods and CapacitorPreferences.

And I don’t want to supply a configuration, as I am assuming Preferences using a Singleton pattern, the configuration should be set by the app already, I don’t want to mess with it.

Currently using a workaround by accessing UserDefaults.standard directly in /ios/App/App/AppDelegate.swift:

import Foundation
private var defaults: UserDefaults {
    return UserDefaults.standard
}
let formatter = ISO8601DateFormatter()
let currentDate = Date()
let isoDateString = formatter.string(from: currentDate)
     
defaults.set("true", forKey: "CapacitorStorage.receivedSilentNotification." + isoDateString)
//print( defaults.dictionaryRepresentation().keys.filter { $0.hasPrefix("CapacitorStorage.silentNotification.") } )

With CapacitorStorage. prefix, the keys/values can be used with the regular Preferences plugin.