24

I'm trying to get a reference to my app delegate from a UIViewController but I keep getting an this error message:

'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.

I've been reading all the articles dealing with this kind of message but my problem seems unrelated as I'm not using any third party framework. While a Today Extension is part of my app, the class in which the error occurs is totally unrelated. I even created a completely new class and keep getting the same error:

https://i.stack.imgur.com/mp2qs.png When I do the same thing in a playground it works fine. What am I missing?

mfaani
  • 33,269
  • 19
  • 164
  • 293
DoertyDoerk
  • 1,215
  • 1
  • 8
  • 19
  • 1
    Is this in an app extension or a standard app? Does your app contain app extensions? – Paulw11 Dec 11 '15 at 14:03
  • 1
    The error occurs in the app itself. The app does have an extension but I'm trying to get the appDel from the app, not from the extension. All classes in which the error occurs have no obvious relation with the extension. – DoertyDoerk Dec 11 '15 at 14:07
  • 1
    Check to make sure that the file giving the error hasn't been associated with the extension target by mistake - see http://stackoverflow.com/questions/32609776/uiapplication-sharedapplication-is-unavailable – Paulw11 Dec 11 '15 at 14:10
  • I did. Both the original file as well as the newly created test class are only associated with the main app but not with the extension. – DoertyDoerk Dec 11 '15 at 14:12
  • Hmm. Well for some reason Xcode thinks that this class is associated with your app extension which is why it is giving this error. – Paulw11 Dec 11 '15 at 14:13
  • 1
    Yeah, don't know where else to check. Could it be in the Schema or Build Settings? Any hint is appreciated. – DoertyDoerk Dec 11 '15 at 14:25
  • 4
    @MuhammadWaqasBhati: Please don't add noise like "Thanks" to questions. – Cerbrus Dec 11 '15 at 14:27
  • Have you tried cleaning your project and/or restarting Xcode? Xcode sometimes gets a bit lost... – jcaron Dec 11 '15 at 14:39
  • Yes, I did. Same result. – DoertyDoerk Dec 11 '15 at 14:50
  • Details understanding refer this link https://stackoverflow.com/questions/32031071/error-sharedapplication-is-unavailable-not-available-on-ios-app-extension/50168964#50168964 – Ashish May 04 '18 at 06:58

4 Answers4

72

Problem solved. I've been to the Build Settings of my app again and stumbled over

Require Only AppExtension-Safe API

all set to YES. The default though is NO. When I set this to NO the error disappeared.

I sure don't remember ever touching or even knowing about this but luckily now it works.

DoertyDoerk
  • 1,215
  • 1
  • 8
  • 19
  • Error is removed but default share view is not present. – Deepak Saki Feb 07 '17 at 07:55
  • 1
    Works! Had the same problem with some cocoa pods. I had to change it for the respective target in the pod project to fix it. Took a while until I figured it out since I first tried to apply it only to my build targets. – Andy Jun 29 '17 at 13:06
  • 2
    Hi i m having the same issue but it could not solve the issue even after applied to your solution – Anita Nagori Jul 13 '17 at 11:04
  • I have alredy a project i have an requirement to add an extention i follow the process as mention in other tutorial but i want a custom layout when some one want to share image using my app extention so when i use the class which i need to include when user tap for sharing contaims the appdelegate instance n i am having the above mention issue. Can you please give me some guild line to resolve the issue – Anita Nagori Jul 13 '17 at 11:08
  • 5
    The Apple store will probably reject your app if you have an app extension and set the requirement to `No`. – Garviel Jul 25 '18 at 07:42
  • In my case, I had to remove the view controller from sources list of the Extension target instead. The extension is supposed to have this flag to YES. But my file is not intended to be part of this target. – Sai Manoj Kumar Yadlapati Mar 12 '20 at 17:01
  • good job, you saved me, i added oneSignalNotificationServiceExtension and all pods generate this error – Sohaib Siddique Dec 25 '20 at 07:27
3

I ran into the same error when I created and added a new target to my project (a Remote Service Notification target) and added the target definition incorrectly in my podfile.

I was following directions from a third party notification platform and they were unclear as to where to place the target definition. Initially, I added the target definition within my main target, much like the Tests target definition. This mistake led me to this error.

I ended up moving the remote service notification target definition outside of my main target to the bottom of my podfile and that resolved my problem.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
drquiz
  • 161
  • 2
  • 6
1

In case anyone came through this error after 3 years from the question time. Make sure that the swift file target Membership doesn't include a Today Extension.

-2

In my case, I have not defined import Foundation in the file causing the error. silly and late but could help an absent-minded like me. Cheers.

Ahsan
  • 173
  • 1
  • 13
  • Not true. You can see in OP's question that they are already importing UIKit, which itself imports Foundation. You don't need to manually import Foundation when you already import UIKit, and doing it anyway would not solve OP's issue because it's unrelated to Foundation. – Eric Aya Jun 05 '21 at 12:18