18

I have installed the facebook sdk via cocoapods but I get the below error:

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

relating to line 701 in FBSDKCoreKit/FBSDKAppEvents.m

UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController;

Why is this happening and how do I resolve it? podfile:

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

 target 'testapp' do
# Comment the next line if you're not using Swift and don't want to use dynamic 

frameworks
  use_frameworks!

  # Pods for testapp

pod 'FLAnimatedImage', '~> 1.0'
pod 'SDWebImage', '~> 4.0'
pod 'FacebookLogin'
pod 'FacebookShare'
pod 'FacebookCore'

 pod 'OneSignal', '>= 2.5.2', '< 3.0'


  target 'testappTests' do
    inherit! :search_paths
    # Pods for testing
  end

target 'OneSignalNotificationServiceExtension' do
  pod 'OneSignal', '>= 2.5.2', '< 3.0'
end

  target 'testappUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end
MattBlack
  • 3,616
  • 7
  • 32
  • 58

6 Answers6

47

You can add the following block to your Podfile to address this programatically.

If you are using pods you can add the following block to your Podfile to address this issue:

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

This will set the require only App-Extension-Safe Api to No for all pod project targets and should allow your builds to succeed.

22

That's what helped me: Pods project -> select Target which contains this error -> Build Settings -> set Require Only App-Extension-Safe API to No

You might probably have to do the same for other FB frameworks

Jack Stroganov
  • 421
  • 4
  • 8
3

Try to comment OneSignalNotificationServiceExtension target & pod , clean , install and run again

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
3

i had the same problem,i solved it by setting require only App-Extension-Safe Api to no it seemed that all the facebook pods require only App-Extension-Safe Api were set to yes so i have to set no for all xcodeproject,target,all facebook pods , you can find require only App-Extension-Safe Api in build setting for pods,target,and project important note when ever you reinstall pods they are reseted to yes

Abdull
  • 53
  • 1
  • 8
0

For the ones who need oneSignal in their project:

The problem is the place the oneSignal pods are added in the podfile. I had the below code in my podfile:


target 'my_app_name' do
  pods()

  target 'my_app_name_dev' do
    pods()
  end

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
    # require for m1 mac
    installer.pods_project.build_configurations.each do |config|
      config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
    end
  end

  target 'OneSignalNotificationServiceExtension' do
    pod 'OneSignalXCFramework', '>= 3.0', '< 4.0'
  end
end

The reason the project was failing to run was I had added the oneSignal lines inside the main project target (my_app_name target) while OneSignalNotificationServiceExtension is a separate target in the project, so I moved it out of my app target pods and it worked:

target 'OneSignalNotificationServiceExtension' do
  pod 'OneSignalXCFramework', '>= 3.0', '< 4.0'
end

target 'my_app_name' do
  pods()

  target 'my_app_name_dev' do
    pods()
  end

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
    # require for m1 mac
    installer.pods_project.build_configurations.each do |config|
      config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
    end
  end
  
end
Mahdieh Shavandi
  • 4,906
  • 32
  • 41
0

If you have already tried other approaches and none of them working and if you are getting this error in your main project and not in your app extension then just make your method unavailable for app extension.

@available(iOSApplicationExtension, unavailable)
    static var getTopSafeArea: CGFloat {
        if let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {
            return window.safeAreaInsets.top
        }
        return 0
    }