Skip to content
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
47 changes: 8 additions & 39 deletions ios/LocalNetworkPrivacy/LocalNetworkPrivacy.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ @interface LocalNetworkPrivacy () <NSNetServiceDelegate>

@implementation LocalNetworkPrivacy

static NSString * const kLocalNetworkPermissionRequestedKey = @"LocalNetworkPermissionRequested";

- (instancetype)init {
if (self = [super init]) {
self.service = [[NSNetService alloc] initWithDomain:@"local." type:@"_lnp._tcp." name:@"LocalNetworkPrivacy" port:1100];
Expand All @@ -28,50 +26,21 @@ - (void)dealloc {
- (void)checkAccessState:(void (^)(BOOL))completion {
self.completion = completion;

BOOL permissionRequestedBefore = [[NSUserDefaults standardUserDefaults] boolForKey:kLocalNetworkPermissionRequestedKey];

if (!permissionRequestedBefore) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLocalNetworkPermissionRequestedKey];

self.publishing = YES;
self.service.delegate = self;
[self.service publish];
self.publishing = YES;
self.service.delegate = self;
[self.service publish];

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) {
if (!self.publishing) {
[self completeWithResult:self.service.includesPeerToPeer];
}
}];
} else {
self.publishing = YES;
self.service.delegate = self;
[self.service publish];

self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:NO block:^(NSTimer * _Nonnull timer) {
[self completeWithResult:NO];
}];
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:NO block:^(NSTimer * _Nonnull timer) {
[self.timer invalidate];
self.completion(NO);
}];
}

#pragma mark - NSNetServiceDelegate

- (void)netServiceDidPublish:(NSNetService *)sender {
self.publishing = NO;
[self completeWithResult:YES];
}

- (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary<NSString *,NSNumber *> *)errorDict {
self.publishing = NO;
[self completeWithResult:NO];
}

#pragma mark - Private Methods

- (void)completeWithResult:(BOOL)result {
[self.timer invalidate];
if (self.completion) {
self.completion(result);
}
self.completion(YES);
}

@end
10 changes: 7 additions & 3 deletions ios/LocalNetworkPrivacy/RNPermissionHandlerLocalNetworkPrivacy.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ - (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve

- (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
LocalNetworkPrivacy *local = [LocalNetworkPrivacy new];
if (![RNPermissionsHelper isFlaggedAsRequested:[[self class] handlerUniqueId]]) {
// This will trigger the local network permission native dialog
[local checkAccessState:^(BOOL granted) {
// Ignoring result for the first time. We just want iOS to initiate the permission request
}];
[RNPermissionsHelper flagAsRequested:[[self class] handlerUniqueId]];

// We can't get the permission dialog result, therefor returning not determined status
return resolve(RNPermissionStatusNotDetermined);
}

[RNPermissionsHelper flagAsRequested:[[self class] handlerUniqueId]];

LocalNetworkPrivacy *local = [LocalNetworkPrivacy new];
[local checkAccessState:^(BOOL granted) {
resolve(granted ? RNPermissionStatusAuthorized : RNPermissionStatusDenied);
}];
Expand Down