plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Question]: The Wi-Fi SSID and BSSID cannot be obtained using network_info_plus, and null is returned.Opens the premise of accurate positioning

Open ZHDKk opened this issue 2 years ago • 15 comments

What is your question?

The Wi-Fi SSID and BSSID cannot be obtained using network_info_plus, and null is returned. xCode 15 physics:IOS 17、16.5、15.5

This is my configuration in xCode:

1 2 3

my code: final networkInfo = NetworkInfo(); var ssid =await networkInfo.getWifiName(); print("ssid:$ssid");

output result: flutter: ssid:null

I tried various methods but couldn't get the Wi-Fi SSID and BSSID.

This question has been bothering me for 3 days,Please help me!Thanks

Checklist before submitting a question

  • [X] I Google'd a solution and I couldn't find it
  • [X] I searched on StackOverflow for a solution and I couldn't find it
  • [X] I read the README.md file of the plugin
  • [X] I am using the latest version of the plugin
  • [X] All dependencies are up to date with flutter pub upgrade
  • [X] I did a flutter clean
  • [X] I tried running the example project

ZHDKk avatar Sep 22 '23 08:09 ZHDKk

From the Readme:

Device permissions # To access protected WiFi methods related to location, you must request additional permissions on Android and iOS. Users of this plugin should use the permission_handler Flutter plugin to handle these cases.

Have you requested the location permission?

bettysteger avatar Sep 23 '23 15:09 bettysteger

From the Readme:

Device permissions # To access protected WiFi methods related to location, 你 must request additional permissions on Android and iOS. Users of this plugin should use the permission_handler Flutter plugin to handle these cases.

Have you requested the location permission?

Yes, I use permission_handler to request location permissions. permission_con WechatIMG615 WechatIMG616

I even used xcode native code to request location permissions and still couldn't get SSID . location_oc But strangely enough, I used xcode to create a new project that did not use any flutter code, and all the configurations were the same as flutter's, to get the Wi-Fi SSID and BSSID. I guarantee the configuration is the same as above. getSsid Success SSID and BSSID cannot be obtained as long as they pass flutter, this error will be reported NEHotspotNetwork nehelper sent invalid result code [1] for Wi-Fi information request . Is there something missing from my configuration between Xcode native code and flutter? This confuses me getssidCodeNav getwifissidnav getWifiInfoFlutter getSsidFail

ZHDKk avatar Sep 25 '23 01:09 ZHDKk

How do fix this problem?

ZHDKk avatar Sep 27 '23 00:09 ZHDKk

Did you do this from the README?

To use .getWifiBSSID() and .getWifiName() on iOS >= 12, the Access WiFi information capability in XCode must be enabled. Otherwise, both methods will return null.

bettysteger avatar Sep 27 '23 07:09 bettysteger

Did you do this from the README?

To use .getWifiBSSID() and .getWifiName() on iOS >= 12, the Access WiFi information capability in XCode must be enabled. Otherwise, both methods will return null.

Yes! I also mentioned this in the third screenshot of the question. All my actions follow the readme, but I still can't get it. I don't know what exactly went wrong

Thank you!

ZHDKk avatar Sep 27 '23 08:09 ZHDKk

I also have the same problem. Both .getWifiBSSID() and .getWifiName() got null.

  • iOS 16.6

JooYoo avatar Sep 27 '23 12:09 JooYoo

I also have the same problem. Both .getWifiBSSID() and .getWifiName() got null.

  • iOS 16.6

Have you solved this problem?

ZHDKk avatar Oct 07 '23 00:10 ZHDKk

Managed to resolve the issue? I am facing the same with MacOS Sonoma 14.0

LiveRock avatar Oct 14 '23 04:10 LiveRock

There seems to be no solution !!!

ZHDKk avatar Nov 01 '23 06:11 ZHDKk

https://github.com/fluttercommunity/plus_plugins/issues/2263

Facing the same issue with MacOS

LiveRock avatar Nov 08 '23 03:11 LiveRock

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Feb 07 '24 00:02 github-actions[bot]

I have the exact same problem, still got null.

SArthurX avatar Feb 07 '24 07:02 SArthurX

The problem is still relevant! iOS 17.2

As I see network_info_plus plugin uses deprecated API: https://github.com/fluttercommunity/plus_plugins/blob/aca016d360411fd24e59526fe78e56fbd7efa11b/packages/network_info_plus/network_info_plus/ios/Classes/FPPCaptiveNetworkInfoProvider.m#L9-L21

But should use a new API for iOS 14+ https://developer.apple.com/documentation/networkextension/nehotspotnetwork/3666511-fetchcurrentwithcompletionhandle?language=objc

Refs:

  • https://developer.apple.com/forums/thread/679038?answerId=672831022#672831022
  • https://developer.apple.com/forums/thread/683089

cc @bettysteger

yauhenipakala avatar Feb 07 '24 22:02 yauhenipakala

@ZHDKk If native implementations work fine, it's not a question. It's a bug. Could you please rename the issue to increase priority?

yauhenipakala avatar Feb 07 '24 22:02 yauhenipakala

Thanks for finding the cause @wcoder Is it possible for you to send a PR with the change? I'll help review it.

miquelbeltran avatar Feb 08 '24 08:02 miquelbeltran

Currently looking at this. The plugin already uses the iOS 14 methods btw.

Calls to NEHotspotNetwork.fetchCurrentWithCompletionHandler on iOS 14 or greater.

The documentation of the method says:

 * @discussion This method returns SSID, BSSID and security type of the current Wi-Fi network when the
 *   requesting application meets one of following 4 requirements -.
 *   1. application is using CoreLocation API and has user's authorization to access precise location.
 *   2. application has used NEHotspotConfiguration API to configure the current Wi-Fi network.
 *   3. application has active VPN configurations installed.
 *   4. application has active NEDNSSettingsManager configuration installed.
 *   An application will receive nil if it fails to meet any of the above 4 requirements.
 *   An application will receive nil if does not have the "com.apple.developer.networking.wifi-info" entitlement.

application is using CoreLocation API and has user's authorization to access precise location.

This is what I am trying with the permission_handler plugin, and in theory I am able to get locationWhenInUse granted. Still, the method fetchCurrentWithCompletionHandler returns null.

But also says this:

An application will receive nil if does not have the "com.apple.developer.networking.wifi-info" entitlement.

According to Apple docs:

To add this entitlement to your app, enable the Access WiFi Information capability in Xcode.

You can add the entitlement also creating a Runner.entitlements file and adding this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>com.apple.developer.networking.wifi-info</key>
	<true/>
</dict>
</plist>

However, people with personal provisioning profiles cannot add this entitlement:

image

So I am blocked here, because I don't have a professional account for this.

What I will do is to continue with the project cleanup and update the package documentation explaining this.

fyi @vbuberen

miquelbeltran avatar Mar 13 '24 13:03 miquelbeltran

@miquelbeltran What would you need in order to get unblocked? Do you need someone else who has an Apple developer account to assist in some way?

I'm blocked by this issue and am happy to help in any way that I can.

erichiggins avatar Mar 28 '24 05:03 erichiggins

Hi @erichiggins.

In my opinion, the plugin already does what it is supposed to do, which is to call the right API to obtain the value.

The issue with the entitlements is already well documented both from Apple side and in the plugin, however I wasn't able to test it by myself because I lack a paid dev account.

What I'd like to ask, is if someone with an Apple dev account can set up these entitlements and run the example project or similar, and see if you can obtain the value.

There is nothing else we can do from our side.

miquelbeltran avatar Mar 28 '24 07:03 miquelbeltran

Hi @erichiggins, @miquelbeltran ,

I have tried it on Sonoma 14.4. with our company's developer account and the following packages / (debug) entitlements: network_info_plus: ^5.0.2 connectivity_plus: ^5.0.2

	<dict>
		<key>com.apple.security.app-sandbox</key>
		<true />
		<key>com.apple.security.cs.allow-jit</key>
		<true />
		<key>com.apple.security.network.client</key>
		<true />
		<key>com.apple.security.network.server</key>
		<true />
		<key>com.apple.security.print</key>
		<true />
		<key>com.apple.developer.networking.wifi-info</key>
		<true />
	</dict>

However, I do get null as a result when I call _getWifiName or getWifiBSSID.

String? wifiName =
        await _networkInfo.getWifiName() ?? t.system.wifi_unknown;
String? wifiBSSID =
        await _networkInfo.getWifiBSSID() ?? t.system.wifi_unknown;

Am I missing another entitlement or something else?

If you send me a minimum example that should work, I'd be more than happy to try it with our paid developer account.

Vera-Spoettl avatar Mar 28 '24 08:03 Vera-Spoettl

Thanks @Vera-Spoettl

According to Apple, only the entitlement com.apple.developer.networking.wifi-info would be necessary.

Remember that you also need to obtain precise location permission before querying the API.

You can try as well with the example in the package repository, set up the iOS project and entitlements, set a breakpoint in the swift code and see if the API is returning correctly or not.

Edit: Just in case, test on a real device, not a simulator too.

miquelbeltran avatar Mar 28 '24 08:03 miquelbeltran

You can see what I'm doing here. I'm not getting Wi-Fi information with flutter, but I'm not getting Wi-Fi information with flutter. I guess Apple made restrictions because of security, and I've given up on that

ZHDKk avatar Mar 28 '24 08:03 ZHDKk

You can see what I'm doing here. I'm not getting Wi-Fi information with flutter, but I'm not getting Wi-Fi information with flutter. I guess Apple made restrictions because of security, and I've given up on that

It's definitely strange that your pure native implementation works but then breaks when Flutter is involved.

There is also a tricky situation with the permission_handler plugin regarding asking for permanent permissions, as you have to ask first for temporal, then ask for permanent. Not sure if that is an issue or not. I'd try to use temporal permissions only ("while on use"), see if that works.

In my tests, when the plugin calls to this method, the NEHotspotNetwork *network returned in the callback was always null. If anyone wants to try to debug this, that's the method, in the ios/Classes/FPPHotspotNetworkInfoProvider.m file.

[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(
                        NEHotspotNetwork *network) {
    dispatch_async(dispatch_get_main_queue(), ^{
      if (network) {
        completionHandler([[FPPNetworkInfo alloc] initWithSSID:network.SSID
                                                         BSSID:network.BSSID]);
        return;
      }
      completionHandler(nil);
    });
  }];

miquelbeltran avatar Mar 28 '24 09:03 miquelbeltran

Thanks @Vera-Spoettl

According to Apple, only the entitlement com.apple.developer.networking.wifi-info would be necessary.

Remember that you also need to obtain precise location permission before querying the API.

You can try as well with the example in the package repository, set up the iOS project and entitlements, set a breakpoint in the swift code and see if the API is returning correctly or not.

Edit: Just in case, test on a real device, not a simulator too.

@miquelbeltran To be honest, I'm a bit lost now. On iOS everything works fine. It's MacOS that troubles me.

The example from the package doesn't work on my Mac (Sonoma 14.4). The source code lacks the entitlement (which was no problem to add), but even after adding I still get this result:

image

I wanted to add permission checking but the only way I know how to do it is the permission_handler package which is not available for MacOS. 😵‍💫

Vera-Spoettl avatar Mar 28 '24 10:03 Vera-Spoettl

This is good news and bad news @Vera-Spoettl 😄

I am happy to hear that you got iOS working! I guess it is a combination of location permissions and entitlements that is a hard requirement here, but glad you had it working.

The MacOS question problem is that the implementation is completely different. The plugin uses internally CWWiFiClient.shared().interface() from CoreWLAN. Found this discussion on the Apple forums: https://forums.developer.apple.com/forums/thread/732431

Accessing the ssid property now requires the location privilege (r. 108641482). We called this out in the header doc comments, where <CoreWLAN/CWInterface.h> now says: SSID information is not available unless Location Services is enabled and the user has authorized the calling app to use location services.

Seems that this is a recent change in the OS. An option would be to use an alternative plugin that also supports MacOS like https://pub.dev/packages/permission_policy

If the problem persist I'd recommend creating a different ticket and keeping this one for iOS discussion only.

miquelbeltran avatar Mar 28 '24 11:03 miquelbeltran

Thanks for the tip about the example app @Vera-Spoettl

Leveraging the permission handling code from that allowed me to get things working on both iOS 15.8.2 and Android 14.

https://github.com/fluttercommunity/plus_plugins/blob/main/packages/network_info_plus/network_info_plus/example/lib/main.dart

Here's what I added to my ios/Runner/Info.plist

       <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
       <string>Location permission is required to handle wifi connections in the background.</string>
       <key>NSLocationWhenInUseUsageDescription</key>
       <string>Location permission is required to handle wifi connections.</string>
       <key>com.apple.developer.networking.wifi-info</key>
       <true/>

And here's what I have in ios/Runner/Runner.entitlements

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>com.apple.developer.networking.HotspotConfiguration</key>
	<true/>
	<key>com.apple.developer.networking.wifi-info</key>
	<true/>
</dict>
</plist>

Note: As is typical with this type of development, I added a bunch of permissions and it's not yet clear if all are needed.

erichiggins avatar Mar 29 '24 19:03 erichiggins

Thank you both for the clarification! I will be closing this issue as this has been solved.

It's unfortunate that Apple does things difficult, and from the plugin perspective is hard for us to leverage that.

If you feel the documentation needs further clarification, please feel free to submit a PR with your suggestions!

miquelbeltran avatar Mar 30 '24 09:03 miquelbeltran

It's unfortunate that Apple does things difficult, and from the plugin perspective is hard for us to leverage that.

FWIW: I had the same issue on Android, not just iOS. Following the example app's code logic resolved the issue for both.

erichiggins avatar Apr 11 '24 16:04 erichiggins

Don't forget to add the following to your Podfile:

` post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)',

            ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
            'PERMISSION_LOCATION=1',
          ]
    end

end end `

https://mugs-lick-y62.craft.me/NlGxFp6yzx8Yj8

EverYeon avatar Jul 18 '24 06:07 EverYeon