I want to download and install a "*.mobileconfig" file, downloaded from webserver. Also, I want to present user a webpage during this process. This page is explaining to user what to do next.
My initial approach was to subclass WKWebView, and use it like this:
webView.load(pageUrlRequest)
webView.startDownload(using: downloadUrlRequest) { ... }
To achive that, I have implemented WKNavigationDelegate
and WKDownloadDelegate
. Example code is down below.
File downloads perfectly, but the problem is - it is not imported into the system, i.e. in Settings there is no item "Profile downloaded". iOS knows nothing about it, and it seems there is no way to install ".mobileconfig" downloaded that way.
When I opening the same mobileconfig URL with SFSafariViewController
- it is recognising and importing this file well.
My question is - how can I download "mobileconfig" with WKWebView? Is there some specific directory where I need to place this file once it is downloaded, or some system API to trigger iOS recognising it?
class MyWKWebView: WKWebView {
private var downloadedFileUrl: URL?
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
super.init(frame: frame, configuration: configuration)
super.navigationDelegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension MyWKWebView: WKNavigationDelegate {
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
) {
if navigationAction.request.url?.absoluteString.hasSuffix("mobileconfig") == true {
decisionHandler(.download)
return
}
if navigationAction.shouldPerformDownload {
decisionHandler(.download)
} else {
decisionHandler(.allow)
}
}
func webView(
_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void
) {
if navigationResponse.canShowMIMEType {
decisionHandler(.allow)
} else {
decisionHandler(.download)
}
}
func webView(_ webView: WKWebView, navigationAction: WKNavigationAction, didBecome download: WKDownload) {
download.delegate = self
}
}
// MARK: Download
extension MyWKWebView: WKDownloadDelegate {
func download(
_ download: WKDownload,
decideDestinationUsing response: URLResponse,
suggestedFilename: String,
completionHandler: @escaping (URL?) -> Void
) {
downloadedFileUrl = FileManager.default.tmpPathForMobileConfig(suggestedFilename)
completionHandler(downloadedFileUrl)
}
func downloadDidFinish(_ download: WKDownload) {
print("••• Download is finished")
}
func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {
print("••• Download is failed: \(error.localizedDescription)")
}
}
fileprivate extension FileManager {
func tmpPathForMobileConfig(_ fileName: String) -> URL {
let tempDirectory = self.urls(for: .documentDirectory, in: .userDomainMask).last!
let tempFolderName = UUID().uuidString
let tempDirectoryPath = tempDirectory.appendingPathComponent(tempFolderName)
do {
try self.createDirectory(at: tempDirectoryPath, withIntermediateDirectories: false)
} catch {
debugPrint(error)
}
return tempDirectoryPath.appendingPathComponent(fileName)
}
}