1

I get a PDF in Json, retrieve it and have it displayed in a webview, but I also need it to be shared with the UIActivityViewController, but when I pass the webview it does not show options available for sharing

I am doing the following:

 //I get the json
    if let url = URL(string: "http://10.1.80.106:9000/api/recibo/imprimir?vigencia=2015-06-30") {
        var urlRequest = URLRequest(url: url)
        urlRequest.setValue("application/pdf", forHTTPHeaderField: "Content-Type")
        urlRequest.setValue("aaaabbbccc", forHTTPHeaderField: "Authorization")

        //i create the webview
        let webView = UIWebView(frame: self.view.frame)
        webView.loadRequest(urlRequest as URLRequest)
        webView.scalesPageToFit = true
        let pdfVC = UIViewController()
        pdfVC.view.addSubview(webView)
        pdfVC.title = "Recibo de Pagamento"
        self.navigationController?.pushViewController(pdfVC, animated: true)


        //instantiate the UIActivityViewController for the share, and step the WebView as parameter
        let vc = UIActivityViewController(activityItems: [(webView.request?.url?.absoluteString) as Any], applicationActivities: [])

        if let popoverController = vc.popoverPresentationController {
            popoverController.sourceView = self.view
            popoverController.sourceRect = self.view.bounds
        }
        self.present(vc, animated: true, completion: nil)

    }

1 Answers1

0

Actually, instead of WebView you could also use the QuickLook Framework, which is designed for this specific purpose. You just pass the location of the file and conform to the protocols. It will present a view controller with the document inside. You can also have the native options to share the file.

Check out this answer about it's implementation and example.

Umar Farooque
  • 2,049
  • 21
  • 32