Learn Courses My Dashboard

Printing the content of a text field in my view to a printer

Hello, I would like to know if there is a way to physically print the content of a text field or text editor on my printer. Is there a command that will do this?

Thanks, Todd

Hi Todd, considering all printers are different, there’s not going to be some built in command.

I’d suggest looking at your printer and see if it has an API, or is Bluetooth.

You may be able to use the “share sheet” and create some kind of PDF and send that to your printer

You should look into UIPrintInteractionController.

Thanks. I found UIActivityViewController when I googled this question. Do you think this could be used? I’m surprised I can’t find much on printing. It seems like more developers would want to send data to a printer.

Todd

UIActivityController can indeed be used, but it will also present the user with more options than just printing. (Though you can disable some of them if they aren’t appropriate for the circumstances.) If that’s okay with you, then use it. If you just want printing, use UIPrintInteractionController.

Thanks

So I decided to use the UIActivityController and I inserted an example of code. It works except it seems like all the default apps are excluded. When I jump to the definition of activity items I get the extension listed at the bottom. I can’t make any changes in the extension. Is there an easy fix for this? I just want the UIActivityController without any excluded activity types. Please help. Todd

import SwiftUI


    struct ShareSheet: UIViewControllerRepresentable {
            typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
            
            let activityItems: [Any]
            let applicationActivities: [UIActivity]? = nil
            let excludedActivityTypes: [UIActivity.ActivityType]? = nil
            let callback: Callback? = nil
            
            func makeUIViewController(context: Context) -> UIActivityViewController {
            
                let controller = UIActivityViewController(
                    activityItems: activityItems,
                    applicationActivities: applicationActivities)
                controller.excludedActivityTypes = excludedActivityTypes
                controller.completionWithItemsHandler = callback
                return controller
            }
            
            func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
                // nothing to do here
            }
        }
    

struct ContentViewA: View {
    @State private var showShareSheet = false
    
    var body: some View {
        VStack(spacing: 20) {
            Text("Hello World")
            Button(action: {
                self.showShareSheet = true
            }) {
                Text("Share Me").bold()
            }
        }
        .sheet(isPresented: $showShareSheet) {
            ShareSheet(activityItems: ["Todd's Text"])
        }
    }
}
struct ContentViewA_Previews: PreviewProvider {
    static var previews: some View {
        ContentViewA()
    }
}