UIDocumentInteractionController 不起作用
UIDocumentInteractionController does not work
在我的应用程序中,我下载了各种格式的文件。我试图让 UIDocumentInteractionController 获得可以打开这种格式的应用程序。问题是 UIDocumentInteractionController 不工作。这是我的代码:
func loadArchiveAtIndex(sender: NSNotification){
let itemIndex = sender.userInfo!["index"] as! Int
let archiveKey = sender.userInfo!["downloadKey"] as! String
self.downloadingItens.append(itemIndex)
print(archiveKey)
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
SQLiteDataController().getTokenSincronismo({
(Valido, Retorno, Token, Tipo) -> Void in
if Valido == true{
switch Retorno {
case 9: // Retorno Válido
print(Token)
let params = [
"tokenSincronizacao":"\(Token)",
"chaveProdutoBiblioteca":"\(archiveKey)"
]
Alamofire.request(.GET, "\(_URLPREFIX)/ObtemProdutoBiblioteca", parameters: params).responseJSON { (response) in
if response.result.isFailure {
print(response.result.error)
} else {
let result = response.result.value
if let archive = result?["ProdutoBiblioteca"] as? NSDictionary{
let bytes = archive.objectForKey("Arquivo") as! String
let name = archive.objectForKey("NomeArquivo") as! String
let data = NSData.init(base64EncodedString: bytes, options: .IgnoreUnknownCharacters)
let url = NSURL.init(string: name.lowercaseString)
data?.writeToURL(url!, atomically: true)
let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
let fileURL = documents.URLByAppendingPathComponent((name.lowercaseString))
print(fileURL)
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
//
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var arrayIndex = 0
for item in self.downloadingItens {
let i = item
if i == itemIndex {
self.downloadingItens.removeAtIndex(arrayIndex)
NSNotificationCenter.defaultCenter().postNotificationName("reloadTable", object: nil, userInfo: ["itens":self.downloadingItens])
break
}
arrayIndex = arrayIndex + 1
}
})
}
}
}
break
default:
self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação")
break
}
}else{
self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação")
}
}, sincFull:true)
})
}
func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) -> UIView? {
return self.view
}
我做错了什么?
您需要从主线程呈现视图。
而不是:
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
尝试:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
})
在我的应用程序中,我下载了各种格式的文件。我试图让 UIDocumentInteractionController 获得可以打开这种格式的应用程序。问题是 UIDocumentInteractionController 不工作。这是我的代码:
func loadArchiveAtIndex(sender: NSNotification){
let itemIndex = sender.userInfo!["index"] as! Int
let archiveKey = sender.userInfo!["downloadKey"] as! String
self.downloadingItens.append(itemIndex)
print(archiveKey)
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
SQLiteDataController().getTokenSincronismo({
(Valido, Retorno, Token, Tipo) -> Void in
if Valido == true{
switch Retorno {
case 9: // Retorno Válido
print(Token)
let params = [
"tokenSincronizacao":"\(Token)",
"chaveProdutoBiblioteca":"\(archiveKey)"
]
Alamofire.request(.GET, "\(_URLPREFIX)/ObtemProdutoBiblioteca", parameters: params).responseJSON { (response) in
if response.result.isFailure {
print(response.result.error)
} else {
let result = response.result.value
if let archive = result?["ProdutoBiblioteca"] as? NSDictionary{
let bytes = archive.objectForKey("Arquivo") as! String
let name = archive.objectForKey("NomeArquivo") as! String
let data = NSData.init(base64EncodedString: bytes, options: .IgnoreUnknownCharacters)
let url = NSURL.init(string: name.lowercaseString)
data?.writeToURL(url!, atomically: true)
let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
let fileURL = documents.URLByAppendingPathComponent((name.lowercaseString))
print(fileURL)
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
//
dispatch_async(dispatch_get_main_queue(), { () -> Void in
var arrayIndex = 0
for item in self.downloadingItens {
let i = item
if i == itemIndex {
self.downloadingItens.removeAtIndex(arrayIndex)
NSNotificationCenter.defaultCenter().postNotificationName("reloadTable", object: nil, userInfo: ["itens":self.downloadingItens])
break
}
arrayIndex = arrayIndex + 1
}
})
}
}
}
break
default:
self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação")
break
}
}else{
self.showError("Atenção", message: "Não foi Possivel Processar a sua Solicitação")
}
}, sincFull:true)
})
}
func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) -> UIView? {
return self.view
}
我做错了什么?
您需要从主线程呈现视图。
而不是:
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
尝试:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let dic = UIDocumentInteractionController.init(URL: fileURL!)
dic.delegate = self
dic.presentPreviewAnimated(true)
})