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

How to Play Audio File on Apple Watch Physical Speaker
I am writing a watchOS app where I have some audio files that I want to play at various points. I am using AVAudioPlayer. It all works in the simulator and it also works if I have Air Pods connected to my watch via Bluetooth. However I get no sound if there isn't a paired set of earphones. In the case of no earphones I would like the sounds to play from the physical watch speaker. I can't seem to find any documentation on how to cause that to happen. Any hints or tips are appreciated.
0
0
571
Sep ’23
No swimming distances on watchOS 10
For some reason I am not receiving HKQuantityTypeIdentifierDistanceSwimming samples when using the watchOS 10 beta (8). The same code works fine on watchOS 9 but not on watchOS 10. I have tried specifically enabling them in the collection types for the live builder and /or starting a query for them, but neither approach is causing any samples to be returned to the app. Is this a known issue? Has something changed for swimming in watchOS 10? Thanks in advance.
4
0
1.1k
Oct ’23
How can users download an old version of a watch app?
I have just released a new version of my watch app, which requires watchOS 8 and is therefore not usable by series 2 and older watches. Is there any way that users with those watches can get the previous version of the app? The new version of the iOS app runs on their phones but the watchOS app will not run on their watches, so they need to use the old version of both. Thanks in advance.
3
0
1.4k
Sep ’23
Beginner Seeking Guidance on Connecting a WatchOS App to Tesla API and Core Data Best Practices
Hello everyone, I'm a complete beginner when it comes to programming and have been learning Swift for the past 2-3 months. I'm in the process of writing my very first project, an Apple Watch app for Tesla owners. So far, I've managed to complete the UI aspect of the app and have recently begun diving into the coding part. However, I find myself a bit lost when it comes to connecting my app to the unofficial Tesla API. On top of that, I'm also wondering if integrating Core Data is necessary for this kind of project? If anyone could help me by providing a clear roadmap, it would greatly accelerate my research and learning process. Any tips, tutorials, or resources you could point me toward would be immensely helpful. Thanks in advance for your assistance! Best Regards Sasan
0
0
373
Aug ’23
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
928
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
565
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
841
Jul ’23
Title: Issues with NSExtensionMainStoryboard or NSExtensionPrincipalClass in Widget Extensions for iOS 14
I'm working on an app that has the following structure: MyApp MyWidgetExtension MyWatchKitApp -- MyWatchKitAppExtension ---- MyWatchKitAppWidgetExtension Both MyWidgetExtension and MyWatchKitAppWidgetExtension were developed using a shared MyWidgetBundle defined as follows: @main struct MyWidgetBundle : WidgetBundle However, I'm running into an issue when attempting to run this on devices with iOS 14. I get an error stating "App extensions must define either NSExtensionMainStoryboard or NSExtensionPrincipalClass keys in the NSExtension dictionary in their Info.plist." Interestingly, if I remove MyWatchKitAppWidgetExtension, the app installs just fine. But, if I add NSExtensionPrincipalClass or NSExtensionMainStoryboard, when I try to distribute the app to TestFlight, I receive an error stating "Unexpected key NSExtensionPrincipalClass found in extension Info.plist". I'm at a loss as to how to resolve this issue. Does anyone have any suggestions or insights?
2
1
1.1k
May ’24
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
752
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
525
Jul ’23
Migrating to WidgetKit, issues with editing Watch Faces on iPhone
Anyone who has successfully migrated to using WidgetKit for their Apple Watch complications, your help would be greatly appreciated! I've migrated to WidgetKit for my Watch app. On the Apple Watch Face Editor, the new Widget names and options appear correctly. However, on the iPhone Apple Watch app, it offers both WidgetKit and old ClockKit complications... this should not be. I also have found rendering issues where Watch Faces on the main My Watch tab have blank gaps for WidgetKit complications, when selecting the Watch Face they then render (see photos) I've put in a feedback FB12460375 Have tried a full clear and reinstall of my app on both devices, no change. This issue occurs on WatchOS 9.5.2 / iOS 16.5.1 and WatchOS 10 Beta 2 / iOS 17 Beta 2 Any other ideas?
5
0
1k
4w
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
Update Watch Complication with CloudKit
I have an iPhone app the uses CloudKit for Core Data syncing. I also have a companion Apple Watch app that syncs to the same CloudKit records. The watch can be used without the iPhone app as well but they both independently sync to the same CloudKit database. This works reasonably well. As soon as either app is opened, the CloudKit will pull down the latest records. I'd like to add a watch complication that shows the state of the CloudKit records. I supported this by adding a Widget extension, embedded in my watch app. The Core Data database in the watch app is part of an app group, so the complication is able to query the same database. So far so good. I'm running into problems with the complication updating after remote records are changed from the iPhone. Here is what is happening: When records are changed on the iPhone, the data syncs to CloudKit but the watch app is not updated when in the background. This is noticed when you open the app and it takes a second for the view to show the new records. Since Core Data database on the watch is not being updated while in the background, the complication is not being updated at all. Part of my confusion is I assumed CloudKit was meant to make this more seamless with silent push notifications? I have the "Remote Notifications" capability enabled on my WatchApp. It seems that only works while the app is in the foreground though. This seems very limiting when you have a complication that depends on the Watch app's Core Data state. I have a few things to work around this with mixed success: "Wake" the watch app explicitly when records are changed on the iPhone app using WatchConnectivityManager. The hope is that by waking the watch app, it will sync with iCloud. This either isn't working or intermittenly works. It is also not clear to me that by activating the watch app, that it will trigger records to sync. I wish there were a way to ask Core Data to sync explicitly. Whenever the Apple watch app wakes in the last step, ask it to WidgetCenter to reload the widget. This process feels convoluted and seems to negate some of the the benefits of CloudKit to synchronize data which I thought would be baked into this process. Any thoughts on a better approach to all this?
3
1
1.2k
Sep ’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
ClockKit Complications not working with Xcode 14 single-target watchOS app
I’ve created a single-target watchOS app in Xcode 14, but I can’t seem to get ClockKit complications working. I’ve added a CLKComplicationDataSource class to my watch target, and in the Info pane for my target I have set the CLKComplicationPrincipalClass key to MODULE-NAME.ComplicationController I haven’t yet added Complication placeholder images to my Assets.xcassets, but as far as I am aware, that shouldn’t be a problem while I am still testing. However, when I run it on a watchOS simulator, the complications never show up on the watch complications list when adding a complication. All of the tutorials I can find for ClockKit complications reference older two-target WatchKit apps. Do the newer single target apps no longer support ClockKit? If so, how can I make a two-target WatchKit app with Xcode 14? Unfortunately I cannot use WidgetKit for my complications because I need to support watchOS 7 at least, and WidgetKit only supports watchOS 9+ Thanks for your help
3
1
1.6k
Jan ’24
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
Name Intent Complications
Hello, I try to add a couple of Complications to my App. Most of the Complications needs to be Configurable, so I use an IntentConfiguration for them. I use this recommendations function to create my complications.     func recommendations() -> [IntentRecommendation<ConfigurationIntent>] {         var recommendations = [IntentRecommendation<ConfigurationIntent>]()        for vehicle in vehicleStatusList {             let intent = ConfigurationIntent() intent.vehicleItem = VehicleItem(identifier: vehicle.id, display: vehicle.name)             recommendations.append(IntentRecommendation(intent:  intent, description: vehicle.name))         }         return recommendations     } But with this I get the result from the screenshot... How can I change this to get a better User Experience? When I click one item in the List, I get a correct Complication. But it would be nice to have a change to name them correctly... The first in the List is a StaticConfiguration...
2
0
1.2k
Oct ’23
Flutter App with WatchOS - Archive Problem
Hi there, I have a problem archiving a Flutter App containing an Apple WatchOS Target. The WatchOS target is built with the new logic, containing only one app and no Watchkit extension. When I build the app on my iPhone/Simulator everything is working just fine and they can communicate without any problems. The error occurs when I'm trying to upload my built archive to App Store Connect for Testflight testing purposes. It fails with the error Asset validation failed: "Missing Info.plist value. A value for the key “WKApplication“, or “WKWatchKitApp“ if your project has a WatchKit App Extension target, is required in “Runner.app/Watch/MyWatchOSApp Watch App.app“ bundle. For details, see: https://developer.apple.com/documentation/watchkit/creating_independent_watchos_apps/setting_up_a_watchos_project". I tried to fix it by adding a info.plist to the WatchOS target, which isn't created initially. By doing so I can not build the Runner App anymore because it fails with "A WatchKit app within this app is not a valid bundle.". Can anyone help me fix it please? Thank you! Greetings
3
1
2.5k
Oct ’23