Skip to content

Commit a7785a0

Browse files
author
Geraint White
committed
use isFlaggedAsRequested when checking local network permission
1 parent 5bb9e16 commit a7785a0

File tree

3 files changed

+14
-38
lines changed

3 files changed

+14
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
typedef enum {
2-
OptionalBoolNone,
3-
OptionalBoolYes,
4-
OptionalBoolNo,
5-
} OptionalBool;
6-
71
@interface LocalNetworkPrivacy : NSObject
82

9-
+ (OptionalBool)authorizationStatus;
103
- (void)checkAccessState:(void (^)(BOOL))completion;
114

125
@end

ios/LocalNetworkPrivacy/LocalNetworkPrivacy.m

+7-23
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ @interface LocalNetworkPrivacy () <NSNetServiceDelegate>
77
@property (nonatomic) void (^completion)(BOOL);
88
@property (nonatomic) NSTimer *timer;
99
@property (nonatomic) BOOL publishing;
10-
@property (class, nonatomic) OptionalBool granted;
1110

1211
@end
1312

1413
@implementation LocalNetworkPrivacy
1514

16-
static OptionalBool granted = OptionalBoolNone;
17-
1815
- (instancetype)init {
1916
if (self = [super init]) {
2017
self.service = [[NSNetService alloc] initWithDomain:@"local." type:@"_lnp._tcp." name:@"LocalNetworkPrivacy" port:1100];
@@ -26,36 +23,23 @@ - (void)dealloc {
2623
[self.service stop];
2724
}
2825

29-
+ (OptionalBool)authorizationStatus {
30-
return granted;
31-
}
32-
3326
- (void)checkAccessState:(void (^)(BOOL))completion {
3427
self.completion = completion;
3528

36-
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
37-
if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) {
38-
return;
39-
}
40-
41-
if (self.publishing) {
42-
granted = OptionalBoolNo;
43-
[self.timer invalidate];
44-
self.completion(NO);
45-
}
46-
else {
47-
self.publishing = YES;
48-
self.service.delegate = self;
49-
[self.service publish];
50-
}
29+
self.publishing = YES;
30+
self.service.delegate = self;
31+
[self.service publish];
32+
33+
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:NO block:^(NSTimer * _Nonnull timer) {
34+
[self.timer invalidate];
35+
self.completion(NO);
5136
}];
5237
}
5338

5439

5540
#pragma mark - NSNetServiceDelegate
5641

5742
- (void)netServiceDidPublish:(NSNetService *)sender {
58-
granted = OptionalBoolYes;
5943
[self.timer invalidate];
6044
self.completion(YES);
6145
}

ios/LocalNetworkPrivacy/RNPermissionHandlerLocalNetworkPrivacy.m

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ + (NSString * _Nonnull)handlerUniqueId {
1313

1414
- (void)checkWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
1515
rejecter:(void (__unused ^ _Nonnull)(NSError * _Nonnull))reject {
16-
switch ([LocalNetworkPrivacy authorizationStatus]) {
17-
case OptionalBoolNone:
18-
return resolve(RNPermissionStatusNotDetermined);
19-
case OptionalBoolNo:
20-
return resolve(RNPermissionStatusDenied);
21-
case OptionalBoolYes:
22-
return resolve(RNPermissionStatusAuthorized);
16+
if (![RNPermissions isFlaggedAsRequested:[[self class] handlerUniqueId]]) {
17+
return resolve(RNPermissionStatusNotDetermined);
2318
}
19+
20+
[self requestWithResolver:resolve rejecter:reject];
2421
}
2522

2623
- (void)requestWithResolver:(void (^ _Nonnull)(RNPermissionStatus))resolve
2724
rejecter:(void (^ _Nonnull)(NSError * _Nonnull))reject {
25+
[RNPermissions flagAsRequested:[[self class] handlerUniqueId]];
26+
2827
LocalNetworkPrivacy *local = [LocalNetworkPrivacy new];
2928
[local checkAccessState:^(BOOL granted) {
30-
[self checkWithResolver:resolve rejecter:reject];
29+
resolve(granted ? RNPermissionStatusAuthorized : RNPermissionStatusDenied);
3130
}];
3231
}
3332

0 commit comments

Comments
 (0)