Search code examples
iosswiftswiftuiwkwebview

error build: Value of type 'UIDragInteraction' has no member 'previewForLifting'


I'm trying to learn to put my web dev skills to use in iOS. Not understanding how to add members to UIDragInteraction.

Seeing this error:

error build: Value of type 'UIDragInteraction' has no member 'previewForLifting'

My code:

import SwiftUI
import WebKit

class DragDelegate: NSObject, UIDragInteractionDelegate {
    func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
        let webView = UIApplication.shared.windows.first?.rootViewController?.view.subviews.first(where: { $0 is WKWebView }) as? WKWebView
        let provider = NSItemProvider(object: webView?.url?.absoluteString as NSString? ?? "")
        let item = UIDragItem(itemProvider: provider)
        return [item]
    }
}

struct BPMNView: UIViewRepresentable {
    let urlString = "https://cdn.staticaly.com/gh/bpmn-io/bpmn-js-examples/dfceec6a/starter/diagram.bpmn"

    func makeUIView(context: Context) -> WKWebView {
        let webView = WKWebView()
        webView.load(URLRequest(url: URL(string: urlString)!))
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.load(URLRequest(url: URL(string: urlString)!))
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("BPMN Diagram")
                .font(.largeTitle)
                .padding()
            BPMNView()
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                .onAppear {
                    if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                        let view = windowScene.windows.first?.rootViewController?.view {
                        let preview = UITargetedPreview(view: view)
                        let interaction = UIDragInteraction(delegate: DragDelegate())
                        view.addInteraction(interaction)
                        interaction.isEnabled = true
                        interaction.previewForLifting = preview

                    }
                }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I'm so new to this I'm not understanding the logic. I also can't get the screen to show the content when I take out interaction.previewForLifting = preview:

enter image description here


Solution

  • „has no member 'previewForLifting'“ is compiler speak for telling the programmer that this method or property does not exist in the class.

    Have a look at:

    https://developer.apple.com/documentation/uikit/uidraginteractiondelegate/2891057-draginteraction/

    UIDragInteraction on its own has very little public interface, which is covered completely here:

    https://developer.apple.com/documentation/uikit/uidraginteraction/

    Apple's documentation often lacks explanation, but technically it is usually pretty complete.

    As was already pointed out, this is a delegate method, not an instance property. The role of the delegate in drag & drop handling is rather critical to understand, as it is used for a lot of interaction handling inside iOS.