Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.4

* Add `requestLocationServiceAuthorization` to request location authorization on iOS.
* Add `getLocationServiceAuthorization` to get location authorization status on iOS.
* Update README: add more information on iOS 13 updates with CNCopyCurrentNetworkInfo.

## 0.4.3+7

* Update README with the updated information about CNCopyCurrentNetworkInfo on iOS 13.
Expand Down
38 changes: 30 additions & 8 deletions packages/connectivity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dispose() {
}
```

You can get WIFI related information using:
You can get wi-fi related information using:

```dart
import 'package:connectivity/connectivity.dart';
Expand All @@ -60,17 +60,39 @@ var wifiIP = await (Connectivity().getWifiIP());network
var wifiName = await (Connectivity().getWifiName());wifi network
```

### Known Issues
### iOS 12

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

The methods `.getWifiBSSID()` and `.getWifiName()` utilize the [CNCopyCurrentNetworkInfo](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo) function on iOS.
### iOS 13

As of iOS 13, Apple announced that these APIs will no longer return valid information by default and will instead return the following:
> SSID: "Wi-Fi" or "WLAN" ("WLAN" will be returned for the China SKU)
> BSSID: "00:00:00:00:00:00"
The methods `.getWifiBSSID()` and `.getWifiName()` utilize the [`CNCopyCurrentNetworkInfo`](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo) function on iOS.

You can follow issue [#37804](https://github.com/flutter/flutter/issues/37804) for the changes required to return valid SSID and BSSID values with iOS 13.
As of iOS 13, Apple announced that these APIs will no longer return valid information.
An app linked against iOS 12 or earlier receives pseudo-values such as:

* SSID: "Wi-Fi" or "WLAN" ("WLAN" will be returned for the China SKU).

* BSSID: "00:00:00:00:00:00"

An app linked against iOS 13 or later receives `null`.

The `CNCopyCurrentNetworkInfo` will work for Apps that:

* The app uses Core Location, and has the user’s authorization to use location information.

* The app uses the NEHotspotConfiguration API to configure the current Wi-Fi network.

* The app has active VPN configurations installed.

If your app falls into the last two categories, it will work as it is. If your app doesn't fall into the last two categories,
and you still need to access the wifi information, you should request user's authorization to use location information.

There is a helper method provided in this plugin to request the location authorization: `requestLocationServiceAuthorization`.
To request location authorization, make sure to add the following keys to your _Info.plist_ file, located in `<project root>/ios/Runner/Info.plist`:

* `NSLocationAlwaysAndWhenInUseUsageDescription` - describe why the app needs access to the user’s location information all the time (foreground and background). This is called _Privacy - Location Always and When In Use Usage Description_ in the visual editor.
* `NSLocationWhenInUseUsageDescription` - describe why the app needs access to the user’s location information when the app is running in the foreground. This is called _Privacy - Location When In Use Usage Description_ in the visual editor.

## Getting Started

Expand Down
4 changes: 4 additions & 0 deletions packages/connectivity/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app requires accessing your location information all the time to get wi-fi information.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires accessing your location information when the app is in foreground to get wi-fi information.</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
8 changes: 8 additions & 0 deletions packages/connectivity/example/ios/Runner/Runner.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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>
35 changes: 33 additions & 2 deletions packages/connectivity/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -90,14 +91,44 @@ class _MyHomePageState extends State<MyHomePage> {
String wifiName, wifiBSSID, wifiIP;

try {
wifiName = await _connectivity.getWifiName();
if (Platform.isIOS) {
LocationAuthorizationStatus status =
await _connectivity.getLocationServiceAuthorization();
if (status == LocationAuthorizationStatus.notDetermined) {
status =
await _connectivity.requestLocationServiceAuthorization();
}
if (status == LocationAuthorizationStatus.authorizedAlways ||
status == LocationAuthorizationStatus.authorizedWhenInUse) {
wifiName = await _connectivity.getWifiName();
} else {
wifiName = await _connectivity.getWifiName();
}
} else {
wifiName = await _connectivity.getWifiName();
}
} on PlatformException catch (e) {
print(e.toString());
wifiName = "Failed to get Wifi Name";
}

try {
wifiBSSID = await _connectivity.getWifiBSSID();
if (Platform.isIOS) {
LocationAuthorizationStatus status =
await _connectivity.getLocationServiceAuthorization();
if (status == LocationAuthorizationStatus.notDetermined) {
status =
await _connectivity.requestLocationServiceAuthorization();
}
if (status == LocationAuthorizationStatus.authorizedAlways ||
status == LocationAuthorizationStatus.authorizedWhenInUse) {
wifiBSSID = await _connectivity.getWifiBSSID();
} else {
wifiBSSID = await _connectivity.getWifiBSSID();
}
} else {
wifiBSSID = await _connectivity.getWifiBSSID();
}
} on PlatformException catch (e) {
print(e.toString());
wifiBSSID = "Failed to get Wifi BSSID";
Expand Down
9 changes: 9 additions & 0 deletions packages/connectivity/example/test_driver/connectivity.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:connectivity/connectivity.dart';
Expand Down Expand Up @@ -28,5 +29,13 @@ void main() {
break;
}
});

test('test location methods, iOS only', () async {
print(Platform.isIOS);
if (Platform.isIOS) {
expect((await _connectivity.getLocationServiceAuthorization()),
LocationAuthorizationStatus.notDetermined);
}
});
});
}
47 changes: 46 additions & 1 deletion packages/connectivity/ios/Classes/ConnectivityPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

#import "Reachability/Reachability.h"

#import <CoreLocation/CoreLocation.h>
#import "FLTConnectivityLocationHandler.h"
#import "SystemConfiguration/CaptiveNetwork.h"

#include <ifaddrs.h>

#include <arpa/inet.h>

@interface FLTConnectivityPlugin () <FlutterStreamHandler>
@interface FLTConnectivityPlugin () <FlutterStreamHandler, CLLocationManagerDelegate>

@property(strong, nonatomic) FLTConnectivityLocationHandler* locationHandler;

@end

@implementation FLTConnectivityPlugin {
Expand Down Expand Up @@ -111,6 +116,18 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result([self getBSSID]);
} else if ([call.method isEqualToString:@"wifiIPAddress"]) {
result([self getWifiIP]);
} else if ([call.method isEqualToString:@"getLocationServiceAuthorization"]) {
result([self convertCLAuthorizationStatusToString:[FLTConnectivityLocationHandler
locationAuthorizationStatus]]);
} else if ([call.method isEqualToString:@"requestLocationServiceAuthorization"]) {
NSArray* arguments = call.arguments;
BOOL always = [arguments.firstObject boolValue];
__weak typeof(self) weakSelf = self;
[self.locationHandler
requestLocationAuthorization:always
completion:^(CLAuthorizationStatus status) {
result([weakSelf convertCLAuthorizationStatusToString:status]);
}];
} else {
result(FlutterMethodNotImplemented);
}
Expand All @@ -121,6 +138,34 @@ - (void)onReachabilityDidChange:(NSNotification*)notification {
_eventSink([self statusFromReachability:curReach]);
}

- (NSString*)convertCLAuthorizationStatusToString:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
return @"notDetermined";
}
case kCLAuthorizationStatusRestricted: {
return @"restricted";
}
case kCLAuthorizationStatusDenied: {
return @"denied";
}
case kCLAuthorizationStatusAuthorizedAlways: {
return @"authorizedAlways";
}
case kCLAuthorizationStatusAuthorizedWhenInUse: {
return @"authorizedWhenInUse";
}
default: { return @"unknown"; }
}
}

- (FLTConnectivityLocationHandler*)locationHandler {
if (!_locationHandler) {
_locationHandler = [FLTConnectivityLocationHandler new];
}
return _locationHandler;
}

#pragma mark FlutterStreamHandler impl

- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)eventSink {
Expand Down
22 changes: 22 additions & 0 deletions packages/connectivity/ios/Classes/FLTConnectivityLocationHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class FLTConnectivityLocationDelegate;

typedef void (^FLTConnectivityLocationCompletion)(CLAuthorizationStatus);

@interface FLTConnectivityLocationHandler : NSObject

+ (CLAuthorizationStatus)locationAuthorizationStatus;

- (void)requestLocationAuthorization:(BOOL)always
completion:(_Nonnull FLTConnectivityLocationCompletion)completionHnadler;

@end

NS_ASSUME_NONNULL_END
58 changes: 58 additions & 0 deletions packages/connectivity/ios/Classes/FLTConnectivityLocationHandler.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "FLTConnectivityLocationHandler.h"

@interface FLTConnectivityLocationHandler () <CLLocationManagerDelegate>

@property(copy, nonatomic) FLTConnectivityLocationCompletion completion;
@property(strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation FLTConnectivityLocationHandler

+ (CLAuthorizationStatus)locationAuthorizationStatus {
return CLLocationManager.authorizationStatus;
}

- (void)requestLocationAuthorization:(BOOL)always
completion:(FLTConnectivityLocationCompletion)completionHandler {
CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
if (status != kCLAuthorizationStatusAuthorizedWhenInUse && always) {
completionHandler(kCLAuthorizationStatusDenied);
return;
} else if (status != kCLAuthorizationStatusNotDetermined) {
completionHandler(status);
return;
}

if (self.completion) {
// If a request is still in process, immediately return.
completionHandler(kCLAuthorizationStatusNotDetermined);
return;
}

self.completion = completionHandler;
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
if (always) {
[self.locationManager requestAlwaysAuthorization];
} else {
[self.locationManager requestWhenInUseAuthorization];
}
}

- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
return;
}
if (self.completion) {
self.completion(status);
self.completion = nil;
}
}

@end
Loading