0

I have written the following function and what I am trying to do is to notify the user by showing an alert.

func handleRecievedQR (QRCode : String) {
    let scannedCode = QRCode.split(separator: ",")
    let attendance = ScannedQRCode(issuer: String(scannedCode[0]),
                                   eventName:String(scannedCode[1]) ,
                                   eventCode: String(scannedCode[2]),
                                   issuedYear: Int(scannedCode[3])!,
                                   issuedMonth: Int(scannedCode[4])!,
                                   issuedDay: Int(scannedCode[5])!,
                                   issuedHour: Int(scannedCode[6])!,
                                   issuedMin: Int(scannedCode[7])!,
                                   lat: Double(scannedCode[8])!,
                                   long: Double(scannedCode[9])!
    )

    if (attendance.validateTime() ){

        if (attendance.validateLocatoin() ){
            eventName.text = attendance.getEventName()
            eventCode.text = attendance.getEventCode()
            fullName.placeholder = "Enter your full name here"
            SignBtn.titleLabel?.text = "Sign the attendece"


        }else {
            //The problem is here. For debuggin I have used a print statement to make sure that the program has reached that point
            print("You are not present here ")
            let alert = UIAlertController(title: "Message", message: "It's detected that you are not actually in the event", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Note", style: .default, handler: { action in
                switch action.style{
                case .default:
                    print("HI")
             case .cancel:
                    return
                case .destructive:
                    return
                }}))


        present(alert, animated: true, completion: nil)

        }
    }else{
        print("Too late to scan the code")

    }

}

this function is called in the viewDidLoad but I cant manage to show any AlertView. Any idea where am I making my mistake and how can I fix it ?

Danial Kosarifa
  • 1,044
  • 4
  • 18
  • 51

2 Answers2

2

It will not show up in viewDidLoad(). You call the function from viewDidAppear()

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
2

You never present the alert view. You should move the code that displays your alert to viewDidAppear(), as mentioned by Martin, and then you need to add a line self.present(alert, animated: true, completion: nil)

Duncan C
  • 128,072
  • 22
  • 173
  • 272