Property in Swift - Computed property | Property Observers (WillSet & DidSet)

   Computed property -  Computed properties are part of a property type in Swift.  Stored properties are the most common which save and return a stored value  Computed properties calculate (rather than store) a value. A computed property provides a getter and an optional setter to indirectly access other properties and values. Computed property in swift is an interesting topic as you can write code inside a property in swift. Computed properties are provided by classes, structures, and enumerations but stored properties are provided only by classes and structures. This computed property swift tutorial has some tips on how to use the computed property in the right way and avoid the common mistakes that swift programming beginners do while computed property. Example :- Computed property A Calculate Simple Intrest struct CalculateLoan {      var amount : Int      var rate : Int      var years : Int      var simpleInterest: Int {          get {              return ( amount * rate

Push Notifications Service - Local Push Notifications Swift 5

How to create local notifications in iOS using swift 5 on click a custom Button. Local notifications are most commonly used in apps like calendar alert, alarm app set event and more Alert with a customized message, a to do reminder etc. So let us start and learn how we can integrate local notification in iOS using swift language. Follow below steps, to integrate local notifications in swift. 

Push Notifications Service - Local Push Notifications Swift 5


Local and Remote Notification Programming Guide - Local notifications and remote notifications are ways to inform users when new data becomes available for your app - Local and Remote Notifications Overview


Local Notifications: Now Getting Started:- 


Step 1:  


import UserNotifications

 

Step 2:


    override func viewDidLoad() {

        super.viewDidLoad()

        // Configure User Notification Center

            UNUserNotificationCenter.current().delegate = self

    }


Step 3:


@IBAction func didLocalNotification(_ sender: Any) {

        // Request to notification settings

        UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in

            switch notificationSettings.authorizationStatus {

                case .notDetermined:

                    self.requestAuthorization(completionHandler: { (success) in

                        guard success else { return }

                        self.getScheduleLocalNotification()  // Schedule Local Notification

                    })

                    // Request Authorization

                    break

                case .authorized:

                       self.getScheduleLocalNotification()  // Schedule Local Notification

                    break

                case .denied:

                    print(" Not Allowed to Display Notifications- Application")

                case .provisional:

                    break

                case .ephemeral:

                    break

                @unknown default:

                    break

            }

        }

    }

 

Step 4: // Request For Authorization

 

private func requestAuthorization(completionHandler: @escaping (_ success: Bool) -> ()) {

        // Request Authorization

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (success, error) in

            if let error = error {

                print("Request Authorization Failed (\(error), \(error.localizedDescription))")

            }

            completionHandler(success)

        }

    }

 

Step 5:

 

    private func scheduleLocalNotification() {

        // Create Notification Content

        let notificationContent = UNMutableNotificationContent()

 

        // Configure Notification Content

        notificationContent.title = “iOS Developer live"

        notificationContent.subtitle = "Local Notifications"

        notificationContent.body = "this tutorial, local notifications with the User Notifications framework. & how to schedule "

 

        // Add Time Interval Trigger

        let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

 

        // Create Request for Notification

        let notificationRequest = UNNotificationRequest(identifier: "cocoacasts_local_notification", content: notificationContent, trigger: notificationTrigger)

 

        // Add Request to User Notification Center

        UNUserNotificationCenter.current().add(notificationRequest) { (error) in

            if let error = error {

                print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")

            }

        }

    }

 

Step 6:

 

extension UIViewController: UNUserNotificationCenterDelegate {

 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler([.alert])

    }

 

}


Comments

Post a Comment