Home > OS >  howto user QLPreviewingController
howto user QLPreviewingController

Time:04-11

In my swift App for macOS, I want to implement a QLPreviewingController. Methods for iOS are very documented, but I can't find examples of previewing for macOS.

I made a NSViewController which has the protocol QLPreviewingController, but I don't understand what I have to do to implement the method preparePreviewOfFile, as I don't understand how to make the controller calling this method.

CodePudding user response:

The QLPreviewingController protocol is for implementing preview extensions that add custom previews to the system.

If you want to show previews from your own AppKit app, you should use QLPreviewPanel or QLPreviewView from the QuickLookUI framework.

https://developer.apple.com/documentation/quicklookui

CodePudding user response:

I solved my problem, according to macOS Swift QuickLook Warning: setDelegate and setDataSource called while the panel has no controller.

first of all, to start opening the file, I had

@objc func previewFile(_ sender: Any) {
     let panel = QLPreviewPanel.shared()
      panel?.dataSource = self
      panel?.delegate = self
      panel?.makeKeyAndOrderFront(nil)
}

The panel dataSource and delegate dont't have to be implemented there, but in the following functions:

override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
    print("beginPreviewPanelController \(panel)")
    panel.dataSource = self
    panel.delegate = self
}

override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
    print("endPreviewPanelController \(panel)")
    panel.dataSource = nil
    panel.delegate = nil
}

after that, I used an URL by calling URL(string: ...). That was the reason of the problem. I had to use URL(fileURLWithPath: ...)

hoping this will help

  • Related