ios browser code example
Example: building an ios file browser
func documentBrowser(_ controller: UIDocumentBrowserViewController,
didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {
os_log("==> Creating A New Document.", log: .default, type: .debug)
let doc = TextDocument()
let url = doc.fileURL
doc.save(to: url, for: .forCreating) { (saveSuccess) in
guard saveSuccess else {
os_log("*** Unable to create a new document. ***", log: .default, type: .error)
importHandler(nil, .none)
return
}
doc.close(completionHandler: { (closeSuccess) in
guard closeSuccess else {
os_log("*** Unable to create a new document. ***", log: .default, type: .error)
importHandler(nil, .none)
return
}
importHandler(url, .move)
})
}
}