7

我在共享扩展中使用以下 pod。编译器一直显示错误:sharedApplication' 不可用:在 iOS 上不可用(应用程序扩展) - 在适当的情况下使用基于视图控制器的解决方案。在 [UIApplication sharedApplication] 行的每个库中

  1. 我在共享扩展目标中安装了所有库。

  2. 我的项目中有四个目标,包括 Share-Extension,并且我为每个目标设置了“AppExtension flag is NO”。仍然出现错误。

目标 -> 构建设置 -> 仅需要 AppExtension-Safe API -> 否

我的播客文件:

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
4

8 回答 8

8

在我的 Swift Packages 中升级到 Xcode 13 后开始遇到这个问题。SPM 没有与 pod 相同的目标成员资格选项,因此 Uday 的回答对我来说不太适用。这个问题本质上是,现在(显然?)正在为所有可用目标编译 SPM 包,无论它们是否实际链接。因此,如果您的代码在您从未打算使用它的某个地方不起作用,那么您根本无法构建您的代码。

我的解决方案是分叉包并将有此问题的类标记为在扩展中不可用:

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

或者等效地在 Swift 中:

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

您还可以将这些可用性宏应用于特定的函数/变量,但对我来说将它放在整个类上的侵入性较小,因为我从来没有打算在扩展中使用这些库。

于 2021-08-02T04:12:56.257 回答
3

某些 Pod 不能在扩展中使用。我遇到了与 IQKeyboardManager 完全相同的问题。 https://github.com/hackiftekhar/IQKeyboardManager/issues/410

sharedApplication 或 shared 在我使用的扩展类型中不可用。

于 2018-09-25T17:27:06.073 回答
3

我在这里找到了一个可行的解决方案:https ://ahbou.org/post/169786332682/shared-is-unavailable-use-view-controller-based

听起来像这样:

在左侧菜单中选择 Pods Project

在目标列表中选择与扩展不兼容的 Pod

选择构建设置

将 Require Only App-Extension Safe API 设置为 NO

于 2021-11-11T14:20:40.240 回答
1

从该文件中删除扩展检查目标成员资格。

在此处输入图像描述

于 2021-02-17T01:00:35.110 回答
0

我不知道为什么,但是通过 deintegrate and install pods 再次修复它!

pod deintegrate
pod install
于 2021-12-10T20:30:55.870 回答
0
  1. 如果有任何旧的扩展名,请检查管理/编辑架构取消选择它,仅使用所需的扩展名。

  2. 在 podfile.xml 中添加以下这些代码行。

    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
    
于 2021-12-13T13:40:26.073 回答
-1

UIApplication.shared在应用程序扩展中不可用。因此,在您的 podfile 中,删除使用的库UIApplication.shared

于 2020-02-29T02:50:51.287 回答
-2

问题是 pod 是用非常早期的 Swift 版本(2?)编写的,而您正试图在更新的版本下使用它。sharedApplication改成shared很久以前的了。

您需要获取 pod 的更新版本(如果可能)。

于 2018-09-25T17:13:30.107 回答