Allow Download on IOS App

Asked

Viewed 135 times

0

I started developing for IOS and I can’t download inside a Webview using Xcode in the Swift language.

The Code is very simple, it opens a website, where it contains a button to download an Image file.

But after clicking the button, it opens the image on a white page. If I press the image, it appears the save option, but when I click the Emulator closes and an Error message appears (Thread 9: signal SIGABRT).

I’ve turned the Internet upside down and looked at other answers right here on Stack Overflow, but nothing worked for me.

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {


    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"https://erroemdownloadios.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)


    } 
}
  • 4

    Why is it urgent?

  • Hello, Anderson. I am a Web developer, and a client of mine gave me an ultimatum, to develop the application on IOS, otherwise, cancel the system.

  • 2

    When he hired you he said he needed to do it for iOS?

  • No, but eventually this need arose and I need to develop this application, because I have another client who is also considering this hypothesis. I already developed three apps for Android, but I came across this obstacle, entering an environment that I’m not used to. You could help me Hugo ?

  • 1

    @David You must be receiving SIGABIRT from the emulator because your app needs NSPhotoLibraryAddUsageDescription in your Info.plist. When the save action on mobile is activated from within your app, OS asks the user for permission to save the image through their app. Try putting this key and see if the crash goes away.

  • Hello Igor, thanks for the reply. I added in Info.plist a Key with name: Nsphotolibraryaddusagedescription with String type. Unfortunately the error continues :(

  • Try "Privacy - Photo Library Additions Usage Description" and "Privacy - Photo Library Usage Description"

  • Thanks for the reply George, He stopped closing the Emulator, but I went to check the folders and he did not save the image. You could help me with that ?

  • Yes, but you have to check which method you are using to save this image, maybe it is better to create another question with more detail.

Show 4 more comments

1 answer

1

I would recommend you to have your Viewcontroller implement WKNavigationDelegate. Nessa delegate, you can use the method:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)

In the navigationAction, you can pick up the URL to which the browser will navigate. If it is an image (you can know this by pathExtension or some other regex in the URL), you can use to download the image via code (with URLSession or Data), and save in user photos.

EDIT (13/09/2019):

You can intercept URL changes this way:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {


    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.naivgationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"https://erroemdownloadios.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)


    } 

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        guard let requestURL = navigationAction.request.url else {
            decisionHandler(.allow)
            return
        }

        //Aqui você tem a URL, e pode fazer o que quiser com ela.

        decisionHandler(.allow)
    }
}
  • Good morning Igor, thank you for your reply. class Viewcontroller: Uiviewcontroller, Wkuidelegate,Wknavigationdelegate{ var webView: Webwkview! 
 
 func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void){

 }

 Você poderia me dizer se está correto?

  • @David I updated my response to include how to use actions.

  • Thanks for the help Igor, I performed the test and unfortunately the error still persists. it Returns: Thread 10: Signal SIGABRT and Closes the application. :(

Browser other questions tagged

You are not signed in. Login or sign up in order to post.