Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchKit : 'sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead

Tags:

ios

watchkit

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.

like image 518
Danger Veger Avatar asked Mar 26 '15 20:03

Danger Veger


1 Answers

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
like image 109
msk Avatar answered Sep 28 '22 15:09

msk