Search code examples
iosuikitswiftuicombineshake

Setting EnvironmentObject from a Shake Gesture (using UIKit) in SwiftUI doesn't update View


I am looking to trigger a SwiftUI function by physically shaking the device. Since motion detection is not a capability of SwiftUI at this time, I need to use UIKit integration along with a coordinator to return a Bool value which will indicate that the device has been shaken, thereby triggering the SwiftUI function.

The following code all works great in recognizing the shaking of the device (this is proven by the separate “makeShakerSound()” function which plays a soundclip upon being shaken and it works great). Besides the working code to recognize the shaking, I also included the code used to call ShakableViewRepresentable(isShaken: $shakeOccurred) from ContentView, below.

I created an EnvironmentObject to flag that the device has been shaken, and used objectWillChange to announce that a change has occurred.

My problem is this: When the shaking motion is detected, the shaking sound effect works great, but my ContentView is not updated for the change in the environmental object myDevice.isShaken. I thought using objectWillChange might take care of this, but it doesn't. What am I missing?

My apologies - I'm a bit new at this.

/* CREATE AN ENVIRONMENTAL OBJECT TO INDICATE DEVICE HAS BEEN SHAKEN */

import Combine
import SwiftUI

class MyDevice: ObservableObject {
    // let objectWillChange = ObservableObjectPublisher()
    var isShaken: Bool = false {
        willSet {
            self.objectWillChange.send()
        }
    }
}


/* DETECT SHAKE GESTURE AND FLAG THAT SHAKING HAS OCCURRED */

import UIKit
import SwiftUI

struct ShakableViewRepresentable: UIViewControllerRepresentable {

    class Coordinator: NSObject {
        var parent: ShakableViewRepresentable
        init(_ parent: ShakableViewRepresentable) {
            self.parent = parent
        }
    }

    func makeCoordinator() -> ShakableViewRepresentable.Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> ShakableViewController {
        ShakableViewController()
    }
    func updateUIViewController(_ uiViewController: ShakableViewController, context: Context) {}
}

class ShakableViewController: UIViewController {

    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        guard motion == .motionShake else { return }

         /* SHAKING GESTURE WAS DETECTED */
        myDevice?.isShaken = true       /* ContentView doesn't update! */
        makeShakerSound()       /* This works great */

        /* I’M TRYING TO TRIGGER A FUNCTION IN SWIFTUI BY SHAKING THE DEVICE:  Despite setting the myDevice.isShaken environment object to "true", my ContentView doesn't update when the shaking gesture is detected.   */

    }
}


ContentView.swift

    @EnvironmentObject var myDevice: MyDevice

    var body: some View {
        NavigationView {

            ZStack {

                /* DETECT SHAKE GESTURE - This works */
                ShakableViewRepresentable()
                    .allowsHitTesting(false)

                VStack {
                    /* SHOW CURRENT STATE OF myDevice.isShaken - Doesn't update */
                    Text(self.myDevice.isShaken ? "The device has been shaken" : "No shaking has occurred")
                    Text("Device was shaken: \(self.myDevice.isShaken.description)")
                }

/* more views below */

Solution

  • example

    import SwiftUI
    import Combine
    
    class MyDevice: ObservableObject {
        //let objectWillChange = ObservableObjectPublisher()
        var isShaken: Bool = false {
            willSet {
                self.objectWillChange.send()
            }
        }
    }
    struct ContentView: View {
        @ObservedObject var model = MyDevice()
        var body: some View {
            VStack {
                Text("\(model.isShaken.description)").onTapGesture {
                    self.model.isShaken.toggle()
                }
    
            }
        }
    }
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    just uncomment the line

    let objectWillChange = ObservableObjectPublisher()
    

    and it stop working.

    DON'T REDEFINE YOUR OWN ObservableObjectPublisher()

    Checked

    import SwiftUI
    import Combine
    
    class MyDevice: ObservableObject {
        //let objectWillChange = ObservableObjectPublisher()
        var isShaken: Bool = false {
            willSet {
                self.objectWillChange.send()
            }
        }
    }
    struct ContentView: View {
        @EnvironmentObject var model: MyDevice
        var body: some View {
            VStack {
                Text("\(model.isShaken.description)").onTapGesture {
                    self.model.isShaken.toggle()
                }
    
            }
        }
    }
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView().environmentObject(MyDevice())
        }
    }
    

    in SceneDelegate.swift

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
            // Create the SwiftUI view that provides the window contents.
            let contentView = ContentView().environmentObject(MyDevice())
            //let contentView = ContentView()
    
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                window.rootViewController = UIHostingController(rootView: contentView)
                self.window = window
                window.makeKeyAndVisible()
            }
        }
    

    it works as expected.

    OK, with your shake detector,

    myDevice?.isShaken = true
    

    will never run, just because myDevice is nil

    The solution

    Make your model globally available, because in SwiftUI we don't have instrument how to make it available for ShakableViewController, or use it as a parameter of its init

    SwiftDelegate.swift

    import UIKit
    import SwiftUI
    
    let model = MyDevice()
    
    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
    
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
            // Create the SwiftUI view that provides the window contents.
            let contentView = ContentView().environmentObject(model)
            //let contentView = ContentView()
    
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                window.rootViewController = UIHostingController(rootView: contentView)
                self.window = window
                window.makeKeyAndVisible()
            }
        }
    
        func sceneDidDisconnect(_ scene: UIScene) {
            // Called as the scene is being released by the system.
            // This occurs shortly after the scene enters the background, or when its session is discarded.
            // Release any resources associated with this scene that can be re-created the next time the scene connects.
            // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
        }
    
        func sceneDidBecomeActive(_ scene: UIScene) {
            // Called when the scene has moved from an inactive state to an active state.
            // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
        }
    
        func sceneWillResignActive(_ scene: UIScene) {
            // Called when the scene will move from an active state to an inactive state.
            // This may occur due to temporary interruptions (ex. an incoming phone call).
        }
    
        func sceneWillEnterForeground(_ scene: UIScene) {
            // Called as the scene transitions from the background to the foreground.
            // Use this method to undo the changes made on entering the background.
        }
    
        func sceneDidEnterBackground(_ scene: UIScene) {
            // Called as the scene transitions from the foreground to the background.
            // Use this method to save data, release shared resources, and store enough scene-specific state information
            // to restore the scene back to its current state.
        }
    
    
    }
    

    ContentView.swift

    import SwiftUI
    import Combine
    
    struct ShakableViewRepresentable: UIViewControllerRepresentable {
    
        class Coordinator: NSObject {
            var parent: ShakableViewRepresentable
            init(_ parent: ShakableViewRepresentable) {
                self.parent = parent
            }
        }
    
        func makeCoordinator() -> ShakableViewRepresentable.Coordinator {
            Coordinator(self)
        }
    
        func makeUIViewController(context: Context) -> ShakableViewController {
            ShakableViewController()
        }
        func updateUIViewController(_ uiViewController: ShakableViewController, context: Context) {}
    }
    
    class ShakableViewController: UIViewController {
        override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
            guard motion == .motionShake else { return }
    
            model.isShaken.toggle()
            print(model.isShaken)
    
    
        }
    }
    
    
    class MyDevice: ObservableObject {
        //let objectWillChange = ObservableObjectPublisher()
        var isShaken: Bool = false {
            willSet {
                self.objectWillChange.send()
            }
        }
    }
    struct ContentView: View {
        @EnvironmentObject var model: MyDevice
        var body: some View {
            VStack {
                ShakableViewRepresentable()
                                   .allowsHitTesting(false)
                Text(self.model.isShaken ? "The device has been shaken" : "No shaking has occurred").onTapGesture {
                    self.model.isShaken.toggle()
                }
    
            }
        }
    }
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView().environmentObject(MyDevice())
        }
    }
    

    and it works, at least on my device :-)

    There in no sound, but it print

    true
    false
    true
    false
    true
    

    for every one shake guesture