简体   繁体   中英

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

I have following internet connectivity checking code using rechability to notify when internet connection status changes in my app.

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

It was working properly before but after adding third party frameworks through cocoa pods i am getting following error. Ambiguous use of 'reachabilityChanged'

As suggested in some sites/blogs if i change it to following code

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

Then error is resolved, but when interent connection changes it does not notify and checkForReachability method not getting called.

How to resolve this issue. Note: I dont know which third party framework / files causing this issue

I am using swift 5

Following is my pod file

# 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

When i changed notification reachabilityChanged name in ReachabilitySwift pod library files to other name, then i was not getting build error and when network status changed checkForReachability method was also getting called.

But as changing something in podfile is not good solution because if pod is updated each time we need to do it, so i removed ReachabilitySwift pod file and used network manager of alamofire library.

It seems like reachability used by both almorfire and externally was causing the issue.

I had both Reachability and ReachabilitySwift pod installed. I removed the ReachabilitySwift pod and then used the following code snippet from my appDidFinishLaunching method

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()
}

This worked for me. Other combinations didn't work for me. The block/selector to NSNotification was just not getting called but it did when i used the above snippet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM