Reputation: 3092
EDIT: My question was not very clear now I edited it to make it clear that I need to open an online web page and not the help book.
I would like to include a question mark button in a NSAlert in a macOS project that points to an online web page with the help resource.
I saw here that there are two possibilities:
var showsHelp: Bool Specifies whether the alert has a help button.
var helpAnchor: String? The alert’s HTML help anchor.
But I can't figure out how to implement it.
I use this code:
@IBAction func buttonPressed(_ sender: Any) {
let myAlert: NSAlert = NSAlert()
myAlert.messageText = "Message"
myAlert.informativeText = "Informative text."
myAlert.showsSuppressionButton = true
myAlert.addButton(withTitle: "Later")
myAlert.addButton(withTitle: "Now")
myAlert.addButton(withTitle: "OK")
let choice = myAlert.runModal()
switch choice {
case NSAlertFirstButtonReturn:
print ("OK")
case NSAlertSecondButtonReturn:
print ("Now")
case NSAlertThirdButtonReturn:
print ("Later")
default: break
}
if myAlert.suppressionButton!.state == 1 {
print ("Checked")
} else {
print ("Not checked")
}
}
Upvotes: 6
Views: 712
Reputation: 90671
You should make your controller class conform to NSAlertDelegate
.
Then, set myAlert.delegate = self
and myAlert.showsHelp = true
.
In your controller class, implement func alertShowHelp(_ alert: NSAlert) -> Bool
to do whatever you like.
In general, to open URLs in the user's default browser, use NSWorkspace
and its open()
method.
Upvotes: 4
Reputation: 3702
Use the helpAnchor
property to set the help anchor, which can redirect to a section in the documentation or a website.
var helpAnchor The alert’s HTML help anchor. https://developer.apple.com/reference/appkit/nsalert/1534314-helpanchor
Example:
var alert = NSAlert()
alert.messageText = "Testing"
alert.helpAnchor = "https://google.com"
alert.showsHelp = true
alert.runModal()
That won't work because the URL is not set as a HelpBook, to set that see this:
For opening external websites:
Please note that the built-in question mark button only supports opening the built in Help window, not internet browsers. If you want to open in a web browser, you have to create a whole custom NSAlert
and provide your own accessoryView
:
Upvotes: 0