2017-07-14 21 views
3

我確實有一個UIWebView包含在加載公共URL的地方;不幸的是,vcard和ical-Links沒有被處理,即當我點擊它們時沒有任何反應。UIWebView:ics和vcard-Links無法處理

我試圖設置所有的數據檢測器,不幸的是沒有運氣。

2017-07-14 13:43:00.982413+0200 xxx[2208:967973] WF: _userSettingsForUser mobile: { 
    filterBlacklist =  (
    ); 
    filterWhitelist =  (
    ); 
    restrictWeb = 1; 
    useContentFilter = 0; 
    useContentFilterOverrides = 0; 
    whitelistEnabled = 0; 
} 

在Safari中,同樣的東西按預期工作:

在Xcode的日誌,我這樣的鏈接,當點擊得到這個位置。

如果我使用Safari瀏覽器UIApplication.shared.openURL(icsOrVcardUrl)得到打開,並從那裏一切正常再次預期,但我不希望用戶離開應用程序...

編輯 這不工作,要麼:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { 
    if let url = request.url { 
     if url.absoluteString.contains("=vcard&") || url.absoluteString.contains("/ical/") { 
      let sessionConfig = URLSessionConfiguration.default 
      let session = URLSession(configuration: sessionConfig) 
      let request = URLRequest(url:url) 
      let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in 
       if let tempLocalUrl = tempLocalUrl, error == nil { 
        DispatchQueue.main.async { 
         self.documentController.url = tempLocalUrl 
         self.documentController.presentPreview(animated: true) 
        } 
       } 
      } 
      task.resume() 
      return false 
     } 
    } 
    return true 
} 
+0

你有沒有嘗試在這裏接受的答案https://stackoverflow.com/a/4442594/2141666讓操作系統處理某些類型的任何鏈接?這個答案讓操作系統嘗試處理任何不是http/https的東西;也許你可以改變它來試圖讓操作系統處理任何包含.vcard或.ics的URL。 – Kdawg

+0

但這些鏈接是啓動下載的https鏈接... – swalkner

+0

由於保存文件的擴展名,編輯中的代碼將不起作用。有關更多信息,請參閱我的答案的編輯。 –

回答

2

使用UIDocumentInteractionController預覽而無需離開您的應用程序。 我用.ics文件快速測試它,它工作正常。

落實UIDocumentInteractionControllerDelegate協議

extension MainViewController: UIDocumentInteractionControllerDelegate { 
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { 
     return self; 
    } 
} 

創建交互控制器的實例:

let documentController = UIDocumentInteractionController() 

截取你的UIWebView的點擊次數在shouldStartLoadWithRequest,返回的鏈接錯誤要與在處理-app預覽,其餘的都是true。最後:

func previewDocument(_ url: URL) { 
    documentController.url = url 
    documentController.presentPreview(animated: true) 
} 

這是在模擬器

enter image description here

編輯:

在回答這個答案的評論: 的原因,它不工作因爲你是因爲UIDocumentInteractionController取決於文件擴展名。臨時文件的擴展名爲.tmp

下載後重命名文件可解決問題。快速和骯髒的例子:

let task = session.downloadTask(with: url!) { (tempLocalUrl, response, error) in 
    if let tempLocalUrl = tempLocalUrl, error == nil { 
     do { 
      let filemgr = FileManager.default 
      let newUrl = tempLocalUrl.appendingPathExtension("ics") 
      try filemgr.moveItem(at: tempLocalUrl, to: newUrl) 
      DispatchQueue.main.async { 
       self.documentController.url = newUrl 
       self.documentController.presentPreview(animated: true) 
      } 
     } catch let error { 
      print("Error!!!: \(error.localizedDescription)") 
     } 

    } 
} 
task.resume() 

在這種情況下,建議後自己清理,因爲任務完成後,雖然OS最終將其刪除,需要空間時,該文件將不會被刪除。如果你經常訪問相同的URL,Library/Caches/可能是這個文件的更好的地方,只是想出好的命名架構,並檢查文件是否已經存在。

+0

請看我的評論,這對我沒有用(我有'https'鏈接不是本地的,因此我首先下載) – swalkner