2016-08-22 5 views
1

Alamofireでダウンロードした後に.pdfファイルをダウンロードしようとしています。しかし、私は "webview"を使用して見たことがあります。したがって、アプリケーションは大量のメモリを消費し、実行可能ではありません。iOSでファイルを開くには?

私が望むのは、ネイティブデバイスアプリケーションで開くことです。助言がありますか?ありがとうございました。

編集:これは、ダウンロードファイルのための私のコードです:

var localPath: NSURL? 

    Alamofire.download(.GET, url, destination: { (temporaryURL, response) in 
    let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
    let pathComponent = response.suggestedFilename 
    localPath = directoryURL.URLByAppendingPathComponent(pathComponent!) 
    return localPath! 
    }) 
    .response { (request, response, _, error) in 

    if error != nil 
    { 
    // got an error in getting the data, need to handle it 
    print("Error: \(error!)") 

    } 

    //print(response) 
    print("Download file en:\(localPath!)") 

    self.view.hideToastActivity() 
    //self.actioncall() 

    } 

} 

私はlocalPathにから開いているファイルを必要とする...

+0

UIDocumentInteractionControllerのことを聞きましたの?それを読んでみてください。私はあなたが必要なものを信じています:)幸せなコーディング:) –

+0

あなたはすでに何をしているかを示すべきです。 –

+0

を更新してください。 @SandeepBhandariを読んでください – FireUser

答えて

1

あなたはUIDocumentInteractionControllerを使用する必要があります。あなたはそれについてthisアップルのマニュアルページで読むことができます。

いくつかのグーグルを行うことで、実装例の一部が表示されるはずです。たとえばhereの場合、これについてのコードは「mattneub」で確認できます。

私はあなたが使用できる1つの以上のコードてみましょう:これまで

var documentInteractionController: UIDocumentInteractionController! 

@IBAction func openDocument(sender: UIButton) { 
    let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")! 

    if (URL != "") { 
     // Initialize Document Interaction Controller 
     self.documentInteractionController = UIDocumentInteractionController(URL: URL) 

     // Configure Document Interaction Controller 
     self.documentInteractionController.delegate = self 

     // Present Open In Menu 
     self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true) 
      //presentOpenInMenuFromRect 
    } 
} 

// UIDocumentInteractionControllerDelegate 
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { 
    return self 
} 
+0

これはalamofireローカルパスで動作しますか? – FireUser

+1

はい、ローカルパスでも動作します。 –

+0

この行のクラッシュ:URL:NSURL = NSBundle.mainBundle()。URLForResource( "yourPDF"、withExtension: "pdf")! ...エラー:致命的なエラーが発生しました:オプション値をアンラープしている間にnilが予期せず見つかりました...スレッド1 exc_bad_instruction(code = exc_i386_invop subcode = 0x0) – FireUser

関連する問題