0

I've been playing around a bit and while this code works in iOs it won't work on WatchOS

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .onTapGesture {
                print("tapped!")
        }
        .onLongPressGesture {
            print("Long pressed!")
        }
    }
}

When I say it "doesn't work" I mean doing the long press on an Apple Watch doesn't run the code within the closure block.

I'm thinking maybe .onLongPressGesture shouldn't be used with an Apple Watch, and there is some other method, but I can't figure out what it is (my understanding is force touch is being done away with in WatchOS 7.0).


edit: This is the actual code that's not working (it has a lot of methods attached to it, so it's hard to troubleshoot)

    var body: some View {
        Start()
            .background(NavigationLink(destination: RunSelector(), isActive: self.$showRunSelectorList) {EmptyView()})
            .background(NavigationLink(destination: RunningTimer(myIntervals: allIntervals.week1d1), isActive: self.$showRunningTimer) {EmptyView()})
            .buttonStyle(PlainButtonStyle())
            .onTapGesture {
                self.showRunningTimer = true
        }
        .onLongPressGesture {
            print("Long pressed!")
            self.showRunSelectorList = true
        }
        .focusable(true)
        .digitalCrownRotation(self.$crownValue, from: 0.1, through: 5.0, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
        .onAppear() {
            HealthKitSetupAssistant.authorizeHealthKit { (authorized, error) in
                guard authorized else {
                    let baseMessage = "HealthKit Authorization Failed"
                    if let error = error {
                        print("\(baseMessage). Reason: \(error.localizedDescription)")
                    } else {
                        print(baseMessage)
                    }
                    return
                }
                
                print("HealthKit Successfully Authorized.")
            }
        }.onReceive(Just(self.crownValue)) { output in
            if self.crownValue > 0.1 {
                self.showRunSelectorList = true
                self.crownValue = 0.0
            } else {
                print("The value is \(self.crownValue)")
            }
        }
    }
}
yangnom
  • 187
  • 1
  • 10
  • Works fine with Xcode 12 / watchOS 7. You'd probably met Simulator bug when `print` sometimes does not work. Just quit Simulator and relaunch watch app. – Asperi Jul 23 '20 at 03:52
  • this worked, although I swore I tried it so many times after many simulator changes etc., but maybe I should show the actual code that it's not working on – yangnom Jul 23 '20 at 16:47
  • Now I'm certain this problem persists because, although there /is/ a problem with printing to the console sometimes, I've added another method ` WKInterfaceDevice.current().play(WKHapticType.notification)` and it's still not working. – yangnom Jul 26 '20 at 19:47

0 Answers0