@capacitor/network 1.0.7 → 4.0.0-nightly-44c040f.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,34 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 4.0.0-beta.0 (2022-06-27)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * inline source code in esm map files ([#760](https://github.com/ionic-team/capacitor-plugins/issues/760)) ([a960489](https://github.com/ionic-team/capacitor-plugins/commit/a960489a19db0182b90d187a50deff9dfbe51038))
12
+ * Make removeAllListeners return a promise ([#895](https://github.com/ionic-team/capacitor-plugins/issues/895)) ([e5c49d6](https://github.com/ionic-team/capacitor-plugins/commit/e5c49d64445dca70286334e6a0441d8021197b13))
13
+ * **network:** Don't add window event listeners if there is no window ([#678](https://github.com/ionic-team/capacitor-plugins/issues/678)) ([0c65780](https://github.com/ionic-team/capacitor-plugins/commit/0c657808083a7f4245027a2248816b3921b813bf))
14
+ * add es2017 lib to tsconfig ([#180](https://github.com/ionic-team/capacitor-plugins/issues/180)) ([2c3776c](https://github.com/ionic-team/capacitor-plugins/commit/2c3776c38ca025c5ee965dec10ccf1cdb6c02e2f))
15
+ * better ignore rules for npm distribution ([#32](https://github.com/ionic-team/capacitor-plugins/issues/32)) ([b8d55b9](https://github.com/ionic-team/capacitor-plugins/commit/b8d55b9233e4ad7b8a1cd41110b4e580fc2a059f))
16
+ * correct addListeners links ([#655](https://github.com/ionic-team/capacitor-plugins/issues/655)) ([f9871e7](https://github.com/ionic-team/capacitor-plugins/commit/f9871e7bd53478addb21155e148829f550c0e457))
17
+ * remove postpublish scripts ([#656](https://github.com/ionic-team/capacitor-plugins/issues/656)) ([ed6ac49](https://github.com/ionic-team/capacitor-plugins/commit/ed6ac499ebf4a47525071ccbfc36c27503e11f60))
18
+ * support deprecated types from Capacitor 2 ([#139](https://github.com/ionic-team/capacitor-plugins/issues/139)) ([2d7127a](https://github.com/ionic-team/capacitor-plugins/commit/2d7127a488e26f0287951921a6db47c49d817336))
19
+
20
+
21
+ ### Features
22
+
23
+ * add commonjs output format ([#179](https://github.com/ionic-team/capacitor-plugins/issues/179)) ([8e9e098](https://github.com/ionic-team/capacitor-plugins/commit/8e9e09862064b3f6771d7facbc4008e995d9b463))
24
+ * Network plugin ([#8](https://github.com/ionic-team/capacitor-plugins/issues/8)) ([08d9891](https://github.com/ionic-team/capacitor-plugins/commit/08d9891710d576ee3c660b4d8dce89c4169d3e0b))
25
+ * set targetSDK default value to 31 ([#824](https://github.com/ionic-team/capacitor-plugins/issues/824)) ([3ee10de](https://github.com/ionic-team/capacitor-plugins/commit/3ee10de98067984c1a4e75295d001c5a895c47f4))
26
+ * set targetSDK default value to 32 ([#970](https://github.com/ionic-team/capacitor-plugins/issues/970)) ([fa70d96](https://github.com/ionic-team/capacitor-plugins/commit/fa70d96f141af751aae53ceb5642c46b204f5958))
27
+ * Upgrade gradle to 7.4 ([#826](https://github.com/ionic-team/capacitor-plugins/issues/826)) ([5db0906](https://github.com/ionic-team/capacitor-plugins/commit/5db0906f6264287c4f8e69dbaecf19d4d387824b))
28
+ * Use java 11 ([#910](https://github.com/ionic-team/capacitor-plugins/issues/910)) ([5acb2a2](https://github.com/ionic-team/capacitor-plugins/commit/5acb2a288a413492b163e4e97da46a085d9e4be0))
29
+
30
+
31
+
32
+
33
+
6
34
  ## [1.0.7](https://github.com/ionic-team/capacitor-plugins/compare/@capacitor/network@1.0.6...@capacitor/network@1.0.7) (2022-01-19)
7
35
 
8
36
 
@@ -11,8 +11,7 @@ Pod::Spec.new do |s|
11
11
  s.author = package['author']
12
12
  s.source = { :git => 'https://github.com/ionic-team/capacitor-plugins.git', :tag => package['name'] + '@' + package['version'] }
13
13
  s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}', 'network/ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
14
- s.ios.deployment_target = '12.0'
14
+ s.ios.deployment_target = '13.0'
15
15
  s.dependency 'Capacitor'
16
- s.dependency 'ReachabilitySwift', '~> 5.0'
17
16
  s.swift_version = '5.1'
18
17
  end
@@ -1,8 +1,8 @@
1
1
  ext {
2
- junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.1'
3
- androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.2.0'
4
- androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.2'
5
- androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.3.0'
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.4.2'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.3'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.4.0'
6
6
  }
7
7
 
8
8
  buildscript {
@@ -11,17 +11,17 @@ buildscript {
11
11
  mavenCentral()
12
12
  }
13
13
  dependencies {
14
- classpath 'com.android.tools.build:gradle:4.2.1'
14
+ classpath 'com.android.tools.build:gradle:7.2.1'
15
15
  }
16
16
  }
17
17
 
18
18
  apply plugin: 'com.android.library'
19
19
 
20
20
  android {
21
- compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 30
21
+ compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 32
22
22
  defaultConfig {
23
- minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
24
- targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 30
23
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
24
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 32
25
25
  versionCode 1
26
26
  versionName "1.0"
27
27
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -36,15 +36,14 @@ android {
36
36
  abortOnError false
37
37
  }
38
38
  compileOptions {
39
- sourceCompatibility JavaVersion.VERSION_1_8
40
- targetCompatibility JavaVersion.VERSION_1_8
39
+ sourceCompatibility JavaVersion.VERSION_11
40
+ targetCompatibility JavaVersion.VERSION_11
41
41
  }
42
42
  }
43
43
 
44
44
  repositories {
45
45
  google()
46
46
  mavenCentral()
47
- jcenter()
48
47
  }
49
48
 
50
49
 
@@ -1,13 +1,17 @@
1
1
  package com.capacitorjs.plugins.network;
2
2
 
3
+ import android.annotation.TargetApi;
3
4
  import android.content.BroadcastReceiver;
4
5
  import android.content.Context;
5
6
  import android.content.Intent;
6
7
  import android.content.IntentFilter;
7
8
  import android.net.ConnectivityManager;
8
- import android.net.NetworkInfo;
9
+ import android.net.ConnectivityManager.NetworkCallback;
10
+ import android.net.NetworkCapabilities;
11
+ import android.os.Build;
9
12
  import androidx.annotation.NonNull;
10
13
  import androidx.annotation.Nullable;
14
+ import androidx.annotation.RequiresApi;
11
15
  import androidx.appcompat.app.AppCompatActivity;
12
16
 
13
17
  public class Network {
@@ -16,12 +20,29 @@ public class Network {
16
20
  * Interface for callbacks when network status changes.
17
21
  */
18
22
  interface NetworkStatusChangeListener {
19
- void onNetworkStatusChanged();
23
+ void onNetworkStatusChanged(boolean wasLostEvent);
24
+ }
25
+
26
+ class ConnectivityCallback extends NetworkCallback {
27
+
28
+ @Override
29
+ public void onLost(@NonNull android.net.Network network) {
30
+ super.onLost(network);
31
+ statusChangeListener.onNetworkStatusChanged(true);
32
+ }
33
+
34
+ @Override
35
+ public void onCapabilitiesChanged(@NonNull android.net.Network network, @NonNull NetworkCapabilities networkCapabilities) {
36
+ super.onCapabilitiesChanged(network, networkCapabilities);
37
+ statusChangeListener.onNetworkStatusChanged(false);
38
+ }
20
39
  }
21
40
 
22
41
  @Nullable
23
42
  private NetworkStatusChangeListener statusChangeListener;
24
43
 
44
+ private ConnectivityCallback connectivityCallback;
45
+ private Context context;
25
46
  private ConnectivityManager connectivityManager;
26
47
  private BroadcastReceiver receiver;
27
48
 
@@ -29,15 +50,21 @@ public class Network {
29
50
  * Create network monitoring object.
30
51
  * @param context
31
52
  */
53
+ @SuppressWarnings("deprecation")
32
54
  public Network(@NonNull Context context) {
33
- connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
34
- receiver =
35
- new BroadcastReceiver() {
36
- @Override
37
- public void onReceive(Context context, Intent intent) {
38
- statusChangeListener.onNetworkStatusChanged();
39
- }
40
- };
55
+ this.context = context;
56
+ this.connectivityManager = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
57
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
58
+ receiver =
59
+ new BroadcastReceiver() {
60
+ @Override
61
+ public void onReceive(Context context, Intent intent) {
62
+ statusChangeListener.onNetworkStatusChanged(false);
63
+ }
64
+ };
65
+ } else {
66
+ this.connectivityCallback = new ConnectivityCallback();
67
+ }
41
68
  }
42
69
 
43
70
  /**
@@ -59,25 +86,73 @@ public class Network {
59
86
 
60
87
  /**
61
88
  * Get the current network information.
62
- * @return NetworkInfo
89
+ * @return NetworkStatus
63
90
  */
64
- public NetworkInfo getNetworkStatus() {
65
- return connectivityManager.getActiveNetworkInfo();
91
+ public NetworkStatus getNetworkStatus() {
92
+ NetworkStatus networkStatus = new NetworkStatus();
93
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
94
+ if (this.connectivityManager != null) {
95
+ android.net.Network activeNetwork = this.connectivityManager.getActiveNetwork();
96
+ NetworkCapabilities capabilities =
97
+ this.connectivityManager.getNetworkCapabilities(this.connectivityManager.getActiveNetwork());
98
+ if (activeNetwork != null && capabilities != null) {
99
+ networkStatus.connected =
100
+ capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) &&
101
+ capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
102
+ if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
103
+ networkStatus.connectionType = NetworkStatus.ConnectionType.WIFI;
104
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
105
+ networkStatus.connectionType = NetworkStatus.ConnectionType.CELLULAR;
106
+ } else {
107
+ networkStatus.connectionType = NetworkStatus.ConnectionType.UNKNOWN;
108
+ }
109
+ }
110
+ }
111
+ } else {
112
+ networkStatus = getAndParseNetworkInfo();
113
+ }
114
+ return networkStatus;
115
+ }
116
+
117
+ @SuppressWarnings("deprecation")
118
+ private NetworkStatus getAndParseNetworkInfo() {
119
+ NetworkStatus networkStatus = new NetworkStatus();
120
+ android.net.NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
121
+ if (networkInfo != null) {
122
+ networkStatus.connected = networkInfo.isConnected();
123
+ String typeName = networkInfo.getTypeName();
124
+ if (typeName.equals("WIFI")) {
125
+ networkStatus.connectionType = NetworkStatus.ConnectionType.WIFI;
126
+ } else if (typeName.equals("MOBILE")) {
127
+ networkStatus.connectionType = NetworkStatus.ConnectionType.CELLULAR;
128
+ }
129
+ }
130
+ return networkStatus;
66
131
  }
67
132
 
68
133
  /**
69
- * Register a Intent Receiver in the activity.
70
- * @param activity
134
+ * Register a network callback.
71
135
  */
72
- public void startMonitoring(@NonNull AppCompatActivity activity) {
73
- IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
136
+ @RequiresApi(api = Build.VERSION_CODES.N)
137
+ public void startMonitoring() {
138
+ connectivityManager.registerDefaultNetworkCallback(connectivityCallback);
139
+ }
140
+
141
+ @TargetApi(Build.VERSION_CODES.M)
142
+ public void startMonitoring(AppCompatActivity activity) {
143
+ IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
74
144
  activity.registerReceiver(receiver, filter);
75
145
  }
76
146
 
77
147
  /**
78
- * Unregister the IntentReceiver to avoid leaking it.
79
- * @param activity
148
+ * Unregister the network callback.
80
149
  */
150
+ @RequiresApi(api = Build.VERSION_CODES.N)
151
+ public void stopMonitoring() {
152
+ connectivityManager.unregisterNetworkCallback(connectivityCallback);
153
+ }
154
+
155
+ @TargetApi(Build.VERSION_CODES.M)
81
156
  public void stopMonitoring(@NonNull AppCompatActivity activity) {
82
157
  activity.unregisterReceiver(receiver);
83
158
  }
@@ -1,14 +1,11 @@
1
1
  package com.capacitorjs.plugins.network;
2
2
 
3
- import android.Manifest;
4
- import android.net.NetworkInfo;
3
+ import android.os.Build;
5
4
  import com.getcapacitor.JSObject;
6
- import com.getcapacitor.Logger;
7
5
  import com.getcapacitor.Plugin;
8
6
  import com.getcapacitor.PluginCall;
9
7
  import com.getcapacitor.PluginMethod;
10
8
  import com.getcapacitor.annotation.CapacitorPlugin;
11
- import com.getcapacitor.annotation.Permission;
12
9
 
13
10
  @CapacitorPlugin(name = "Network")
14
11
  public class NetworkPlugin extends Plugin {
@@ -22,7 +19,17 @@ public class NetworkPlugin extends Plugin {
22
19
  @Override
23
20
  public void load() {
24
21
  implementation = new Network(getContext());
25
- implementation.setStatusChangeListener(this::updateNetworkStatus);
22
+ Network.NetworkStatusChangeListener listener = wasLostEvent -> {
23
+ if (wasLostEvent) {
24
+ JSObject jsObject = new JSObject();
25
+ jsObject.put("connected", false);
26
+ jsObject.put("connectionType", "none");
27
+ notifyListeners(NETWORK_CHANGE_EVENT, jsObject);
28
+ } else {
29
+ updateNetworkStatus();
30
+ }
31
+ };
32
+ implementation.setStatusChangeListener(listener);
26
33
  }
27
34
 
28
35
  /**
@@ -39,7 +46,7 @@ public class NetworkPlugin extends Plugin {
39
46
  */
40
47
  @PluginMethod
41
48
  public void getStatus(PluginCall call) {
42
- call.resolve(getStatusJSObject(implementation.getNetworkStatus()));
49
+ call.resolve(parseNetworkStatus(implementation.getNetworkStatus()));
43
50
  }
44
51
 
45
52
  /**
@@ -47,7 +54,11 @@ public class NetworkPlugin extends Plugin {
47
54
  */
48
55
  @Override
49
56
  protected void handleOnResume() {
50
- implementation.startMonitoring(getActivity());
57
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
58
+ implementation.startMonitoring();
59
+ } else {
60
+ implementation.startMonitoring(getActivity());
61
+ }
51
62
  }
52
63
 
53
64
  /**
@@ -55,43 +66,21 @@ public class NetworkPlugin extends Plugin {
55
66
  */
56
67
  @Override
57
68
  protected void handleOnPause() {
58
- implementation.stopMonitoring(getActivity());
69
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
70
+ implementation.stopMonitoring();
71
+ } else {
72
+ implementation.stopMonitoring(getActivity());
73
+ }
59
74
  }
60
75
 
61
76
  private void updateNetworkStatus() {
62
- notifyListeners(NETWORK_CHANGE_EVENT, getStatusJSObject(implementation.getNetworkStatus()));
63
- }
64
-
65
- /**
66
- * Transform a NetworkInfo object into our JSObject for returning to client
67
- * @param info
68
- * @return
69
- */
70
- private JSObject getStatusJSObject(NetworkInfo info) {
71
- JSObject ret = new JSObject();
72
- if (info == null) {
73
- ret.put("connected", false);
74
- ret.put("connectionType", "none");
75
- } else {
76
- ret.put("connected", info.isConnected());
77
- ret.put("connectionType", getNormalizedTypeName(info));
78
- }
79
- return ret;
77
+ notifyListeners(NETWORK_CHANGE_EVENT, parseNetworkStatus(implementation.getNetworkStatus()));
80
78
  }
81
79
 
82
- /**
83
- * Convert the Android-specific naming for network types into our cross-platform type
84
- * @param info
85
- * @return
86
- */
87
- private String getNormalizedTypeName(NetworkInfo info) {
88
- String typeName = info.getTypeName();
89
- if (typeName.equals("WIFI")) {
90
- return "wifi";
91
- }
92
- if (typeName.equals("MOBILE")) {
93
- return "cellular";
94
- }
95
- return "none";
80
+ private JSObject parseNetworkStatus(NetworkStatus networkStatus) {
81
+ JSObject jsObject = new JSObject();
82
+ jsObject.put("connected", networkStatus.connected);
83
+ jsObject.put("connectionType", networkStatus.connectionType.getConnectionType());
84
+ return jsObject;
96
85
  }
97
86
  }
@@ -0,0 +1,24 @@
1
+ package com.capacitorjs.plugins.network;
2
+
3
+ public class NetworkStatus {
4
+
5
+ public enum ConnectionType {
6
+ WIFI("wifi"),
7
+ CELLULAR("cellular"),
8
+ NONE("none"),
9
+ UNKNOWN("unknown");
10
+
11
+ private String connectionType;
12
+
13
+ ConnectionType(String connectionType) {
14
+ this.connectionType = connectionType;
15
+ }
16
+
17
+ public String getConnectionType() {
18
+ return this.connectionType;
19
+ }
20
+ }
21
+
22
+ public boolean connected = false;
23
+ public ConnectionType connectionType = ConnectionType.NONE;
24
+ }
@@ -1,5 +1,4 @@
1
1
  import Foundation
2
- import Reachability
3
2
 
4
3
  public typealias NetworkConnectionChangedObserver = (Network.Connection) -> Void
5
4
 
@@ -37,7 +36,7 @@ public class Network {
37
36
  fileprivate extension Reachability.Connection {
38
37
  var equivalentEnum: Network.Connection {
39
38
  switch self {
40
- case .unavailable, .none:
39
+ case .unavailable:
41
40
  return .unavailable
42
41
  case .wifi:
43
42
  return .wifi
@@ -5,5 +5,5 @@
5
5
  // each method the plugin supports using the CAP_PLUGIN_METHOD macro.
6
6
  CAP_PLUGIN(CAPNetworkPlugin, "Network",
7
7
  CAP_PLUGIN_METHOD(getStatus, CAPPluginReturnPromise);
8
- CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
8
+ CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnPromise);
9
9
  )
@@ -0,0 +1,395 @@
1
+ /*
2
+ Copyright (c) 2014, Ashley Mills
3
+ All rights reserved.
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ 1. Redistributions of source code must retain the above copyright notice, this
7
+ list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ this list of conditions and the following disclaimer in the documentation
10
+ and/or other materials provided with the distribution.
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
15
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21
+ POSSIBILITY OF SUCH DAMAGE.
22
+ */
23
+
24
+ import SystemConfiguration
25
+ import Foundation
26
+
27
+ public enum ReachabilityError: Error {
28
+ case failedToCreateWithAddress(sockaddr, Int32)
29
+ case failedToCreateWithHostname(String, Int32)
30
+ case unableToSetCallback(Int32)
31
+ case unableToSetDispatchQueue(Int32)
32
+ case unableToGetFlags(Int32)
33
+ }
34
+
35
+ @available(*, unavailable, renamed: "Notification.Name.reachabilityChanged")
36
+ public let reachabilityChangedNotification = NSNotification.Name("ReachabilityChangedNotification")
37
+
38
+ public extension Notification.Name {
39
+ static let reachabilityChanged = Notification.Name("reachabilityChanged")
40
+ }
41
+
42
+ public class Reachability {
43
+
44
+ public typealias NetworkReachable = (Reachability) -> Void
45
+ public typealias NetworkUnreachable = (Reachability) -> Void
46
+
47
+ @available(*, unavailable, renamed: "Connection")
48
+ public enum NetworkStatus: CustomStringConvertible {
49
+ case notReachable, reachableViaWiFi, reachableViaWWAN
50
+ public var description: String {
51
+ switch self {
52
+ case .reachableViaWWAN: return "Cellular"
53
+ case .reachableViaWiFi: return "WiFi"
54
+ case .notReachable: return "No Connection"
55
+ }
56
+ }
57
+ }
58
+
59
+ public enum Connection: CustomStringConvertible {
60
+ case unavailable, wifi, cellular
61
+ public var description: String {
62
+ switch self {
63
+ case .cellular: return "Cellular"
64
+ case .wifi: return "WiFi"
65
+ case .unavailable: return "No Connection"
66
+ }
67
+ }
68
+
69
+ @available(*, deprecated, renamed: "unavailable")
70
+ public static let none: Connection = .unavailable
71
+ }
72
+
73
+ public var whenReachable: NetworkReachable?
74
+ public var whenUnreachable: NetworkUnreachable?
75
+
76
+ @available(*, deprecated, renamed: "allowsCellularConnection")
77
+ public let reachableOnWWAN: Bool = true
78
+
79
+ /// Set to `false` to force Reachability.connection to .none when on cellular connection (default value `true`)
80
+ public var allowsCellularConnection: Bool
81
+
82
+ // The notification center on which "reachability changed" events are being posted
83
+ public var notificationCenter: NotificationCenter = NotificationCenter.default
84
+
85
+ @available(*, deprecated, renamed: "connection.description")
86
+ public var currentReachabilityString: String {
87
+ return "\(connection)"
88
+ }
89
+
90
+ @available(*, unavailable, renamed: "connection")
91
+ public var currentReachabilityStatus: Connection {
92
+ return connection
93
+ }
94
+
95
+ public var connection: Connection {
96
+ if flags == nil {
97
+ try? setReachabilityFlags()
98
+ }
99
+
100
+ switch flags?.connection {
101
+ case .unavailable?, nil: return .unavailable
102
+ case .cellular?: return allowsCellularConnection ? .cellular : .unavailable
103
+ case .wifi?: return .wifi
104
+ }
105
+ }
106
+
107
+ fileprivate var isRunningOnDevice: Bool = {
108
+ #if targetEnvironment(simulator)
109
+ return false
110
+ #else
111
+ return true
112
+ #endif
113
+ }()
114
+
115
+ fileprivate(set) var notifierRunning = false
116
+ fileprivate let reachabilityRef: SCNetworkReachability
117
+ fileprivate let reachabilitySerialQueue: DispatchQueue
118
+ fileprivate let notificationQueue: DispatchQueue?
119
+ fileprivate(set) var flags: SCNetworkReachabilityFlags? {
120
+ didSet {
121
+ guard flags != oldValue else { return }
122
+ notifyReachabilityChanged()
123
+ }
124
+ }
125
+
126
+ public required init(reachabilityRef: SCNetworkReachability,
127
+ queueQoS: DispatchQoS = .default,
128
+ targetQueue: DispatchQueue? = nil,
129
+ notificationQueue: DispatchQueue? = .main) {
130
+ self.allowsCellularConnection = true
131
+ self.reachabilityRef = reachabilityRef
132
+ self.reachabilitySerialQueue = DispatchQueue(label: "uk.co.ashleymills.reachability", qos: queueQoS, target: targetQueue)
133
+ self.notificationQueue = notificationQueue
134
+ }
135
+
136
+ public convenience init(hostname: String,
137
+ queueQoS: DispatchQoS = .default,
138
+ targetQueue: DispatchQueue? = nil,
139
+ notificationQueue: DispatchQueue? = .main) throws {
140
+ guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else {
141
+ throw ReachabilityError.failedToCreateWithHostname(hostname, SCError())
142
+ }
143
+ self.init(reachabilityRef: ref, queueQoS: queueQoS, targetQueue: targetQueue, notificationQueue: notificationQueue)
144
+ }
145
+
146
+ public convenience init(queueQoS: DispatchQoS = .default,
147
+ targetQueue: DispatchQueue? = nil,
148
+ notificationQueue: DispatchQueue? = .main) throws {
149
+ var zeroAddress = sockaddr()
150
+ zeroAddress.sa_len = UInt8(MemoryLayout<sockaddr>.size)
151
+ zeroAddress.sa_family = sa_family_t(AF_INET)
152
+
153
+ guard let ref = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else {
154
+ throw ReachabilityError.failedToCreateWithAddress(zeroAddress, SCError())
155
+ }
156
+
157
+ self.init(reachabilityRef: ref, queueQoS: queueQoS, targetQueue: targetQueue, notificationQueue: notificationQueue)
158
+ }
159
+
160
+ deinit {
161
+ stopNotifier()
162
+ }
163
+ }
164
+
165
+ public extension Reachability {
166
+
167
+ // MARK: - *** Notifier methods ***
168
+ func startNotifier() throws {
169
+ guard !notifierRunning else { return }
170
+
171
+ let callback: SCNetworkReachabilityCallBack = { (reachability, flags, info) in
172
+ guard let info = info else { return }
173
+
174
+ // `weakifiedReachability` is guaranteed to exist by virtue of our
175
+ // retain/release callbacks which we provided to the `SCNetworkReachabilityContext`.
176
+ let weakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info).takeUnretainedValue()
177
+
178
+ // The weak `reachability` _may_ no longer exist if the `Reachability`
179
+ // object has since been deallocated but a callback was already in flight.
180
+ weakifiedReachability.reachability?.flags = flags
181
+ }
182
+
183
+ let weakifiedReachability = ReachabilityWeakifier(reachability: self)
184
+ let opaqueWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.passUnretained(weakifiedReachability).toOpaque()
185
+
186
+ var context = SCNetworkReachabilityContext(
187
+ version: 0,
188
+ info: UnsafeMutableRawPointer(opaqueWeakifiedReachability),
189
+ retain: { (info: UnsafeRawPointer) -> UnsafeRawPointer in
190
+ let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
191
+ _ = unmanagedWeakifiedReachability.retain()
192
+ return UnsafeRawPointer(unmanagedWeakifiedReachability.toOpaque())
193
+ },
194
+ release: { (info: UnsafeRawPointer) -> Void in
195
+ let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
196
+ unmanagedWeakifiedReachability.release()
197
+ },
198
+ copyDescription: { (info: UnsafeRawPointer) -> Unmanaged<CFString> in
199
+ let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
200
+ let weakifiedReachability = unmanagedWeakifiedReachability.takeUnretainedValue()
201
+ let description = weakifiedReachability.reachability?.description ?? "nil"
202
+ return Unmanaged.passRetained(description as CFString)
203
+ }
204
+ )
205
+
206
+ if !SCNetworkReachabilitySetCallback(reachabilityRef, callback, &context) {
207
+ stopNotifier()
208
+ throw ReachabilityError.unableToSetCallback(SCError())
209
+ }
210
+
211
+ if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef, reachabilitySerialQueue) {
212
+ stopNotifier()
213
+ throw ReachabilityError.unableToSetDispatchQueue(SCError())
214
+ }
215
+
216
+ // Perform an initial check
217
+ try setReachabilityFlags()
218
+
219
+ notifierRunning = true
220
+ }
221
+
222
+ func stopNotifier() {
223
+ defer { notifierRunning = false }
224
+
225
+ SCNetworkReachabilitySetCallback(reachabilityRef, nil, nil)
226
+ SCNetworkReachabilitySetDispatchQueue(reachabilityRef, nil)
227
+ }
228
+
229
+ // MARK: - *** Connection test methods ***
230
+ @available(*, deprecated, message: "Please use `connection != .none`")
231
+ var isReachable: Bool {
232
+ return connection != .unavailable
233
+ }
234
+
235
+ @available(*, deprecated, message: "Please use `connection == .cellular`")
236
+ var isReachableViaWWAN: Bool {
237
+ // Check we're not on the simulator, we're REACHABLE and check we're on WWAN
238
+ return connection == .cellular
239
+ }
240
+
241
+ @available(*, deprecated, message: "Please use `connection == .wifi`")
242
+ var isReachableViaWiFi: Bool {
243
+ return connection == .wifi
244
+ }
245
+
246
+ var description: String {
247
+ return flags?.description ?? "unavailable flags"
248
+ }
249
+ }
250
+
251
+ fileprivate extension Reachability {
252
+
253
+ func setReachabilityFlags() throws {
254
+ try reachabilitySerialQueue.sync { [unowned self] in
255
+ var flags = SCNetworkReachabilityFlags()
256
+ if !SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags) {
257
+ self.stopNotifier()
258
+ throw ReachabilityError.unableToGetFlags(SCError())
259
+ }
260
+
261
+ self.flags = flags
262
+ }
263
+ }
264
+
265
+ func notifyReachabilityChanged() {
266
+ let notify = { [weak self] in
267
+ guard let self = self else { return }
268
+ self.connection != .unavailable ? self.whenReachable?(self) : self.whenUnreachable?(self)
269
+ self.notificationCenter.post(name: .reachabilityChanged, object: self)
270
+ }
271
+
272
+ // notify on the configured `notificationQueue`, or the caller's (i.e. `reachabilitySerialQueue`)
273
+ notificationQueue?.async(execute: notify) ?? notify()
274
+ }
275
+ }
276
+
277
+ extension SCNetworkReachabilityFlags {
278
+
279
+ typealias Connection = Reachability.Connection
280
+
281
+ var connection: Connection {
282
+ guard isReachableFlagSet else { return .unavailable }
283
+
284
+ // If we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
285
+ #if targetEnvironment(simulator)
286
+ return .wifi
287
+ #else
288
+ var connection = Connection.unavailable
289
+
290
+ if !isConnectionRequiredFlagSet {
291
+ connection = .wifi
292
+ }
293
+
294
+ if isConnectionOnTrafficOrDemandFlagSet {
295
+ if !isInterventionRequiredFlagSet {
296
+ connection = .wifi
297
+ }
298
+ }
299
+
300
+ if isOnWWANFlagSet {
301
+ connection = .cellular
302
+ }
303
+
304
+ return connection
305
+ #endif
306
+ }
307
+
308
+ var isOnWWANFlagSet: Bool {
309
+ #if os(iOS)
310
+ return contains(.isWWAN)
311
+ #else
312
+ return false
313
+ #endif
314
+ }
315
+ var isReachableFlagSet: Bool {
316
+ return contains(.reachable)
317
+ }
318
+ var isConnectionRequiredFlagSet: Bool {
319
+ return contains(.connectionRequired)
320
+ }
321
+ var isInterventionRequiredFlagSet: Bool {
322
+ return contains(.interventionRequired)
323
+ }
324
+ var isConnectionOnTrafficFlagSet: Bool {
325
+ return contains(.connectionOnTraffic)
326
+ }
327
+ var isConnectionOnDemandFlagSet: Bool {
328
+ return contains(.connectionOnDemand)
329
+ }
330
+ var isConnectionOnTrafficOrDemandFlagSet: Bool {
331
+ return !intersection([.connectionOnTraffic, .connectionOnDemand]).isEmpty
332
+ }
333
+ var isTransientConnectionFlagSet: Bool {
334
+ return contains(.transientConnection)
335
+ }
336
+ var isLocalAddressFlagSet: Bool {
337
+ return contains(.isLocalAddress)
338
+ }
339
+ var isDirectFlagSet: Bool {
340
+ return contains(.isDirect)
341
+ }
342
+ var isConnectionRequiredAndTransientFlagSet: Bool {
343
+ return intersection([.connectionRequired, .transientConnection]) == [.connectionRequired, .transientConnection]
344
+ }
345
+
346
+ // swiftlint:disable identifier_name
347
+ var description: String {
348
+ let W = isOnWWANFlagSet ? "W" : "-"
349
+ let R = isReachableFlagSet ? "R" : "-"
350
+ let c = isConnectionRequiredFlagSet ? "c" : "-"
351
+ let t = isTransientConnectionFlagSet ? "t" : "-"
352
+ let i = isInterventionRequiredFlagSet ? "i" : "-"
353
+ let C = isConnectionOnTrafficFlagSet ? "C" : "-"
354
+ let D = isConnectionOnDemandFlagSet ? "D" : "-"
355
+ let l = isLocalAddressFlagSet ? "l" : "-"
356
+ let d = isDirectFlagSet ? "d" : "-"
357
+
358
+ return "\(W)\(R) \(c)\(t)\(i)\(C)\(D)\(l)\(d)"
359
+ }
360
+ // swiftlint:enable identifier_name
361
+ }
362
+
363
+ /**
364
+ `ReachabilityWeakifier` weakly wraps the `Reachability` class
365
+ in order to break retain cycles when interacting with CoreFoundation.
366
+ CoreFoundation callbacks expect a pair of retain/release whenever an
367
+ opaque `info` parameter is provided. These callbacks exist to guard
368
+ against memory management race conditions when invoking the callbacks.
369
+ #### Race Condition
370
+ If we passed `SCNetworkReachabilitySetCallback` a direct reference to our
371
+ `Reachability` class without also providing corresponding retain/release
372
+ callbacks, then a race condition can lead to crashes when:
373
+ - `Reachability` is deallocated on thread X
374
+ - A `SCNetworkReachability` callback(s) is already in flight on thread Y
375
+ #### Retain Cycle
376
+ If we pass `Reachability` to CoreFoundtion while also providing retain/
377
+ release callbacks, we would create a retain cycle once CoreFoundation
378
+ retains our `Reachability` class. This fixes the crashes and his how
379
+ CoreFoundation expects the API to be used, but doesn't play nicely with
380
+ Swift/ARC. This cycle would only be broken after manually calling
381
+ `stopNotifier()` — `deinit` would never be called.
382
+ #### ReachabilityWeakifier
383
+ By providing both retain/release callbacks and wrapping `Reachability` in
384
+ a weak wrapper, we:
385
+ - interact correctly with CoreFoundation, thereby avoiding a crash.
386
+ See "Memory Management Programming Guide for Core Foundation".
387
+ - don't alter the public API of `Reachability.swift` in any way
388
+ - still allow for automatic stopping of the notifier on `deinit`.
389
+ */
390
+ private class ReachabilityWeakifier {
391
+ weak var reachability: Reachability?
392
+ init(reachability: Reachability) {
393
+ self.reachability = reachability
394
+ }
395
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/network",
3
- "version": "1.0.7",
3
+ "version": "4.0.0-nightly-44c040f.0",
4
4
  "description": "The Network API provides network and connectivity information.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -29,7 +29,7 @@
29
29
  ],
30
30
  "scripts": {
31
31
  "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
32
- "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin && cd ..",
32
+ "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
33
33
  "verify:android": "cd android && ./gradlew clean build test && cd ..",
34
34
  "verify:web": "npm run build",
35
35
  "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
@@ -45,10 +45,10 @@
45
45
  "publish:cocoapod": "pod trunk push ./CapacitorNetwork.podspec --allow-warnings"
46
46
  },
47
47
  "devDependencies": {
48
- "@capacitor/android": "^3.0.0",
49
- "@capacitor/core": "^3.0.0",
48
+ "@capacitor/android": "next",
49
+ "@capacitor/core": "next",
50
50
  "@capacitor/docgen": "0.0.18",
51
- "@capacitor/ios": "^3.0.0",
51
+ "@capacitor/ios": "next",
52
52
  "@ionic/eslint-config": "^0.3.0",
53
53
  "@ionic/prettier-config": "~1.0.1",
54
54
  "@ionic/swiftlint-config": "^1.1.2",
@@ -61,7 +61,7 @@
61
61
  "typescript": "~4.1.5"
62
62
  },
63
63
  "peerDependencies": {
64
- "@capacitor/core": "^3.0.0"
64
+ "@capacitor/core": "next"
65
65
  },
66
66
  "prettier": "@ionic/prettier-config",
67
67
  "swiftlint": "@ionic/swiftlint-config",
@@ -79,5 +79,5 @@
79
79
  "publishConfig": {
80
80
  "access": "public"
81
81
  },
82
- "gitHead": "f18ee6482e1ec8e5fb0290d51f598397133af990"
82
+ "gitHead": "44c040f725a76414eaf5d922ffd64aa9d0c32ec6"
83
83
  }