Xcode code completion suggests `printContent(_)` above `print(_)`

Viewed 218

Using Xcode 13, typing any substring of print suggests printContent() first in the Xcode code completion list above the common Swift print() function(s).

printContent first in list

printContent(_ sender: Any?)
Tells your app to print available content.

"Jump to Definition" displays the following declaration, new for iOS 15 in 2021:

public protocol UIResponderStandardEditActions : NSObjectProtocol {
    
    // ...
    
    @available(iOS 15.0, *)
    optional func printContent(_ sender: Any?)
}

What is the printContent() function and how is it used?

Is it in any way a new better replacement for print(), justifying its prominent code completion location in Xcode?

If not, how can I go back to pre-Xcode 13 behavior and suggest the extremely common print() function first in the list?

1 Answers

If your app includes the UIApplicationSupportsPrintCommand key in its Info.plist file, people can print from your app using the keyboard shortcut Command-P, which calls printContent(_:). You can also set printContent(_:) as the action on other print-related controls such as a print button on a toolbar.

override func printContent(_ sender: Any?) {
    let info = UIPrintInfo.printInfo()
    info.outputType = .photo
    info.orientation = .portrait
    info.jobName = modelItem.title
    
    let printInteractionController = UIPrintInteractionController()
    printInteractionController.printInfo = info
    printInteractionController.printingItem = modelItem.image
    
    let completionHandler: UIPrintInteractionController.CompletionHandler = {
        (controller: UIPrintInteractionController, completed: Bool, error: Error?) in
        if let error = error {
            Logger().error("Print failed due to an error: \(error.localizedDescription)")
        }
    }
    
    if traitCollection.userInterfaceIdiom == .pad {
        if let printButton = navigationItem.rightBarButtonItem {
            printInteractionController.present(from: printButton, animated: true, completionHandler: completionHandler)
        }
    } else {
        printInteractionController.present(animated: true, completionHandler: completionHandler)
    }
}

Apple developer link

Related