diff --git a/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java b/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java index c59850d..40bb21c 100644 --- a/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java +++ b/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java @@ -6,6 +6,10 @@ import android.net.nsd.NsdServiceInfo; import android.net.wifi.WifiManager; import android.os.Build; +import android.Manifest; +import android.content.pm.PackageManager; +import androidx.core.content.ContextCompat; + import com.balthazargronon.RCTZeroconf.Zeroconf; import com.balthazargronon.RCTZeroconf.ZeroconfModule; @@ -46,6 +50,12 @@ public void scan(String type, String protocol, String domain) { this.stop(); + if (ContextCompat.checkSelfPermission(getReactApplicationContext(), Manifest.permission.CHANGE_WIFI_MULTICAST_STATE) != PackageManager.PERMISSION_GRANTED) { + String error = "Local network permission is not granted. Please request the necessary permission."; + zeroconfModule.sendEvent(getReactApplicationContext(), ZeroconfModule.EVENT_ERROR, error); + return; + } + if (multicastLock == null) { @SuppressLint("WifiManagerLeak") WifiManager wifi = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE); multicastLock = wifi.createMulticastLock("multicastLock"); diff --git a/ios/RNZeroconf/RNZeroconf.m b/ios/RNZeroconf/RNZeroconf.m index 1ff5371..7c2aecb 100644 --- a/ios/RNZeroconf/RNZeroconf.m +++ b/ios/RNZeroconf/RNZeroconf.m @@ -9,6 +9,9 @@ #import "RNZeroconf.h" #import "RNNetServiceSerializer.h" +#import +#import + @interface RNZeroconf () @property (nonatomic, strong, readonly) NSMutableDictionary *resolvingServices; @@ -25,6 +28,18 @@ @implementation RNZeroconf RCT_EXPORT_METHOD(scan:(NSString *)type protocol:(NSString *)protocol domain:(NSString *)domain) { [self stop]; + + if (@available(iOS 14.0, *)) { + if (CLLocationManager.locationServicesEnabled) { + CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; + if (status != kCLAuthorizationStatusAuthorizedAlways && status != kCLAuthorizationStatusAuthorizedWhenInUse) { + NSString *error = @"Local network permission is not granted. Please request the necessary permission."; + [self reportError:@{@"message": error}]; + return; + } + } + } + [self.browser searchForServicesOfType:[NSString stringWithFormat:@"_%@._%@.", type, protocol] inDomain:domain]; }