通过 cocoa pods 安装第三方框架后出现 Ambiguous use of 'reachabilityChanged' 错误?

Getting Ambiguous use of 'reachabilityChanged' error after installing third party framework through cocoa pods?

我有以下互联网连接检查代码,使用可恢复性在我的应用程序中的互联网连接状态发生变化时发出通知。

self.reachability = Reachability.forInternetConnection();
self.reachability.startNotifier();
NotificationCenter.default.addObserver(self, selector: #selector(self.checkForReachability(notification:)), name: Notification.Name.reachabilityChanged, object: reachability)

它之前工作正常,但在通过 cocoa pods 添加第三方框架后出现以下错误。 'reachabilityChanged'

的使用不明确

如某些 sites/blogs 中所建议,如果我将其更改为以下代码

NotificationCenter.default.addObserver(self, selector: #selector(self.checkForReachability(notification:)), name: Notification.Name("reachabilityChanged"), object: nil)

然后错误得到解决,但是当内部连接更改时它不会通知并且不会调用 checkForReachability 方法。

如何解决这个问题。 注意:我不知道是哪个第三方框架/文件导致了这个问题

我正在使用 swift 5

以下是我的pod文件

# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
source "https://gitlab.linphone.org/BC/public/podspec.git"
source "https://github.com/CocoaPods/Specs.git"

def basic_pods
  if ENV['PODFILE_PATH'].nil?
    pod 'linphone-sdk/basic-frameworks', '> 4.3.0-alpha'
    else
    pod 'linphone-sdk/basic-frameworks', :path => ENV['PODFILE_PATH']  # loacl sdk
  end

  if not ENV['USE_CRASHLYTHICS'].nil?
    # activate crashlythics
    pod 'Firebase/Core'
    pod 'Fabric', '~> 1.9.0'
    pod 'Crashlytics', '~> 3.12.0'
    pod 'Firebase/Performance'

  end
end

target ‘ProjectNameTests' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for liblinphoneTester
  basic_pods

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

end

target 'ProjectName' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for linphone
  basic_pods
  pod 'SVProgressHUD'
  pod 'Alamofire'
  pod 'SwiftyJSON'
  pod 'IQKeyboardManagerSwift'
  pod 'Kingfisher'
  pod 'MBProgressHUD'
  pod 'SCNetworkReachability'
  pod 'ReachabilitySwift'
  pod 'SideMenu', '~> 6.0'
  pod 'TPKeyboardAvoiding'
  pod "QBImagePickerController"
  pod 'SwiftyContacts'
  pod 'Stripe'
  pod 'BraintreeDropIn'

  #Multiple image selection
  pod 'OpalImagePicker'

  #Firebase
  pod 'Firebase/Analytics'
  pod 'Firebase/Messaging'


  #pod 'DPOTPView'
  pod 'XMPPFramework'
  #pod 'ReachabilitySwift'
  pod 'SDWebImage', '4.4.7'
  #pod 'GrowingTextView', '0.4'
  pod 'GrowingTextView', '0.7.2'

  pod 'TTTAttributedLabel'
  pod 'GooglePlaces'
  pod 'GooglePlacePicker'
  pod 'GoogleMaps'
  pod 'ReverseExtension', '~> 0.5.0'
  pod 'PlacesPicker'



 # target 'ProjectNameTests' do
    #inherit! :search_paths
    #pod 'KIF', :configurations => ['Debug']
    # Pods for testing
  #end

end


=begin
target 'latestCallsWidget' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for latestCallsWidget

end

target 'latestChatroomsWidget' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for latestChatroomsWidget
end


target 'richNotifications' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  use_frameworks!

  # Pods for richNotifications

end
=end

post_install do |installer|
  # Get the version of linphone-sdk
  installer.pod_targets.each do |target|
    if target.pod_name == 'linphone-sdk'
      target.specs.each do |spec|
        $linphone_sdk_version = spec.version
      end
    end
  end

  app_project = Xcodeproj::Project.open(Dir.glob("*.xcodeproj")[0])
  app_project.native_targets.each do |target|
    if target.name == 'ProjectName'
      target.build_configurations.each do |config|
        if ENV['USE_CRASHLYTHICS'].nil?
          if config.name == "Debug" then
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = '$(inherited) DEBUG=1'
          else
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = '$(inherited)'
          end
        else
          # activate crashlythics
          if config.name == "Debug" then
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = '$(inherited) DEBUG=1 USE_CRASHLYTHICSS=1'
            else
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = '$(inherited) USE_CRASHLYTHICSS=1'
          end
        end

        config.build_settings['OTHER_CFLAGS'] = '-DBCTBX_LOG_DOMAIN=\"ios\"',
                                                '-DCHECK_VERSION_UPDATE=FALSE',
                                                '-DENABLE_QRCODE=TRUE',
                                                '-DENABLE_SMS_INVITE=TRUE',
                                                '$(inherited)',
                                                "-DLINPHONE_SDK_VERSION=\\"#{$linphone_sdk_version}\\""

        app_project.save
      end
    end
  end
end

当我将 ReachabilitySwift pod 库文件中的通知 reachabilityChanged 名称更改为其他名称时,我没有收到构建错误,并且当网络状态更改时,checkForReachability 方法也被调用。

但是由于更改 podfile 中的某些内容不是好的解决方案,因为如果每次我们需要更新 pod,所以我删除了 ReachabilitySwift pod 文件并使用了 alamofire 库的网络管理器。

似乎是 almorfire 和外部使用的可达性导致了这个问题。

我同时安装了 Reachability 和 ReachabilitySwift pod。我删除了 ReachabilitySwift pod,然后使用了我的 appDidFinishLaunching 方法中的以下代码片段

func startReachability() {
    self.reachability = Reachability.forInternetConnection() //reachability has strong reference.
    NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "kReachabilityChangedNotification") , object: nil, queue: OperationQueue.main) { (noti) in
        let r = noti.object as! Reachability
        DispatchQueue.main.async {
            let alert = UIAlertController(title: "Success!", message: "\(r.isReachable())", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in

            }))
            self.window?.rootViewController?.present(alert, animated: true, completion: nil)
        }
    }
    self.reachability.startNotifier()
}

这对我有用。其他组合对我不起作用。 NSNotification 的 block/selector 只是没有被调用,但是当我使用上面的代码片段时它被调用了。