Hi,
I am creating an AppExtension that uses ReplayKit. I want to know that ReplayKit was launched in HostApp, so I figured I could record that it was launched in the suite's UserDefaults and KVO that record in HostApp.
Here is the minimal code.
Xcode12.2
iPhone12 Pro
iOS14.2
When delivering in ReplayKit with HostApp running, I expected the labels to change. But nothing changed.
How do I do KVO in the suite UserDefaults?
Thanks for any help.
I am creating an AppExtension that uses ReplayKit. I want to know that ReplayKit was launched in HostApp, so I figured I could record that it was launched in the suite's UserDefaults and KVO that record in HostApp.
Here is the minimal code.
Xcode12.2
iPhone12 Pro
iOS14.2
HostApp Code
Code Block swift import UIKit final class ViewController: UIViewController { @IBOutlet private weak var messageLabel: UILabel! private let userDefaults = UserDefaults(suiteName: "app_group_id") private let key = "key" private var context = 0 override func viewDidLoad() { super.viewDidLoad() self.userDefaults?.addObserver(self, forKeyPath: self.key, options: .new, context: &self.context) } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { guard context == &self.context, let change = change, let message = change[.newKey] as? String else { return } self.messageLabel.text = message } deinit { self.userDefaults?.removeObserver(self, forKeyPath: self.key, context: &self.context) } }
AppExtension Code
Code Block swift import ReplayKit final class SampleHandler: RPBroadcastSampleHandler { private let userDefaults = UserDefaults(suiteName: "app_group_id") private let key = "key" override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) { self.userDefaults?.setValue("Broadcast Started😄", forKey: self.key) } override func broadcastFinished() { self.userDefaults?.setValue("Broadcast Finished😴", forKey: self.key) } }
When delivering in ReplayKit with HostApp running, I expected the labels to change. But nothing changed.
How do I do KVO in the suite UserDefaults?
Thanks for any help.