diff --git a/CHANGELOG.md b/CHANGELOG.md index 1abe18e..a44da5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.0.3 + +* Fix iOS AppHang: dispatch `SCNetworkReachabilityGetFlags` and `CTTelephonyNetworkInfo()` + to a background queue to avoid blocking the main thread during XPC calls. +* Fix typo: `transport_subtype` for WiFi was `"unknwon"`, now `"unknown"`. + ## 0.0.1 * Initial release supporting the fields: diff --git a/ios/Classes/FlutterNetworkCapabilitiesPlugin.swift b/ios/Classes/FlutterNetworkCapabilitiesPlugin.swift index 9c54cf2..4efca18 100644 --- a/ios/Classes/FlutterNetworkCapabilitiesPlugin.swift +++ b/ios/Classes/FlutterNetworkCapabilitiesPlugin.swift @@ -12,11 +12,13 @@ public class FlutterNetworkCapabilitiesPlugin: NSObject, FlutterPlugin { } public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { - if (call.method != "getNetworkInfo") { + if call.method != "getNetworkInfo" { result(FlutterMethodNotImplemented) return } - result(getNetworkInfoResult()) + DispatchQueue.global(qos: .utility).async { + result(self.getNetworkInfoResult()) + } } private func getNetworkInfoResult() -> [String : String] { @@ -48,7 +50,7 @@ public class FlutterNetworkCapabilitiesPlugin: NSObject, FlutterPlugin { } } else { networkInfoResult["transport_type"] = "wifi" - networkInfoResult["transport_subtype"] = "unknwon" + networkInfoResult["transport_subtype"] = "unknown" } return networkInfoResult diff --git a/lib/flutter_network_capabilities_method_channel.dart b/lib/flutter_network_capabilities_method_channel.dart index 88490d9..8f2d4e2 100644 --- a/lib/flutter_network_capabilities_method_channel.dart +++ b/lib/flutter_network_capabilities_method_channel.dart @@ -12,13 +12,17 @@ class MethodChannelFlutterNetworkCapabilities @override Future> getNetworkInfo() async { - final networkInfo = await methodChannel - .invokeMapMethod('getNetworkInfo'); + try { + final networkInfo = await methodChannel + .invokeMapMethod('getNetworkInfo'); - if (networkInfo == null) { + if (networkInfo == null) { + return {}; + } + + return networkInfo; + } catch (e) { return {}; } - - return networkInfo; } } diff --git a/pubspec.yaml b/pubspec.yaml index a0417db..40984ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_network_capabilities description: This package allows monitoring of Network Capabilities from Android and iOS devices. -version: 0.0.2 +version: 0.0.3 homepage: https://nubank.com/ repository: https://github.com/nubank/flutter-network-capabilities diff --git a/test/flutter_network_capabilities_method_channel_test.dart b/test/flutter_network_capabilities_method_channel_test.dart index 0231aae..8142867 100644 --- a/test/flutter_network_capabilities_method_channel_test.dart +++ b/test/flutter_network_capabilities_method_channel_test.dart @@ -21,4 +21,18 @@ void main() { test('getNetworkInfo', () async { expect(await platform.getNetworkInfo(), {"testing": "testing"}); }); + + test('getNetworkInfo returns empty map when native returns null', () async { + channel.setMockMethodCallHandler((MethodCall methodCall) async => null); + + expect(await platform.getNetworkInfo(), isEmpty); + }); + + test('getNetworkInfo returns empty map on PlatformException', () async { + channel.setMockMethodCallHandler((MethodCall methodCall) async { + throw PlatformException(code: 'UNAVAILABLE'); + }); + + expect(await platform.getNetworkInfo(), isEmpty); + }); }