Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect UIWebview drop in iOS 11?

I have a UIWebView. I am trying to implement ios11 drag and drop feature. When I drag item from Safari and drop it in my UIWebView, I want to know which delegate method will get called. Any suggestions?

like image 251
Sheik_101 Avatar asked Aug 03 '17 12:08

Sheik_101


People also ask

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

Does iOS WebView support WebRTC?

A simple working iOS RTCDataChannel built using WKWebView. Rather than include the external native WebRTC iOS framework at https://webrtc.org/native-code/ios, this library leverages WebKit's inbuilt WebRTC functionality and exposes WebRTC functionality through the WKWebView control.

Is UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0.

Is WKWebView same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.


2 Answers

Most of the functionality of UIWebview is implemented in an undocumented class called UIWebBrowserView. It is that view which has its 'interactions' property set and the delegate for those interactions is set to that same UIWebBrowserView.

Depending on your needs, there are 3 options to detect the drop:

1) You could use javascript in the content of the web view to detect the drop. This would looks something like:

document.addEventListener('drop', function() {
     // Drop detected
} ) ;

2) You could disable the drop interactions in the UIWebBrowserView. That will allow them to propagate up into one of the containing views, and you can add your own interactions there. That would look like:

webView.scrollView.subviews[0].interactions = @[] ;  

Once you've done that, the you can create your own interactions and add them to the container view:

 UIDropInteraction *dropInteration = [[UIDropInteraction alloc]initWithDelegate:myController] ;
 [webView addInteraction:dropInteration] ;
 webView.userInteractionEnabled = YES ;

You would then implement the UIDropInteractionDelegate functionality in 'myController'

3) You could swizzle UIWebBrowserView and replace the UIDropInteractionDelegate methods with your own.

like image 173
Rich Waters Avatar answered Oct 05 '22 03:10

Rich Waters


UIDragInteractionDelegate & UIDropInteractionDelegate protocols are what you're looking for.

Check this out as well: How to Drop Images in UIViewController with UIDropInteraction (Ep 1)

like image 42
dispatchswift Avatar answered Oct 05 '22 03:10

dispatchswift