Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automatic check for missing local network access #1242

Open
wutschel opened this issue Feb 3, 2025 · 0 comments
Open

Implement automatic check for missing local network access #1242

wutschel opened this issue Feb 3, 2025 · 0 comments

Comments

@wutschel
Copy link
Collaborator

wutschel commented Feb 3, 2025

I suspect that many of the users who report the App is not working, not working after update or cannot connect simply face the problem of missing local network access. Sadly, this is sometimes not easy to resolve due ani iOS internal bug. In such case users need workaround like

  • remove and reinstall app
  • delete the network settings and restart

To improve this situation the Remote App can implement an automatic check for local network access. If not granted, a message / popup with details on how to resolve the issue can be shown. The implementation can be taken from https://stackoverflow.com/questions/67058134/objective-c-ios-14-how-to-do-network-privacy-permission-check

// LocalNetworkPrivacy.h

@interface LocalNetworkPrivacy : NSObject

- (void)checkAccessState:(void (^)(BOOL))completion;
    
@end

// LocalNetworkPrivacy.m

#import <UIKit/UIKit.h>
//#import "LocalNetworkPrivacy.h"

@interface LocalNetworkPrivacy () <NSNetServiceDelegate>

@property (nonatomic) NSNetService *service;
@property (nonatomic) void (^completion)(BOOL);
@property (nonatomic) NSTimer *timer;
@property (nonatomic) BOOL publishing;

@end

@implementation LocalNetworkPrivacy

- (instancetype)init {
    if (self = [super init]) {
        self.service = [[NSNetService alloc] initWithDomain:@"local." type:@"_lnp._tcp." name:@"LocalNetworkPrivacy" port:1100];
    }
    return self;
}

- (void)dealloc {
    [self.service stop];
}

- (void)checkAccessState:(void (^)(BOOL))completion {
    self.completion = completion;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) {
            return;
        }
        
        if (self.publishing) {
            [self.timer invalidate];
            self.completion(NO);
        }
        else {
            self.publishing = YES;
            self.service.delegate = self;
            [self.service publish];
        }
    }];
}


#pragma mark - NSNetServiceDelegate

- (void)netServiceDidPublish:(NSNetService *)sender {
    [self.timer invalidate];
    self.completion(YES);
}

@end

Calling the check:

LocalNetworkPrivacy* localNetworkPrivacy = [LocalNetworkPrivacy new];
[localNetworkPrivacy checkAccessState:^(BOOL granted) {
    NSLog(@"Granted: %@", granted ? @"YES" : @"NO");
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant