I am using following pods in a Share Extension. The complier is keep showing error: sharedApplication' is unavailable: not available on iOS (App Extension) - Use view controller based solutions where appropriate instead. in every library on [UIApplication sharedApplication] line

  1. I installed all libraries in the Share Extension target.

  2. I have four targets including Share-Extension in my project and I have set “AppExtension flag is NO” for each target. Still appear errors.

Target -> Build Settings -> Require Only AppExtension-Safe API -> NO

My Podfile:

platform :ios, '11.2'
def shared_pods
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'

pod 'XMPPFramework', :git => "https://github.com/robbiehanson/XMPPFramework.git", :branch => 'master'

pod 'MBProgressHUD'  

pod 'GoogleMaps'

pod 'GooglePlaces'

pod 'AccountKit'

pod 'Alamofire', '~> 4.5'

pod 'AlamofireImage', '~> 3.3'

pod 'SVProgressHUD'

pod 'SDWebImage', '~> 4.1.1'

pod 'AWSS3', '~> 2.6.0'

pod 'Fabric'

pod 'Crashlytics'

pod 'Firebase/Core'

pod 'Firebase/Messaging'

pod 'ReverseExtension'

end

target ‘Production' do
    shared_pods
end

target ‘Development’ do
    shared_pods
end

target ‘Testing’ do
    shared_pods
end
10

There are 10 best solutions below

0
On

Some Pods can not be used in extensions. I ran into this exact same issue with IQKeyboardManager. https://github.com/hackiftekhar/IQKeyboardManager/issues/410

sharedApplication or shared are not available in the Extension type I was using.

1
On

The problem is that the pod is written in a very early version of Swift (2?), and you are trying to use it under a more recent version. sharedApplication changed to shared quite a long time ago.

You need to obtain an updated version of the pod (if possible).

0
On

You can not use this pods in your target. You have 2 option for this.

First Option : You marked unchecked

Second Option:

You have to remove this pod from your pod file for new target. For instance, you created push notification target and added new pod for this target. But one of them is use .share so, you can got an error.

    target 'NotificationServiceNew' do
    inhibit_all_warnings!
    use_frameworks!
    pod 'Netmera'
    pod 'Alamofire'
    pod 'AlamofireObjectMapper'
end
0
On

Remove Extension check from that files Target membership.

enter image description here

0
On

I don't know why but with deintegrate and install pods again it fixed!

pod deintegrate
pod install
0
On
  1. Check Manage/Edit Schema if there are any old extensions unselect it, use required extension only.

  2. Add these following lines of code in podfile.

    post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'No'
         end
      end
    end
    
0
On

Started hitting this issue after upgrading to Xcode 13 in my Swift Packages. SPM doesn't have the same target membership options as pods and so Uday's answer couldn't quite do it for me. This issue is, in essence, that SPM packages are now (apparently?) being compiled for all available targets regardless if they're actually linked or not. Thus, if your code doesn't work in some place you never intended to use it, you simply cannot build your code.

My solution was to fork the packages and mark the classes that had this issue as being unavailable in extension:

NS_EXTENSION_UNAVAILABLE("xxxx not supported in extensions (deprecated use of UIApplication shared)")
@interface xxxx : UIView
...

Or equivalently in Swift:

@available(iOSApplicationExtension, unavailable)
class xxxx {
...

You can also apply these availability macros to specific functions/variables but it was less invasive for me to just throw it on the entire class since I never intended to use these libraries in an extension anyways.

0
On

UIApplication.shared is not available in the App extension. so, in your podfile, remove the library that uses UIApplication.shared

1
On

I've found a working solution here: https://ahbou.org/post/169786332682/shared-is-unavailable-use-view-controller-based

It sounds like this:

Select the Pods Project on the left menu

Select the Pod that’s incompatible with the extension in targets list

Select Build Settings

Set Require Only App-Extension Safe API to NO

0
On

I don't know if it can be a solution for you. I too had the same problem. I changed the PodFile struct and it fixed. I don't know it was a true way. But it worked for me. Old PodFile:

# Uncomment the next line to define a global platform for your project
platform :ios, '13.0'

target 'MyApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  pod 'SDWebImage'
  pod 'Firebase'
  pod 'Firebase/Core'
  pod 'Firebase/Auth'
  pod 'GoogleSignIn'
  pod 'FirebaseDynamicLinks'
  pod 'OneSignal', '~> 3.11'
  target 'OneSignalNotificationServiceExtension' do
     # Comment the next line if you don't want to use dynamic frameworks
     use_frameworks!
     pod 'OneSignal', '~> 3.11'
  end
end

I wrote my other targets outside of the main app target.

# Uncomment the next line to define a global platform for your project
platform :ios, '13.0'

target 'MyApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  pod 'SDWebImage'
  pod 'Firebase'
  pod 'Firebase/Core'
  pod 'Firebase/Auth'
  pod 'GoogleSignIn'
  pod 'FirebaseDynamicLinks'
  pod 'OneSignal', '~> 3.11'
end

target 'OneSignalNotificationServiceExtension' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  pod 'OneSignal', '~> 3.11'
end

after 'pod install' i fixed this problem.