如何使用swift 3中的UIActivityViewController将图像和pdf网址分享到社交媒体

时间:2018-01-15 06:08:53

标签: swift3 uiactivityviewcontroller

我想使用UIActivityController分享图片和pdf网址,但无法分享。在我的情况下,从服务器获取此URL,此URL是图像或pdf文件。

示例网址:

https:/storage.googleapis.com/records-records/e52723db2c898c012a5a99354597cc8d_56.pdf -- file:///

1 个答案:

答案 0 :(得分:-1)

1)必须首先将PDF保存在文件系统中。例如,这可以在加载UIWebView后完成:

class yourViewController: UIViewController{
       var documentPathToLoad = "http://myPdfFile.pdf"

       func webViewDidFinishLoad(_ webView: UIWebView) { 
           if yourWebView.isLoading { 
               // still loading 
               return 
           } 

           //Save the pdf document to file system 
           savePdf() 
       }

       func savePdf(){    
           let fileManager = FileManager.default 
           let paths = (NSSearchPathForDirectoriesInDomain((.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("documento.pdf") 
           let pdfDoc = NSData(contentsOf:URL(string: documentPathToLoad)!) 
           fileManager.createFile(atPath: paths as String, contents: pdfDoc as Data?, attributes: nil) 
       } 

2)读取保存的文件,然后共享。这种方法适用于Swift。

func loadPDFAndShare() { 
           let fileManager = FileManager.default 
           let documentoPath = (self.getDirectoryPath() as NSString).appendingPathComponent("documento.pdf") 

           if fileManager.fileExists(atPath: documentoPath){              
                let documento = NSData(contentsOfFile: documentoPath) 
                let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documento!], applicationActivities: nil) 
                activityViewController.popoverPresentationController?.sourceView=self.view 
                present(activityViewController, animated: true, completion: nil) 
            } 
            else { 
                print("document was not found") 
            } 
        }