Apple watch app does not work when iPhone app is in terminated state?
You'll need to handle the request in the iPhone app a bit differently in order for your app to not be killed off by the OS. I've shared a similar answer here: https://stackoverflow.com/a/29848521/3704092
Can I keep my WatchKit App running in the background on the Apple Watch?
Not possible. Because Apple Document for watchkit
clearly mentions that "WatchKit extensions do not support background execution modes,they run only while the user is interacting with the corresponding app on Apple Watch.".
You can refer apple document Link to Apple Doc
Read Communicating Directly with Your Containing iOS App.
Location Services not working in background called by WatchKit
Three things to check and be aware of:
Location Permissions like
[self.locationManager requestAlwaysAuthorization];
are only acknowledged once by the OS. If you have already requested permission, doesn't matter the level, the OS will NOT display a request to the user. The OS will just pass over the request and leave the permission level as is. The only time you can be assured that the OS will display the request to the user is if the[CLLocationManager authorizationStatus]
returnskCLAuthorizationStatusNotDetermined
. In every other case, you must manually request permission by displaying an Alert or other form of UI display. Also note that the OS retains whether or not it already displayed the request, even if you delete your app and reinstall it. So to test, you need to reset your Simulator's Content or your iPhone's Location Privacy.Make sure you have added the plist keys for
NSLocationAlwaysUsageDescription
ANDNSLocationWhenInUseUsageDescription
If you don't add this to your plist, the OS will ignore any Location Permission Requests.If you want to use
requestAlwaysAuthorization
to get location data from the phone (not the watch app extension) while the phone app is in the background, will also require you register for Background Modes Location updates under Project>Target>Capabilities.
UPDATE
Use a background task to give your app time to respond when in the background. Something like this:
-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo))reply{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask __block = [app beginBackgroundTaskWithName:@"watchAppRequest" expirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//make your calls here to your tasks, when finished, send the reply then terminate the background task
//send reply back to watch
reply(replyInfo);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[app endBackgroundTask:bgTask];
bgTask=UIBackgroundTaskInvalid;
});
}
Related Topics
"This Class Is Not Key Value Coding-Compliant" Using Coreimage
Fbsdksharephoto Not Sharing Link Alongside Photo Using Swift
Count Number of Instances of a Class Swift
Override UIgesturerecognizer Touchesbegan
Ambiguous Reference to Member 'Buildblock()'
Using Multiple Hosting Controllers in Swiftui on Watchos
Swift Switch Case Compiler Error
Position of Mouse Click Relative to Scene, Not Window
How to Refresh The Google Map in My Application Using Swift
Using Associatedtype in a Delegate Protocol for a Generic Type
What Was The Reason for Swift Assignment Evaluation to Void
iOS 16 Swiftui List Background
How to Put View on Top of All Other Views in Swiftui
Enum of String Type Vs Struct with Static Constant
Table View Cells Changing Colors When Scrolling Swift