WatchKit

RSS for tag

Build apps that leverage watchOS features like background tasks, extended runtime sessions, and access to the Digital Crown using WatchKit.

Posts under WatchKit tag

132 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Crash when using SwiftData Access in background triggered from WCSessionDelegate
I'm having a weird issue. I have a watch app which communicates with the phone using a WCSessionDelegate. if the phone app is open everything works fine, but if the app is closed, when the watch sends a message, my app is woken from the background, after which onAppear() is called in my app which causes a swiftData query to run. Calling any swiftdata function from a backgrounded app causes it to immediately crash with the following stack trace. Any ideas what im doing wrong? or a better way to trigger my code instead of onAppear, so it won't be called when my watch wakes my app from the background? .onAppear { reloadData() } private func reloadData() { let fetch = FetchDescriptor<SavedServer>( predicate: nil, sortBy: [.init(\.displayOrder)] ) guard let results = try? modelContext.fetch(fetch) else { self.rows = [] return } self.rows = results } ------------------------------------- Translated Report (Full Report Below) ------------------------------------- Incident Identifier: 057999AF-7840-410E-B3EE-29082C5AED00 CrashReporter Key: 28AF2AA0-4626-9964-9664-36077DAF4E1A Hardware Model: MacBookPro18,2 Process: MC Status [68915] Path: /Users/USER/Library/Developer/CoreSimulator/Devices/C61698BA-C4CA-4DD9-B824-DBF57AC65090/data/Containers/Bundle/Application/A685371C-9174-4CF7-9E99-D573310CC3E5/MC Status.app/MC Status Identifier: com.shemeshapps.MinecraftServerStatus Version: 2.0 (1) Code Type: ARM-64 (Native) Role: Non UI Parent Process: launchd_sim [55432] Coalition: com.apple.CoreSimulator.SimDevice.C61698BA-C4CA-4DD9-B824-DBF57AC65090 [164301] Responsible Process: SimulatorTrampoline [53834] OS Version: macOS 13.4.1 (22F82) Release Type: User Report Version: 104 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Triggered by Thread: 0 Last Exception Backtrace: 0 CoreFoundation 0x18046589c __exceptionPreprocess + 160 1 libobjc.A.dylib 0x18005c09c objc_exception_throw + 56 2 CoreData 0x184989b94 -[NSFetchRequest(_NSInternalMethods) _incrementInUseCounter] + 0 3 CoreData 0x1849aa99c -[NSManagedObjectContext executeRequest:error:] + 164 4 CoreData 0x1848fe250 NSManagedObjectContext.fetch<A>(_:) + 80 5 SwiftData 0x1a89b7ad8 ModelContext.fetch<A>(_:) + 124 6 SwiftData 0x1a89c48c0 dispatch thunk of ModelContext.fetch<A>(_:) + 20 7 MC Status 0x102530ef4 MainAppContentView.reloadData(forceRefresh:) + 752 (MainAppContentView.swift:112) 8 MC Status 0x102533540 closure #2 in MainAppContentView.body.getter + 44 (MainAppContentView.swift:78) 9 SwiftUI 0x108a0b7a0 0x107b8c000 + 15202208 10 SwiftUI 0x108a0b7bc 0x107b8c000 + 15202236 11 SwiftUI 0x108a0b7a0 0x107b8c000 + 15202208 12 SwiftUI 0x108fdce70 0x107b8c000 + 21302896 13 SwiftUI 0x108fd6ec0 0x107b8c000 + 21278400 14 SwiftUI 0x1081edb24 0x107b8c000 + 6691620 15 SwiftUI 0x10928d650 0x107b8c000 + 24122960 16 libdispatch.dylib 0x1801424f4 _dispatch_call_block_and_release + 24 17 libdispatch.dylib 0x180143d3c _dispatch_client_callout + 16 18 libdispatch.dylib 0x180152b24 _dispatch_main_queue_drain + 1272 19 libdispatch.dylib 0x18015261c _dispatch_main_queue_callback_4CF + 40 20 CoreFoundation 0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 21 CoreFoundation 0x1803c08cc __CFRunLoopRun + 1936 22 CoreFoundation 0x1803bfd28 CFRunLoopRunSpecific + 572 23 GraphicsServices 0x189864bc0 GSEventRunModal + 160 24 UIKitCore 0x103b30208 -[UIApplication _run] + 868 25 UIKitCore 0x103b33e80 UIApplicationMain + 124 26 SwiftUI 0x108a10524 0x107b8c000 + 15222052 27 SwiftUI 0x108a103c4 0x107b8c000 + 15221700 28 SwiftUI 0x108722088 0x107b8c000 + 12148872 29 MC Status 0x102506d30 static MCStatusApp.$main() + 40 30 MC Status 0x102506de0 main + 12 (MCStatusApp.swift:12) 31 dyld_sim 0x1028fd558 start_sim + 20 32 dyld 0x1026b1f28 start + 2236 33 ??? 0x3c15800000000000 ??? Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libsystem_kernel.dylib 0x102f1cfa8 __pthread_kill + 8 1 libsystem_pthread.dylib 0x10285712c pthread_kill + 256 2 libsystem_c.dylib 0x1801375ec abort + 104 3 libc++abi.dylib 0x180263c78 abort_message + 128 4 libc++abi.dylib 0x180255198 demangling_terminate_handler() + 300 5 libobjc.A.dylib 0x180037bf0 _objc_terminate() + 124 6 libc++abi.dylib 0x180263150 std::__terminate(void (*)()) + 12 7 libc++abi.dylib 0x180263100 std::terminate() + 52 8 libdispatch.dylib 0x180143d50 _dispatch_client_callout + 36 9 libdispatch.dylib 0x180152b24 _dispatch_main_queue_drain + 1272 10 libdispatch.dylib 0x18015261c _dispatch_main_queue_callback_4CF + 40 11 CoreFoundation 0x1803c61b4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 12 CoreFoundation 0x1803c08cc __CFRunLoopRun + 1936 13 CoreFoundation 0x1803bfd28 CFRunLoopRunSpecific + 572 14 GraphicsServices 0x189864bc0 GSEventRunModal + 160 15 UIKitCore 0x103b30208 -[UIApplication _run] + 868 16 UIKitCore 0x103b33e80 UIApplicationMain + 124 17 SwiftUI 0x108a10524 0x107b8c000 + 15222052 18 SwiftUI 0x108a103c4 0x107b8c000 + 15221700 19 SwiftUI 0x108722088 0x107b8c000 + 12148872 20 MC Status 0x102506d30 static MCStatusApp.$main() + 40 21 MC Status 0x102506de0 main + 12 (MCStatusApp.swift:12) 22 dyld_sim 0x1028fd558 start_sim + 20 23 dyld 0x1026b1f28 start + 2236
1
2
929
Aug ’23
WatchKit sendMessage wakes up iPhone app
According to the docs, calling WatchKit's sendMessage on an Apple Watch app extension wakes up the corresponding iPhone app in the background. This enables the iPhone app to provide the Watch app with data it needs, even if the iPhone app is currently not in the foreground. Unfortunatelly, I cannot find much information about this process. I'd like to know the answer to these questions: Is there a special launchOptions key present when the iPhone app starts up in the background? According to these docs, an empty launchOptions indicates that the user started the app from the home screen - all other startup reasons have their own launchOptions key. However, when testing the wakeup, I get an empty launchOptions. Are there other options that can be used to detect that the iPhone app runs in the background? For example, I expected [UIScreen mainScreen] to be nil because in the background we do not have screens and windows, but there seems to be no difference in mainScreen in the background process. Are there any limits to the background process? For example, is there a limit in how long it can run? Is there a way I can debug such a background process in XCode? Can I tell XCode "wait until the app runs on the iPhone", such that the debugger gets attached as soon as the app runs?
1
0
1.1k
Aug ’23
WatchOS 9 complications with WidgetKit - daily limit.
I am working on an indipendant WatchOS application which received messages from APNS push notifications. There are three kind of messages and each one has it's own widget which represent the current state of the message category. With WatchOS 9, my ClockKit complications are deprecated and I have to migrate to WidgetKit. I did that, but I am having problems with updating the widgets on time. It seems like I am exceeding the widget's daily limit or somehow the OS throttle the updates (they are not instant after the third update in 2 minutes). The main app and the widget extension targets share the data using CoreData. I need to update the widgets every time the database changes. I am currently using WidgetCenter.shared.reloadTimelines(ofKind: ***). I am calling this function when the app is in foreground and thought that the limit should not be a problem, but it looks like it does. Did someone manage to achieve this kind of widgets update?
2
2
1.2k
Aug ’23
Can't Apple Watch App Widget Complication to Update
Hello, So I made a VERY simple watch app that basically tracks drinks for a project. I was going to go with complications, but they seem to be outdated. Lo and behold Widgetkit! I set up a VERY simple widget that shows the drink count in my app, and it works AMAZINGLY well in Xcode using the WidgetCenter.shared.reloadAllTimelines() function. However, on actual hardware, the widget will only update to the new drink count if I lock and unlock the watch. It does update overtime in accordance to the widget timeline, but I'd rather it update while the user is in app (no update budget used) and in real time, so the user doesn't see their drink count from 2 minutes ago after they've already added a drink. Keep in mind this is a WatchOS app, there is no iOS app it's attached to. Is there anyway I can refresh or re-render the widget like how the Lock/unlock watch does? Anyone else having this issue? I see a few options, but they involve creating an iOS app with background enabled.. I'd like to keep this simple and a watchOS app. Thank you
4
1
1.3k
Aug ’23
Xcode 14: WatchKit App doesn't contain any WatchKit Extensions
I am trying to add a watch extension to my existing iOS app. I added a new watch target and I see the new single watch app folder (no extension folder anymore). I also added the watch target as a dependency (embedded content / target dependency) to my app target. I also made sure the bundle identifier of the watch target matches the apps identifier + .watchkitapp But when I try to archive the app and choose validate, I get the error that the watch info.plist is missing the WKWatchKitApp property. So I add it via the Info tab on the watch target. (Which creates a new info.plist file that only contains this single property) When I try again, the build fails with this: Error (Xcode): WatchKit App doesn't contain any WatchKit Extensions. Verify that the value of NSExtensionPointIdentifier in your WatchKit Extension's Info.plist is set to com.apple.watchkit. So I add <key>NSExtension</key> <dict> <key>NSExtensionPointIdentifier</key> <string>com.apple.watchkit</string> </dict> to the watch info.plist but the build error remains. I am a bit lost here, as all questions and tutorials refer to the old Xcode style.
2
0
1.2k
Aug ’23
Is this a good approach for creating this Apple Watch sleep game feature?
I am creating a watch app that keeps track of the amount of time a user has been asleep for and then updates a game using an API in real time if they surpass another user's sleep time. They select who they want to surpass so the app knows the target time they want to sleep for. I know this probably sounds silly, it's not the final iteration of what I want to build but I know I will need this functionality. So I need this watch app to be able to update the game if they surpass the opponent while the user is sleeping. I don't think this is possible without using extended runtime sessions so that it can run in the background and check to see how long the user has been asleep for after the watch locks. As far as I know the extended runtime sessions only last for 30 minutes though. My current plan to deal with this is to reschedule the session using session.start(at: ) I am currently thinking about doing it like this: User selects opponent and goes to bed App gets the amount of time the user needs to sleep for to surpass opponent, "sleepTime" App schedules an extended runtime session to activate sleepTime seconds in the future sleepTime seconds in the future the session activates and the app checks to see how long the user has actually slept since they went to bed. If the user has slept less than the sleepTime needed to surpass the opponent the app will reschedule the extended runtime session for sleepTime - amountSleptSoFar == "sleepRemaining" seconds, in the future. sleepRemaining seconds in the future the session will activate and check to see how long the user has actually slept for again. If the user has slept less than the time needed to surpass the opponent then the session is rescheduled like before If the user has slept more than the time needed to surpass the opponent then the app uses an API to update the game. It is important that the game is updated as soon as the user surpasses an opponent and I don't want to set up a database to store sleep information and then update the game using the database data as soon as the database is updated. Does this seem like an okay approach? And more importantly is it possible to do? From what I've read and implemented so far it seems possible but please let me know if I'm missing something. The one thing I'm unsure about is if I can use session.start(at: ) when the app is not currently open. If I can't reschedule using that then how else could I do what I need to do?
0
0
566
Aug ’23
watchOS SwiftUI application deeplink on push notifications
Hey, I am currently trying to add deeplink handling from tapping on push notifications in my watchOS application with a SwiftUI lifecycle. I already have deeplinking working with several onOpenURL modifiers throughout the app for the iOS version. What I wanted to do, is whenever I receive a push notification I construct an URL and then utilise the onOpenURL view modifier to handle the deeplink. I currently struggle to understand if this is possible at all. I have the following setup @main struct WatchApp: App { @WKApplicationDelegateAdaptor var appDelegate: WatchAppDelegate // MARK: - Body var body: some Scene { WindowGroup { ContentView() } } } class WatchAppDelegate: NSObject, WKApplicationDelegate, ObservableObject { func applicationDidFinishLaunching() { UNUserNotificationCenter.current().delegate = self } } extension WatchAppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { // handle the push notification } } I do receive the the notification as expected in userNotificationCenter(center:, didReceive:) but from there on I didn't find a way bring this information into my app. On iOS I do leverage UIApplication.shared.open(url:) but this is obviously not available on watchOS. What is the official guidance of handling watchOS deep links with SwiftUI lifecycle from tapping of push notifications ? Currently the onOpenURL modifiers are placed on several views, so it would be nice if there is a way to trigger them from a central place. Would be really appreciated if someone knows a way on how to do it :)
0
0
842
Jul ’23
How to launch apple watch app from companion app?
Is there a way to open an app on Apple Watch from the iOS companion app as Maps is doing? When a navigation is started from Maps in iOS, Maps app starts right away on Apple Watch. Notice that it is not the notification which is started on Apple Watch, it's the real Maps which is automatically launched on Apple Watch. Do you know how to reproduce the same behaviour for any apps in watchOS? Best!
2
0
753
Jul ’23
Delay when posting Local notification on Apple Watch
Hi! I have some trouble with local notification delivery on Apple Watch. It can take more than 1 minute to see the first local notification delivered. I made sure to not use a trigger so the notification will be delivered right away possible as specified in the UNNotificationRequest initializer doc. But there is always a delay for notification delivery. Here is how I'm creating the local notification: content.title = "title" content.subtitle = "subtitle" content.body = "body" content.userInfo = "userInfo" content.categoryIdentifier = "categoryIdentifier" content.threadIdentifier = "threadIdentifier" content.sound = UNNotificationSound.default let notifyRequest = UNNotificationRequest(identifier: stringWithUUID(), content: content, trigger: nil) let center = UNUserNotificationCenter.current() center.add(notifyRequest) { ( error: Error?) in if let theError = error { print(theError.localizedDescription) } else { print("Scheduled OK") } } Do you have any idea how to avoid the delay? Best!
0
0
526
Jul ’23
Using the same widget extension for both iOS and WatchOS
Isn't it possible to use the same widgetkit extension for both iOS and WatchOS? In the WWDC's BackyarBirds project from Apple, I can see the widget extension is added to both the multiplatform (including iOS) and the watch targets. I can also see that the SwiftUI code even uses macros to check which platform the code is running on. However, I'm having several issues with adding the same extension to both targets. When I add it to the watch target, I get a build error that I'm trying to embed an application that also builds for iOS and that is not allowed for the watch app. Not sure if any code here is helpful, but this is my widget UI code: struct AkvaWidget: Widget { private let kind = "Akva Widget" var families: [WidgetFamily] { #if os(iOS) return [.accessoryCircular, .accessoryRectangular, .systemSmall] #elseif os(watchOS) return [.accessoryCircular, .accessoryRectangular, .accessoryInline, .accessoryCorner] #endif } var body: some WidgetConfiguration { StaticConfiguration( kind: kind, provider: AkvaSnapshotTimelineProvider() ) { entry in AkvaWidgetView(entry: entry) } .configurationDisplayName("Akva") .description("Keep track of your water intake.") .supportedFamilies(families) } }
1
0
1.2k
Jul ’23