Search code examples
iosswiftswift3

How do I connect my login screen with my home screen after I login?


I have a login screen and wanted to connect it with my home page after I log in. I created a Segue with Identifier but It doesn't work when I try it. What can I do?

let message = json!["message"] as? String

if (message?.contains("success"))!
{                                                   
    self.performSegue(withIdentifier: "homelogin", sender: self)

    let id = json!["id"] as? String
    let name = json!["name"] as? String
    let username = json!["username"] as? String
    let email = json!["email"] as? String


    print(String("Emri") + name! )
    print(String("Emaili") + email! )
    print(String("Id") + id! )
    print(String("username") + username! )                                                      
}
else
{
    print(String("check your password or email"))
}

this is the segue with Identifier


Solution

  • There are lots of issues with your code. Try it and let me know if it works. I have also added explanation for the changes I done.

    if let json = json { // The “if let” allows us to unwrap optional values safely only when there is a value, and if not, the code block will not run and jump to else statement
    
        let message = json["message"] as? String ?? "" // The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. 
    
        if message == "success" {
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "homelogin", sender: self)
            }
    
            let id = json["id"] as? String ?? "" // String can directly be entered in double quotes. no need to use String()
    
            let name = json["name"] as? String ?? ""
            let username = json["username"] as? String ?? ""
            let email = json["email"] as? String ?? ""
    
    
    
            print("Emri " + name )
            print("Emaili " + email )
            print("Id " + id )
            print("username " + username )
    
    
        }else{
            print("check your password or email")
        }
    } else {
        print("Invalid json")
    }