This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Connectivity] add a method to request location on iOS (for iOS 13) #1999
Merged
cyanglaz
merged 19 commits into
flutter-team-archive:master
from
cyanglaz:connectivity_ios13
Aug 29, 2019
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5c8d7f3
draft
7201c6f
add a method to request location
bb199d4
cleanup
0fd8cd9
update version
17f69cf
assert
69ab075
Update packages/connectivity/README.md
50a0ffb
Update packages/connectivity/README.md
169c5e6
Update packages/connectivity/README.md
782c472
Update packages/connectivity/README.md
0634d62
Update packages/connectivity/lib/connectivity.dart
c7666d6
Update packages/connectivity/lib/connectivity.dart
3a68b15
Update packages/connectivity/lib/connectivity.dart
ef2dcfb
Update packages/connectivity/lib/connectivity.dart
c0d9783
Update packages/connectivity/README.md
26b2874
review fixes
666821b
remove unnecesary result property
e577181
review fixes and add driver test for
12efe99
fix tests
f908024
Merge branch 'master' into connectivity_ios13
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 WIFI information.</string> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check caps on WIFI, consider wi-fi |
||
| <key>NSLocationWhenInUseUsageDescription</key> | ||
| <string>This app requires accessing your location information when the app is in foreground to get WIFI information.</string> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check caps on WIFI, consider wi-fi |
||
| <key>UILaunchStoryboardName</key> | ||
| <string>LaunchScreen</string> | ||
| <key>UIMainStoryboardFile</key> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -90,14 +91,48 @@ 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 { | ||
| print( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not thrilled about this print statement, it seems like it could clutter the logs. Do you think it's necessary? |
||
| 'location service is not authorized, wifiName might not be correct'); | ||
| 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 { | ||
| print( | ||
| 'location service is not authorized, WIFIBSSID might not be correct'); | ||
| wifiBSSID = await _connectivity.getWifiBSSID(); | ||
| } | ||
| } else { | ||
| wifiBSSID = await _connectivity.getWifiBSSID(); | ||
| } | ||
| } on PlatformException catch (e) { | ||
| print(e.toString()); | ||
| wifiBSSID = "Failed to get Wifi BSSID"; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/connectivity/ios/Classes/FLTConnectivityLocationHandler.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
58
packages/connectivity/ios/Classes/FLTConnectivityLocationHandler.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, immdediately return. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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 | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be wrong here but I think it should just be Privacy - Location Always Usage Description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I thought before writing this PR too :) Appearently the name has been changed.
https://developer.apple.com/documentation/bundleresources/information_property_list/nslocationalwaysandwheninuseusagedescription?language=objc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add "This is called" from my suggestion above?