前提・実現したいこと
前回の続きですが、URLSessionにてファイルダウンロードで
出来たファイルが.tmpで開けない。。。
該当のソースコード
ソースコード import UIKit class ViewController: UIViewController, URLSessionDownloadDelegate { var progressBar: UIProgressView! @objc override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type: .system) button.setTitle("ダウンロード開始", for: .normal) button.titleLabel?.font = UIFont(name: "Arial", size: 24) button.addTarget(self, action: #selector(self.startDownloadTask), for: .touchUpInside) button.sizeToFit() button.center = self.view.center self.view.addSubview(button) //button.addTarget(self, action: #selector(self.startDownloadTask), for: .touchUpInside) progressBar = UIProgressView(progressViewStyle: .default) progressBar.layer.position = CGPoint(x: self.view.center.x, y: self.view.frame.height / 4) self.view.addSubview(progressBar) } @objc func startDownloadTask() { let sessionConfig = URLSessionConfiguration.background(withIdentifier: "myapp-background") let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil) let url = URL(string: "任意のダウンロード先URL")! let downloadTask = session.downloadTask(with: url) downloadTask.resume() } func getIdFromDateTime() -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd-HH-mm-ss" return dateFormatter.string(from: Date()) } func getSaveDirectory() -> String { let fileManager = Foundation.FileManager.default let path = NSSearchPathForDirectoriesInDomains(Foundation.FileManager.SearchPathDirectory.libraryDirectory, Foundation.FileManager.SearchPathDomainMask.userDomainMask, true)[0] + "/DownloadFiles/" if !fileManager.fileExists(atPath: path) { createDir(path: path) } return path } func createDir(path: String) { do { let fileManager = Foundation.FileManager.default try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError { print("createDir: (error)") } } func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { print("didFinishDownloading") do { if let data = NSData(contentsOf: location) { let fileExtension = location.pathExtension let filePath = getSaveDirectory() + getIdFromDateTime() + "." + fileExtension print(filePath) try data.write(toFile: filePath, options: .atomic) } } catch let error as NSError { print("download error: (error)") } } func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite) print(String(format: "%.2f", progress * 100) + "%") DispatchQueue.main.async(execute: { self.progressBar.setProgress(progress, animated: true) }) } func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { if error != nil { print("download error") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
試したこと
拡張子の.tmpを.mp4に変えてみたり付け加えてみたり。。。(ああ、わかる人から見れば笑い者だよな)
.tmpは一時ファイルであることはサイトのあちこちに書いてありましたが、ここ2.3日悩んでます。。昔のバージョンはmp4が出来ていたのかな?聞いてばかりで申し訳ないですが、どうすればダウンロードしたファイルがみれますか?教えてください。。。
補足情報(FW/ツールのバージョンなど)
参考としたサイト
https://joyplot.com/documents/2016/09/30/swift-urlsession-progressbar/
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。