Hi All,Thanks in advance for taking the time to check this out. I was hoping someone could help me with this problem I'm having trouble getting past.So I'm using SwiftUI to create a standalone watch app on watchOS 6.1, and am trying to get the magnetometer data to read on the screen and update as I move the watch. When I run the app on my watch, it shows the text, but is not updating or changing values from 0.000000.Here is what my code looks like right now.MagnetometerView.swiftimport SwiftUI
struct MagnetometerView: View {
@ObservedObject
var motion: MotionManager
var body: some View {
VStack {
Text("Magnetometer Data")
Text("X: \(motion.x)")
Text("Y: \(motion.y)")
Text("Z: \(motion.z)")
}
}
}
struct MagnetometerView_Previews: PreviewProvider {
static var previews: some View {
MagnetometerView(motion: MotionManager())
}
}MotionManager.swift fileimport Foundation
import Combine
import CoreMotion
class MotionManager: ObservableObject {
private var motionManager: CMMotionManager
@Published
var x: Double = 0.0
@Published
var y: Double = 0.0
@Published
var z: Double = 0.0
init() {
self.motionManager = CMMotionManager()
self.motionManager.magnetometerUpdateInterval = 1/60
self.motionManager.startMagnetometerUpdates(to: .main) { (magnetometerData, error) in
guard error == nil else {
print(error!)
return
}
if let magnetData = magnetometerData {
self.x = magnetData.magneticField.x
self.y = magnetData.magneticField.y
self.z = magnetData.magneticField.z
}
}
}
}When I run the app, it builds and shows the view properly, but the values do not change from 0.Any advice would be greatly appreciated,Thank you in advance!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi everyone,
I used In-App Purchase in Independent watchOS App as following SwiftUI code but I cannot get products. It seems that the 'SKProductsRequestDelegate' and 'SKRequestDelegate' protocol are not called.
Please tell me the ways if you used to use IAP in Independent watchOS App in SwiftUI (watchOS 6.2+). Thanks
(My code worked on iOS app (companion version))
class IAPManager: NSObject, ObservableObject {
static let shared = IAPManager()
let purchasePublisher = PassthroughSubject<(String, Bool), Never>()
var totalRestoredPurchases = 0
var purchaseHandler : ((String,Bool)->Void)? = nil
private override init() {
super.init()
}
func returnProductIDs() -> [String] {
return ["com.sample.package01"]
}
func getProductsV5() {
let productIDs = Set<String>(returnProductIDs())
if #available(watchOSApplicationExtension 6.2, *) {
let request = SKProductsRequest(productIdentifiers: productIDs)
request.delegate = self
request.start()
} else {
// Fallback on earlier versions
print("This feature is not supported!")
}
}
extension IAPManager: SKProductsRequestDelegate, SKRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let badProducts = response.invalidProductIdentifiers
let goodProducts = response.products
if goodProducts.count > 0 {
ProductsDB.shared.items = response.products
print("bon ",ProductsDB.shared.items)
}
print("badProducts ",badProducts)
}
func request(_ request: SKRequest, didFailWithError error: Error) {
print("didFailWithError ",error.localizedDescription)
purchasePublisher.send(("Purchase request failed ",true))
}
func requestDidFinish(_ request: SKRequest) {
print("request did finish")
}
}