Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use NWPathMonitor for iOS/macOS #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

bomjkolyadun
Copy link

@bomjkolyadun bomjkolyadun commented Sep 21, 2021

Overview

The network type for ethernet was reported incorrectly on macOS.

Since there's a new API NWPathMonitor (introduced in iOS 12/macOS 10.14) to detect network connectivity changes, I rewrote ios network state watching using this API.

Test Plan

I'm blocked on testing on the example project because the build is failing on the latest Xcode (likely due to an outdated RN version). Here's what I get on Xcode 12.5.1:
Screen Shot 2021-09-20 at 6 04 14 PM

Demo from our project that's using a forked lib:
https://user-images.githubusercontent.com/1094629/134097094-ed289262-c1e6-4cba-b979-6e290d1ff0d8.mov

Test Case 1:

  • connect to WiFI network
  • check that the network connectivity state is reported correctly - connected, wifi
  • Turn off WiFi
  • check that the network connectivity state is reported correctly - disconnected

Test Case 2:

  • connect to the ethernet
  • check that the network connectivity state & network type is reported correctly - connected, ethernet
  • disconnect from ethernet
  • check that the network connectivity state is reported correctly - disconnected

@mikehardy
Copy link
Contributor

If NWPathMonitor was only introduced in iOS12/macOS10.14 that's problematic, this is a module used widely so rather than go past the current react-native minimum (which is 11) this module really should maintain compatibility with the iOS/macOS versions supported by the most recent couple of versions. It was 10 until quite recently (react-native 0.65 was released just a short while ago with that change to 11)

12 shouldn't be the minimum for quite a while

Similarly, react-native-macos runs on macOS 10.13 so using these symbols exclusively is problematic at this point

@mikehardy
Copy link
Contributor

Separately - the compile error reminded me of this thebergamo/react-native-fbsdk-next#97 - I wonder if a separate commit (or separate PR?) with the needed change would do the trick.

That said, I haven't had any problems compiling netinfo with any version of Xcode up to and including the last Xcode 13 beta - I had not checked the RC and it's released proper as 13 now, but I assume it will still work. I'm surprised to see a compile error.

@bomjkolyadun
Copy link
Author

bomjkolyadun commented Sep 21, 2021

@mikehardy thanks for reviewing my PR! I really appreciate it!

Regarding compile errors – I compiled the example, and since errors are in React-Core, probably that's just some older version of ReactNative or something like that. I can dig into this.

Regarding deployment target:
I believe that SCNetworkReachability won't be able to differentiate between WiFI and Ethernet (here's the list of supported flags), so, unfortunately, to address that issue I had to use NWPathMonitor.

We can go with one of two options here:

  • We could just close this PR because the feature visibility is rather small – I think, react-native is way more popular in mobile. If anyone would face this issue, they would be able to use my fork (or create their own)
  • I can revert SCNetworkReachability and wrap it into ifdef to keep compatibility with older OSes. But this solution would be rather messy because we would have two independent approaches to achieve the same result. Which would complicate testing.

Please let me know your thoughts

@mikehardy
Copy link
Contributor

I am actually all for new (better) API uses, and if it requires an IFDEF for new APIs, with the "old" style marked clearly as "delete when iOS deployment target is greater than XYZ" and the APIs themselves (in the README) are also marked clearly as "uses XYZ strategy for ios < NN / macOS < NN, uses ABC strategy for ios >= YY / macOS >= YY". That puts the information for necessary testing out there, marks it for sunset, and lets the newer/better APIs shine

I can say that I just compiled the module itself using Xcode 13 and it's fine, and I know it was fine on Xcode 12.5 etc so that must be some sort of error local to your environment or as you mention something about the react-native version in the example. The example is probably pretty stale so it's easy to guess that is the source...

@mikehardy
Copy link
Contributor

(actually, rather than ifdef since those symbols should be available in all supported Xcode versions, maybe an if (@available(iOS 12.0, macOS 10.14, *) check, assuming that construction compiles)

@mikehardy
Copy link
Contributor

Happy to merge this as long as it has some backwards-compatibility for the old style, iOS 12 is still not even the react-native default so that's a non-starter for a while unfortunately. Probably 2 years really.

Copy link
Contributor

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the ability to merge + release here now but needs changes as noted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants