7

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.

Danger Veger
  • 1,117
  • 1
  • 9
  • 16

5 Answers5

5

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
msk
  • 8,885
  • 6
  • 41
  • 72
  • #ifndef WATCH_KIT_EXTENSION_TARGET [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; #endif ur logic is not working if i do this? – iOSdev Apr 21 '15 at 09:06
  • I also have the same problem when i added the share extention. Need help please – Anita Nagori Jul 13 '17 at 09:48
0

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.

Nicolas S
  • 5,325
  • 3
  • 29
  • 36
0

To avoid compile time errors about the using of [UIApplication sharedApplication] in an app extension you can use my category.

Ruslan Skorb
  • 491
  • 4
  • 5
0

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]];
Korey Hinton
  • 2,532
  • 2
  • 25
  • 24
0

[[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?

Community
  • 1
  • 1
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93