Skip to content

Instantly share code, notes, and snippets.

@geraintwhite
Created May 18, 2022 16:23
Show Gist options
  • Save geraintwhite/56b75c92a0a193d4b8793466d0f47398 to your computer and use it in GitHub Desktop.
Save geraintwhite/56b75c92a0a193d4b8793466d0f47398 to your computer and use it in GitHub Desktop.
#import "AppDelegate.h"
#import "Swift-Header.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
LocalNetworkPrivacy *local = [LocalNetworkPrivacy new];
[local checkAccessStateWithCompletion:^(BOOL granted) {
NSLog(@"Granted: %@", granted ? @"yes" : @"no");
}];
return YES;
}
@end
//
// LocalNetworkPrivacy.swift
// CloudVoiceExpress
//
// Created by Geraint White on 29/04/2022.
//
import Foundation
import UIKit
class LocalNetworkPrivacy : NSObject {
let service: NetService
var completion: ((Bool) -> Void)?
var timer: Timer?
var publishing = false
override init() {
service = .init(domain: "local.", type:"_lnp._tcp.", name: "LocalNetworkPrivacy", port: 1100)
super.init()
}
@objc
func checkAccessState(completion: @escaping (Bool) -> Void) {
self.completion = completion
timer = .scheduledTimer(withTimeInterval: 2, repeats: true, block: { timer in
guard UIApplication.shared.applicationState == .active else {
return
}
if self.publishing {
self.timer?.invalidate()
self.completion?(false)
}
else {
self.publishing = true
self.service.delegate = self
self.service.publish()
}
})
}
deinit {
service.stop()
}
}
extension LocalNetworkPrivacy : NetServiceDelegate {
func netServiceDidPublish(_ sender: NetService) {
timer?.invalidate()
completion?(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment