Ios 如何在chrome或safari中打开pdf文件而无需在swift中下载?
当用户点击pdfButton时,如何在chrome或safari上显示pdf文件。我不熟悉iOS和文档显示概念。下面是我从服务器获取的数据Ios 如何在chrome或safari中打开pdf文件而无需在swift中下载?,ios,swift,pdf,document,Ios,Swift,Pdf,Document,当用户点击pdfButton时,如何在chrome或safari上显示pdf文件。我不熟悉iOS和文档显示概念。下面是我从服务器获取的数据 { "status" : 200, "body" : "<div>Hi Attachment,<\/div><div><br><\/div><div>Attachment Test<br><\/div>", "attachment" : "http:\
{
"status" : 200,
"body" : "<div>Hi Attachment,<\/div><div><br><\/div><div>Attachment Test<br><\/div>",
"attachment" : "http:\/\/192.168.1.46:3000\/system\/images\/178\/mca%282016-2019%29transfer_certificates_original.pdf?1528805255",
"sender" : "Dr.P.Shanthi(employee@inmeghcms.in)",
"message" : "Details fetched successfully",
"time" : "12 Jun 2018",
"profile" : "http:\/\/192.168.1.46:3000\/assets\/female.png",
"subject" : "TEST"
}
{
“地位”:200,
“正文”:“Hi附件,
附件测试
”,
“附件”:“http:\/\/192.168.1.46:3000\/system\/images\/178\/mca%282016-2019%29转让证书原件.pdf?152880525”,
“发件人”:“P.Shanthi博士(employee@inmeghcms.in)",
“消息”:“已成功获取详细信息”,
“时间”:“2018年6月12日”,
“简介”:“http:\/\/192.168.1.46:3000\/assets\/female.png”,
“受试者”:“测试”
}
您好,请尝试以下代码,我正在使用@Binesh代码,我已更新到swift 4.0
var docController:UIDocumentInteractionController!
let pdfUrl = NSURL(string: "Use your url")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadPDfDoc(pdfUrl: pdfUrl!)
}
@IBAction func pdfViewAction(_ sender: AnyObject) {
docController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}
func downloadPDfDoc(pdfUrl : NSURL) {
let urlTest = self.pdfUrl!.absoluteString
let pdfUrl = NSURL(string: urlTest!)
if(pdfUrl != nil){
let pdfRequest: NSURLRequest = NSURLRequest(url: pdfUrl! as URL)
NSURLConnection.sendAsynchronousRequest(pdfRequest as URLRequest, queue: OperationQueue.main) {(response, data, error) in
let httpResponse = response as? HTTPURLResponse
if(httpResponse?.statusCode == 200 && error == nil){
let documentsUrl = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first! as NSURL
if let fileName = self.pdfUrl!.lastPathComponent {
let destinationUrl = documentsUrl.appendingPathComponent(fileName)
if let data = data {
do {
try data.write(to: destinationUrl!, options: .atomic)
} catch {
print(error)
}
self.docController = UIDocumentInteractionController(url: destinationUrl!)
}
}
}
}
}
}
}
试试这个,如果您遇到任何问题,请告诉我。如果您有pdf文件的url,您可以使用默认的web浏览器打开它
UIApplication.shared.open(url)
您可以使用
UIDocumentInteractionController
或Webkit
直接路径显示@Anbu.karthikya当然,你可以用