I've added Manager.m (iOS class) in my build phase in the watch kit extension. However, I got the following error
'sharedApplication' is unavailable: not available on iOS (App
Extension) - Use view controller based solutions where appropriate
instead
Here is the line in my Manager.m that is causing the problem.
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
I want to use code in Manager.m, but lines of code like these are causing issues and won't let me run the watch App. Is there a way around this? I don't want to rewrite the whole class just to accommodate watchkit.
Define/Add a macro (e.g. WATCH_KIT_EXTENSION_TARGET) in your watch kit extension's target build settings and use it to selectively build code. E.g. as sharedApplication is not available on iOS extension, so you can code like below
#ifndef WATCH_KIT_EXTENSION_TARGET
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
#endif
Extensions are a different target so they need to have access to the source code, that includes all .m classes it needs to compile.
That means that if your class is being used in both the main app and the extension, you should refactor it so the extension has access only to the behavior/logic that it needs and not all of it. You could manually disable the compiling of code blocks but that will add noise to your code IMO, and make it harder to understand for others.
I would say split it into two or three classes, for a cleaner code. One for the extension and one for the main app, and one shared class if needed. You can use different design patterns to do so, and without any knowledge of your app I cannot recommend one in particular.
To avoid compile time errors about the using of [UIApplication sharedApplication]
in an app extension you can use my category.
If you know that a particular function will only be called from the iPhone app then another option is to pass in the sharedApplication
or its delegate
as a parameter to the method on your Manager.
- (void)doSomething:(UIApplication *)sharedApplication {
[sharedApplication delegate];
}
Then the caller (from somewhere that doesn't have Watch Extension target membership):
[[Manager sharedInstance] doSomething:[UIApplication sharedApplication]];
[[UIApplication sharedApplication] delegate];
is not available in WatchKit, but a parallel that accesses the shared delegate object is now available—see What is the equivalent of UIApplication.sharedApplication().delegate in WatchKit?