From 4f3699e72baef0e5cc91984d7e2b43b390b8c1fc Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Mon, 21 Oct 2024 10:59:02 +0200 Subject: [PATCH] Add local network authorization --- Linphone/Info.plist | 2 + .../Fragments/PermissionsFragment.swift | 2 +- Linphone/Utils/PermissionManager.swift | 53 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Linphone/Info.plist b/Linphone/Info.plist index fdbc8ca20..89d3fd147 100644 --- a/Linphone/Info.plist +++ b/Linphone/Info.plist @@ -2,6 +2,8 @@ + NSLocalNetworkUsageDescription + App requires access to the local network to establish VoIP connections CFBundleURLTypes diff --git a/Linphone/UI/Assistant/Fragments/PermissionsFragment.swift b/Linphone/UI/Assistant/Fragments/PermissionsFragment.swift index 40e2fdc8c..370ae5a38 100644 --- a/Linphone/UI/Assistant/Fragments/PermissionsFragment.swift +++ b/Linphone/UI/Assistant/Fragments/PermissionsFragment.swift @@ -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() diff --git a/Linphone/Utils/PermissionManager.swift b/Linphone/Utils/PermissionManager.swift index e20bc2d51..dab08971b 100644 --- a/Linphone/Utils/PermissionManager.swift +++ b/Linphone/Utils/PermissionManager.swift @@ -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 } } }