Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 585
pkamb Avatar asked Aug 15 '26 02:08

pkamb


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

like image 50
Jagveer Singh Avatar answered Aug 18 '26 08:08

Jagveer Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!