Instance Method

validateContextMenuItem(withCommand:in:userInfo:validationHandler:)

Validates whether a particular contextual menu item should be displayed.

Declaration

optional func validateContextMenuItem(withCommand command: String, in page: SFSafariPage, userInfo: [String : Any]? = nil, validationHandler: @escaping (Bool, String?) -> Void)

Parameters

command

The command, specified in the Info.plist file, for the context menu item being validated.

page

The page where the context menu item is going to be presented.

userInfo

Optional message content. If specified, the dictionary's value objects conform to the W3C standard for safe passing of structured data, such as Boolean objects, numeric values, strings, and arrays.

validationHandler

A code block used to set the state of the contextual menu item. The block receives the following parameters:

shouldHide

A Boolean value that indicates whether the menu item should be hidden.

text

The text to use for the menu item. Pass nil to use the default text from the Info.plist file.

Discussion

If you do not implement this method, the contextual menu item is always available. Otherwise, this method is called before the contextual menu is shown so that you can determine whether the menu item should be displayed and the text that appears in the item.

To hide the contextual menu item:

Listing 1

Hide the menu item.

validationHandler(true, nil)

To change the menu text:

Listing 2

Change the menu text.

validationHandler(false, “Updated text”)

To use the default text:

Listing 3

Use the default text.

validationHandler(false, nil)

See Also

Responding to Context Menu Selections

func contextMenuItemSelected(withCommand: String, in: SFSafariPage, userInfo: [String : Any]?)

Called when one of the app extension's context menu items is selected.