Achieving the above is one of the more easy things to do in Swift. First, we need to create an NSAlert().

let AlertView = NSAlert()

Now, we will make a suitable TextView, or in this case an NSSecureTextView

// NSMakeRect(x, y, width, height) - The location and dimensions of the TextField we need

let InputText = NSSecureTextField(frame: NSMakeRect(0,0,500,20))

Now we will add in all of the other parameters to make our NSAlert. Note that our application logo is automatically added to the left hand side, and buttons are automatically drawn.

Alert.accessoryView = InputText  //Add the SecureTextField to the Alert view as an accessory

Alert.messageText = "Would you like to remotely start your vehicle?" // The bolded text at the top of the alert

Alert.informativeText = "By clicking either of the \"Yes\" options, you are enabling keyless driving in your vehicle for two minutes." // The informative text below the messageText

Alert.addButton(withTitle: "Cancel")

Alert.addButton(withTitle: "Yes, Start and Unlock my vehicle")

Alert.addButton(withTitle: "Yes, Start my Vehicle") //Buttons are in this specific order for a reason!

Alert.layout() // Whenever the accessoryView is something, you must call the layout() function

We will then run the Alert and retrieve whatever the user entered.

let response = Alert.runModal()

if(response == 1001) {

print("Start and Unlock")

let password = InputText.stringValue // Get the string that the user entered

print(password) //Print whatever the user entered in the textView

} else if(response == 1002) {

print("Start") //Corresponding button

 

} else if(response == 1000) {

print("Cancel")

}

Hope this is useful! Feel free to comment below if you have questions, corrections or suggestions.

Spencer