Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

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

122 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

scenePhase change on display cadence change
My Watch app is a workout app that has to be careful to minimize CPU load when backgrounded to get below the 15% avg/60 s in order to avoid being terminated by the system. Accordingly I have logic to detect a transition to the background using a .onChange(of: scenePhase) { phase in… } handler which works well for cases where the app is backgrounded - it is able to reduce its CPU load to just 6-9% while backgrounded. But a special case of the change to scenePhase presents a challenge - when an overlay such as the Control Center or a long-look notification presents, there is no transition to .background, only to .inactive. This is a problem because the system nevertheless imposes the background CPU limit on the app while it’s being covered by the overlay, even though the app didn’t get the .background transition on the scenePhase handler. To further complicate matters, it now seems with watchOS 11 that whenever a transition is made in the screen cadence from .live to .seconds, the scenePhase handler is called with a transition to .inactive. Which is a problem because this transition has to be distinguished from the case of the overlay presentation - the cadence change has no CPU usage implications as long as the app remains foregrounded, but the overlay presentation does, and the system will terminate the app if it exceeds 15% avg/60s. My question: how can I distinguish between the two causes of the transition to .inactive for the two cases: 1) when the overlay presents, and 2) when the display shifts to the slow cadence?
0
0
308
Sep ’24
watchOS SwiftUI background task not working
I'm attempting to create a standalone watchOS app that fetches the prayer timings of my local mosque in JSON format via an API call. I want the app to fetch the prayer timings in the background, but only once per day, at the start of the day (when prayer timings change, i.e., midnight). I'm trying to implement this using SwiftUI's backgroundTask modifier as explained in the docs and in this WWDC22 video. I made sure to enable the Background Modes capability, and this is what my app's Info.plist looks like: However, I've been unable to get it to work. I would appreciate any assistance I can get and feedback for improvements I can make, even with the Info.plist if anything is incorrect about it. Thank you! This is what I have so far: // PrayerTimesCompanionApp.swift // PrayerTimesCompanion Watch App import SwiftUI import WatchKit @main struct PrayerTimesCompanion_Watch_AppApp: App { var body: some Scene { WindowGroup { ContentView() } .backgroundTask(.appRefresh("TIMINGS_REFRESH")) { print("Found matching task") scheduleNextBackgroundRefresh() } } } // Schedule the next background refresh func scheduleNextBackgroundRefresh() { let today = Calendar.current.startOfDay(for: .now) if let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today) { WKApplication.shared().scheduleBackgroundRefresh(withPreferredDate: tomorrow, userInfo: "TIMINGS_REFRESH" as NSSecureCoding & NSObjectProtocol) { error in if error != nil { fatalError("*** An error occurred while scheduling the background refresh task. ***") } print("*** Scheduled! ***") } } } // ContentView.swift // PrayerTimesCompanion Watch App import SwiftUI struct ContentView: View { @StateObject var prayerTimeModel = PrayerTimesModel() var body: some View { List { VStack { VStack(spacing: 15) { // Table Header HStack { Text("Prayer") .bold() .frame(maxWidth: .infinity, alignment: .leading) // Align to the left Text("Iqamah") .bold() .frame(maxWidth: .infinity, alignment: .trailing) // Align to the right } .padding() // Table Rows (5 prayers) ForEach(prayerTimeModel.prayerTimes.data.iqamah, id: \.date) { iqamah in rowView(prayer: "Fajr", time: iqamah.fajr) rowView(prayer: "Zuhr", time: iqamah.zuhr) rowView(prayer: "Asr", time: iqamah.asr) rowView(prayer: "Maghrib", time: iqamah.maghrib) rowView(prayer: "Isha", time: iqamah.isha) } } .padding() } .padding() .onAppear { prayerTimeModel.fetch() } } .edgesIgnoringSafeArea(.top) } func rowView(prayer: String, time: String) -> some View { HStack { Text(prayer) .frame(maxWidth: .infinity, alignment: .leading) Text(time) .frame(maxWidth: .infinity, alignment: .trailing) } } } #Preview { ContentView() } // PrayerTimesModel.swift // PrayerTimesCompanion Watch App import Foundation import SwiftUI // Main struct for the response struct PrayerTimesResponse: Codable { let status: String var data: SalahData let message: [String] } // Struct for the data section struct SalahData: Codable { var salah: [Salah] var iqamah: [Iqamah] } // Struct for Salah times struct Salah: Codable { let date: String let hijriDate: String let hijriMonth: String let day: String var fajr: String let sunrise: String var zuhr: String var asr: String var maghrib: String var isha: String enum CodingKeys: String, CodingKey { case date, hijriDate = "hijri_date", hijriMonth = "hijri_month", day, fajr, sunrise, zuhr, asr, maghrib, isha } } // Struct for Iqamah times struct Iqamah: Codable { let date: String var fajr: String var zuhr: String var asr: String var maghrib: String var isha: String let jummah1: String let jummah2: String enum CodingKeys: String, CodingKey { case date, fajr, zuhr, asr, maghrib, isha, jummah1 = "jummah1", jummah2 = "jummah2" } } class PrayerTimesModel: ObservableObject { @Published var prayerTimes: PrayerTimesResponse = PrayerTimesResponse( status: "Unknown", data: SalahData( salah: [Salah(date: "", hijriDate: "", hijriMonth: "", day: "", fajr: "", sunrise: "", zuhr: "", asr: "", maghrib: "", isha: "")], iqamah: [Iqamah(date: "", fajr: "", zuhr: "", asr: "", maghrib: "", isha: "", jummah1: "", jummah2: "")]), message: ["No data available"] ) // fetches the local mosque's prayer timings via an API call func fetch() { guard let url = URL(string: "https://masjidal.com/api/v1/time/range?masjid_id=3OA87VLp") else { return } let task = URLSession.shared.dataTask(with: url) { [self] data, _, error in guard let data = data, error == nil else { return } // Convert to JSON do { var prayerTimes = try JSONDecoder().decode(PrayerTimesResponse.self, from: data) DispatchQueue.main.async { self.prayerTimes = prayerTimes } } catch { print(error) } } task.resume() } }
0
0
525
Sep ’24
What does it mean? Bug description: your app displayed an error message when we used Sign in with Apple and did not proceed.
What does it mean? 2.1.0 Performance: App Completeness Guideline 2.1 - Performance - App Completeness Submissions to App Review, including apps you make available for pre-order, should be final versions with all necessary metadata and fully functional URLs included; placeholder text, empty websites, and other temporary content should be scrubbed before submission. Make sure your app has been tested on-device for bugs and stability before you submit it, and include demo account info (and turn on your back-end service!) if your app includes a login. If you are unable to provide a demo account due to legal or security obligations, you may include a built-in demo mode in lieu of a demo account with prior approval by Apple. Ensure the demo mode exhibits your app’s full features and functionality. We will reject incomplete app bundles and binaries that crash or exhibit obvious technical problems. Issue Description The app exhibited one or more bugs that would negatively impact App Store users. Bug description: your app displayed an error message when we used Sign in with Apple and did not proceed. Review device details: Device type: Apple Watch OS version: watchOS 10.5 Next Steps Test the app on supported devices to identify and resolve bugs and stability issues before submitting for review. If the bug cannot be reproduced, try the following: For new apps, uninstall all previous versions of the app from a device, then install and follow the steps to reproduce. For app updates, install the new version as an update to the previous version, then follow the steps to reproduce. Resources For information about testing apps and preparing them for review, see Testing a Release Build. To learn about troubleshooting networking issues, see Networking Overview.
0
0
517
Sep ’24
Apple Watch Crash - iOS 18
Hello everyone, I really need your help here :-)) iOS App. 17 and up. Widget extensions and watchOS app as well. New build for iOS 18 earlier this week. All platforms works perfect, except the Apple Watch version, which crash constantly on launch. Testing on simulator and testing devices - works perfect. Uploading to TestFlight and running on the same device - crash. The only thing I see in the crash report is StoredLocationBased.get error. It takes me to nowhere inside the project. Opened a DTS to Apple as well. Any help here will be fully appreciated. Thank you so much!
1
0
465
Sep ’24
Watch OS11 My recording play gets paused after a while
Watch OS11 My recording play gets paused when watch I turned down. It was not happening in previous versions. In my app I recorded my recording. And When I play it in my app, it was playing good in debug mode(when Xcode is connected) could not debug. Otherwise, it was automatically paused(when my wrist is down or inactivity time is elapsed) I want it to be continued.
1
0
584
Sep ’24
WKHaptic Issue when headphones are connected
Info watchOS: 11.0 (22R5348a) *Though has been present since watchOS 10. Issue: Other apps playing music cancel out WKHaptics from firing (low volume and no vibrations) Description When another app is playing music (ex: spotify) in the background while using my app, that uses WKHaptics. The WKHaptics vibrations are non existent as long as headphones are connected. When the headphones are disconnected the vibrations return. Test MVP test app >> https://github.com/mazefest/AppleCodeSupportTestApp
1
0
505
Sep ’24
XCode not supporting Apple Watch Series 10 models
I am probably being stupid but I can't find a way to code for the new 42mm/46mm series 10 watches. I have downloaded the XCode RC and from what I remember from previous years with new screen sizes (Ultra, Series 7, Series 4 etc) that usually supports the new watches after they have been announced. However I can't see them in the simulator or as options in storyboards. Do I need to download something else, or do I have to wait for a new version of XCode? Adjusting my app for new screen sizes is always time consuming and the new watches will be released in 10 days so I was hoping to make a start on it now.
2
0
777
Sep ’24
Apple wallet non ui extension not able to access remote passes
I am implementing apple wallet extension. As part of it I have developed UI and Non-UI extension. I have implemented status , pass entries and remote pass entries method to return as per apple standard. My problem is pass entries is showing correct list of cards/passes as it is able to match against phone wallet. But remote pass entries method is not able to access watch app/watch wallet to match the existing passes. Any help is appreciated. When we print passlibrary count it displays the count matching phone wallet. But it ignores watch wallet passes which belongs to our app.
4
0
730
Aug ’24
There seems to be a bug with digitalCrownAccessory
After reading the documentation on .digitalCrownAccessory, I am under the impression that the system should know to show your accessory and hide your accessory according to whether the crown is being rotated. I have noticed this is not the case. I have also tried a few work arounds with no luck. Here is a simplified version of my code. I have recorded a video of the issue I am seeing, but I can't upload it, so I have attached some screenshots instead. Expected result: After rotating the crown, the accessory image should disappear along with the vertical system scroll bar. Actual result: After rotating the crown, the vertical system scroll bar disappears but the accessory image stays behind. Versions: Sonoma 14.6.1 Xcode 16 Beta 5 (and Xcode 15.4) Apple Watch Ultra 2 watchOS 11 (device and simulator) Starting file (main): import SwiftUI @main struct DummyWatch_Watch_AppApp: App { var body: some Scene { WindowGroup { ContentView() } } } ContentView.swift: import SwiftUI struct ContentView: View { @State var crownValue: CGFloat = 0 var body: some View { VStack { Text("Hello, world!") } .focusable() .digitalCrownRotation($crownValue, from: 0, through: 10, by: 0.1, sensitivity: .low, isContinuous: false) .digitalCrownAccessory { Image(systemName: "speaker.fill") } .padding() } } Images: Scrolling: Speaker wont go away:
1
1
496
Feb ’25
Refresh SmartStack Widget in watchOS 11
Hi, I have an app that provides a SmartStack Widget. The content in that widget is dependent on a state inside the application so from time to time the app needs to update the widget state. I tried: WidgetCenter.shared.invalidateConfigurationRecommendations() WidgetCenter.shared.reloadAllTimelines() However this does not have any effect. I only see updates on reboots (device with watchOS 11 B5) Is there any other way or am I doing something wrong?
2
0
890
Aug ’24
Build issue when creating an Apple Watch standalone app archive
Build issue when creating an Apple Watch standalone app archive This is an Apple Watch standalone app. (Without an iPhone app) Even if you create an Apple Watch standalone app as a new project in Xcode, two targets are created: "iPhone target + Apple Watch target". The iPhone target cannot be built or modified. Usually, builds or archives are created with the Apple Watch target. It's been a while since I updated the app, so I tried to fix it, add new features, and update it. When I created an archive, the version kept being created as 1.0 - 1. I entered 1.1 - 8 for the Apple Watch target. In the previous Xcode version, when I clearly modified the version and build version of the Apple Watch target, it was reflected in the archive file. However, in the current Xcode, it is not reflected. Does anyone know how we can fix this issue?
1
0
502
Aug ’24
Integrating Apple Watch Health Data with Vision Pro
Hi everyone, I have a question regarding the integration of Apple Watch and Vision Pro. Is it possible to connect an Apple Watch to Vision Pro to access health data and display it within Vision Pro applications? If so, could you provide some guidance or point me towards relevant resources or APIs that would help in achieving this? Thank you in advance for your assistance!
4
0
759
Aug ’24
Apple Watch Acceleration Limit
Hi! I'm working on an app that records x,y,z accelerometer values and when subjecting the watch to extreme acceleration (a swing) I notice the x, and y acceleration seems to get stuck around ~30G. Is this a hardware limitation of the watch sensors? I have a Apple Watch Series 7. Below is the chart of the acceleration recording session. Appreciate your insights! Joao
3
0
939
Aug ’24
Is it possible to start an extendedRuntimeSession in background
I have a Companion Watch App for my iPhone App and communicate via the messages and applicationContext between the watch and the phone. If I start an Event at my iPhone and my watch is turned on the extendedRuntimeSession starts as it should. But as soon as my Watches Display is off or Im not in the App I can't start the extendedRuntimeSession, even though I receive it trough the applicationContext and I get the following errorMessage when trying to start the session in the background. -[CSLSSessionService startSession:completionWithExpirationDate:]_block_invoke session A76273B7-3E01-4333-920C-0614C1FAC5B0 encountered error Error Domain=com.apple.CarouselServices.SessionErrorDomain Code=12 "app not in required app state" UserInfo={NSLocalizedDescription=app not in required app state} Is Running is true Timer started/continuend -[SPApplicationDelegate appBeginWorkout:]_block_invoke:1334: Asked to start a workout, but WKExtensionDelegate <SwiftUI.ExtensionDelegate: 0x15d7c500> doesn't implement handleWorkoutConfiguration: Extended runtime session invalidated with reason: WKExtendedRuntimeSessionInvalidationReason(rawValue: -1), error: Optional(Error Domain=WKExtendedRuntimeSessionErrorDomain Code=3 "The app must be active and before applicationWillResignActive to start or schedule a WKExtendedRuntimeSession." UserInfo={NSLocalizedDescription=The app must be active and before applicationWillResignActive to start or schedule a WKExtendedRuntimeSession..}) I control and manage my session like this: class WatchViewModel: NSObject, ObservableObject { static let shared = WatchViewModel() var session: WCSession var extendedRuntimeSessionManager: ExtendedRuntimeSessionManager var lastMessage: [String: Any] = ["method": "none"] @Published var iOSIsReachable = false @Published var dataFromMessages = DataFromMessages() @Published var flextailCoordinates: FlextailCoordinates? init(session: WCSession = .default, extendedRuntimeSessionManager: ExtendedRuntimeSessionManager = ExtendedRuntimeSessionManager()) { NSLog("init start") self.session = session self.extendedRuntimeSessionManager = extendedRuntimeSessionManager super.init() self.session.delegate = self session.activate() NSLog("init done") } } class ExtendedRuntimeSessionManager: NSObject, WKExtendedRuntimeSessionDelegate, ObservableObject{ var extendedRuntimeSession: WKExtendedRuntimeSession? @Published var sessionIsActive = false override init() { super.init() setupExtendedRuntimeSession() } func setupExtendedRuntimeSession() { extendedRuntimeSession = WKExtendedRuntimeSession() extendedRuntimeSession?.delegate = self } func startExtendedRuntimeSession() { if sessionIsActive == false{ extendedRuntimeSession?.start() } else{ print("Already running extendedRuntimeSession") } } func stopExtendedRuntimeSession() { extendedRuntimeSession?.invalidate() sessionIsActive = false setupExtendedRuntimeSession() } // WKExtendedRuntimeSessionDelegate methods func extendedRuntimeSessionDidStart(_ extendedRuntimeSession: WKExtendedRuntimeSession) { print("Extended runtime session started") sessionIsActive = true } func extendedRuntimeSessionWillExpire(_ extendedRuntimeSession: WKExtendedRuntimeSession) { print("Extended runtime session will expire soon") } func extendedRuntimeSession(_ extendedRuntimeSession: WKExtendedRuntimeSession, didInvalidateWith reason: WKExtendedRuntimeSessionInvalidationReason, error: Error?) { print("Extended runtime session invalidated with reason: \(reason), error: \(String(describing: error))") sessionIsActive = false setupExtendedRuntimeSession() } } I tried sending it through the applicationContext but that didn't work, even though the message gets received and parsed as I want. The error specifically seems to be in the starting of an extendedRuntimeSession in the background.
1
0
686
Jul ’24
"This app cannot be installed because its integrity could not be verified"
Hi, We developed a app for iPhone and Apple Watch. The app has been tested and it worked well on iPhone SE and Apple Watch Series 6 running earlier OS. We recently upgraded the Apple Watches to Series 9 & watchOS 10.5, and the iOS on the same iPhone is upgraded to 17.4.1. The app can still be built on the iPhone but when I tried to install the app on the new Apple Watch, it won't work and shows "This app cannot be installed because its integrity could not be verified." To make sure it's not a OS issue, I also upgraded the watchOS on the old Apple Watch Series 6 to 10.5 and the app worked. I wonder what cause the app fail on the newer Apple Watch running the same watchOS. Thank you
1
0
1.9k
Jul ’24
Problems with SwiftUI preview and packages
My project has an iOS and WatchOS app. I added a package dependency, which supports both iOS and WatchOS, though I have only added the library to my iOS app. I can compile and run both apps just fine but when I try to use one of the WatchOS app views in Canvas preview I get errors saying different properties defined in the packages files are not available on watchOS. These blocks of code that give errors are wrapped in: "if TARGET_OS_IPHONE". I have selected the Watch Target and a watch device in the run selector as well as in the canvas. Why is this failing and how can I get it working? Thanks
0
0
483
Jul ’24