Add local network authorization

This commit is contained in:
Benoit Martins 2024-10-21 10:59:02 +02:00
parent 0933b71618
commit 4f3699e72b
3 changed files with 49 additions and 8 deletions

View file

@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocalNetworkUsageDescription</key>
<string>App requires access to the local network to establish VoIP connections</string>
<key>CFBundleURLTypes</key>
<array>
<dict>

View file

@ -194,7 +194,7 @@ struct PermissionsFragment: View {
}
.navigationViewStyle(StackNavigationViewStyle())
.navigationBarHidden(true)
.onReceive(permissionManager.$contactsPermissionGranted, perform: { (granted) in
.onReceive(permissionManager.$allPermissionsHaveBeenDisplayed, perform: { (granted) in
if granted {
withAnimation {
sharedMainViewModel.changeWelcomeView()

View file

@ -22,6 +22,7 @@ import Photos
import Contacts
import UserNotifications
import SwiftUI
import Network
class PermissionManager: ObservableObject {
@ -32,18 +33,28 @@ class PermissionManager: ObservableObject {
@Published var cameraPermissionGranted = false
@Published var contactsPermissionGranted = false
@Published var microphonePermissionGranted = false
@Published var allPermissionsHaveBeenDisplayed = false
private init() {}
func getPermissions() {
pushNotificationRequestPermission()
microphoneRequestPermission()
photoLibraryRequestPermission()
cameraRequestPermission()
contactsRequestPermission()
pushNotificationRequestPermission {
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
self.microphoneRequestPermission()
self.photoLibraryRequestPermission()
self.cameraRequestPermission()
self.contactsRequestPermission(group: dispatchGroup)
dispatchGroup.notify(queue: .main) {
// Now request local network authorization last
self.requestLocalNetworkAuthorization()
}
}
}
func pushNotificationRequestPermission() {
func pushNotificationRequestPermission(completion: @escaping () -> Void) {
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
if let error = error {
@ -52,6 +63,7 @@ class PermissionManager: ObservableObject {
DispatchQueue.main.async {
self.pushPermissionGranted = granted
}
completion()
}
}
@ -79,12 +91,39 @@ class PermissionManager: ObservableObject {
})
}
func contactsRequestPermission() {
func contactsRequestPermission(group: DispatchGroup) {
let store = CNContactStore()
store.requestAccess(for: .contacts) { success, _ in
DispatchQueue.main.async {
self.contactsPermissionGranted = success
}
group.leave()
}
}
func requestLocalNetworkAuthorization() {
// Use a general UDP broadcast endpoint to attempt triggering the authorization request
let host = NWEndpoint.Host("255.255.255.255") // Broadcast on the local network
let port = NWEndpoint.Port(12345) // Choose an arbitrary port
let params = NWParameters.udp
let connection = NWConnection(host: host, port: port, using: params)
connection.stateUpdateHandler = { newState in
switch newState {
case .ready:
print("Connection ready")
connection.cancel() // Close the connection after establishing it
case .failed(let error):
print("Connection failed: \(error)")
connection.cancel()
default:
break
}
}
connection.start(queue: .main)
DispatchQueue.main.async {
self.allPermissionsHaveBeenDisplayed = true
}
}
}