通过可可豆荚安装第三方框架后出现“reachabilityChanged”错误?

Posted

技术标签:

【中文标题】通过可可豆荚安装第三方框架后出现“reachabilityChanged”错误?【英文标题】:Getting Ambiguous use of 'reachabilityChanged' error after installing third party framework through cocoa pods? 【发布时间】:2020-01-24 13:14:18 【问题描述】:

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

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

之前它工作正常,但在通过可可豆荚添加第三方框架后,我收到以下错误。 'reachabilityChanged' 的模糊使用

如果我将其更改为以下代码,则如某些网站/博客中所建议的那样

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

【问题讨论】:

您将哪些库添加为 cocoapods? 你试过下面的swift 4 / swift 5 answer ***.com/a/30743763/2303865的可能重复 【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

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

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 的块/选择器没有被调用,但是当我使用上面的 sn-p 时它确实被调用了。

【讨论】:

以上是关于通过可可豆荚安装第三方框架后出现“reachabilityChanged”错误?的主要内容,如果未能解决你的问题,请参考以下文章

通过可可豆荚问题安装 AFNetworking

可可豆荚更新破坏了一些东西,现在“豆荚更新”会产生错误

使用可可豆荚的 Parse 1.7.1 中缺少 PFImageView

如何找出已安装的可可豆荚的版本?

如何使用可可豆荚安装 Google Api

已删除 GoogleMaps 可可豆荚,现在出现链接器错误