Friday, 16 April 2021

Read contacts in iOS using Swift with error handling

Just copy and paste the below code

Pre-step before pasting the below code
Add below line in AppDelegate.swift file below import statement

let kAppDelegate = UIApplication.shared.delegate as! AppDelegate

 

import Foundation
import UIKit
import Contacts

class Permission {
    
    static func askPermissionForEvent(authorised: (_ authorised: Bool) -> ()) {
        let authorizationStatus = EKEventStore.authorizationStatus(for: .event);
        switch authorizationStatus {
        case .restricted:
            openNotificationInSettings()
            
        case .denied:
            print("denied")
            
        case .authorized:
            authorised(true)
            
        default:
            authorised(true)
        }
        
    }
    
    static func openNotificationInSettings() {
        let alertController = UIAlertController(title: "Title", message: "Your message here", preferredStyle: .alert)
        let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
            guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
            if UIApplication.shared.canOpenURL(settingsUrl) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    })
                } else {
                    // Fallback on earlier versions
                }
            }
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(settingsAction)
        DispatchQueue.main.async {
            kAppDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
            
        }
    }
    
    static func requestForContactAccess(completionHandler: @escaping (_ accessGranted: Bool) -> Void) {
        let authorizationStatus = CNContactStore.authorizationStatus(for: CNEntityType.contacts)
        switch authorizationStatus {
            
        case .authorized:
            completionHandler(true)
            
        case .denied:
            completionHandler(false)
            self.openSettings()
            
        case .notDetermined:
            let store = CNContactStore()
            store.requestAccess(for: CNEntityType.contacts, completionHandler: { (access, accessError) -> Void in
                completionHandler(access)
                
                print(accessError?.localizedDescription ?? "")
            })
            
        default:
            completionHandler(false)
        }
    }
    
    static func openSettings() {
        let alertController = UIAlertController(title: "Title", message: "Your message here", preferredStyle: .alert)
        let settingsAction = UIAlertAction(title: "Settings", style: .cancel) { (_) -> Void in
            guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
            if UIApplication.shared.canOpenURL(settingsUrl) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    })
                } else {
                    // Fallback on earlier versions
                }
            }
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(settingsAction)
        DispatchQueue.main.async {
            kAppDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
            
        }
    }
}

No comments:

Post a Comment

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...