UIApplication.sharedApplication not available
Asked Answered
B

4

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://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKnMxcF_AZVLQamyA/mp2qs.png When I do the same thing in a playground it works fine. What am I missing?

Basaltware answered 11/12, 2015 at 13:56 Comment(10)
Is this in an app extension or a standard app? Does your app contain app extensions?Supernova
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.Basaltware
Check to make sure that the file giving the error hasn't been associated with the extension target by mistake - see #32610276Supernova
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.Basaltware
Hmm. Well for some reason Xcode thinks that this class is associated with your app extension which is why it is giving this error.Supernova
Yeah, don't know where else to check. Could it be in the Schema or Build Settings? Any hint is appreciated.Basaltware
@MuhammadWaqasBhati: Please don't add noise like "Thanks" to questions.Sensorimotor
Have you tried cleaning your project and/or restarting Xcode? Xcode sometimes gets a bit lost...Submediant
Yes, I did. Same result.Basaltware
Details understanding refer this link #32031571Feld
B
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.

Basaltware answered 11/12, 2015 at 15:37 Comment(7)
Error is removed but default share view is not present.Memory
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.Moneymaking
Hi i m having the same issue but it could not solve the issue even after applied to your solutionOutgroup
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 issueOutgroup
The Apple store will probably reject your app if you have an app extension and set the requirement to No.Olivette
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.Jd
good job, you saved me, i added oneSignalNotificationServiceExtension and all pods generate this errorBouton
S
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.

Secretarial answered 19/8, 2019 at 23:15 Comment(1)
I have the same thing happen to me. The third party notification platform in question was OneSignal.Elfrieda
B
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.

Beelzebub answered 23/2, 2019 at 18:53 Comment(0)
M
-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.

Malchus answered 5/6, 2021 at 7:33 Comment(1)
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.Nauru

© 2022 - 2024 — McMap. All rights reserved.