我有一个NSURLDownloadTask,可以在后台成功下载文件(大图像或视频文件).我成功复制了网址并调用此功能将我的网址保存到照片库.如您所见,我想发送UILocalNotification以通知用户他们的下载已
我的问题是虽然PHP应用程序处于后台时会调用PHPhotoLibrary.sharedPhotoLibrary().performChanges,但它的完成块不会. (但它要求应用程序重新回到前台)我试着通过评论抓住主线程,看看是否有帮助,但事实并非如此.并且我不想在完成块之前发送本地通知,因为我想告诉用户下载成功/失败的通知.
我想我可以在NSURLDownloadDelegateTask方法中发送通知.这将让用户知道文件已成功下载,但不知道是否将其保存到他们的照片是成功的.我不想告诉我的用户他们的下载成功,然后他们无法在他们的照片库中找到它.
这是我访问和修改照片库的代码.
func saveURLToPhotosLibrary(url: NSURL, fileName: String) { if let fileExtension = url.pathExtension { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let fileUnmanagedIDTag = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil) let fileIDTag = fileUnmanagedIDTag?.takeRetainedValue() if let fileUTType = fileIDTag { if UTTypeConformsTo(fileUTType, kUTTypeImage) { PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url) } else if UTTypeConformsTo(fileUTType, kUTTypeMovie){ PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url) } } else { print("Error getting type of file from download") } }) { (success, error) in //dispatch_async(dispatch_get_main_queue(), { if success { print("finished") self.sendLocalNotification(downloadSuccessful: true, error : nil, fileName: fileName) } else { if let error = error { self.sendLocalNotification(downloadSuccessful: false, error : error, fileName: fileName) } } //}) } } }好吧找到了我的工作解决方案.发现应用程序在没有运行代码后立即暂停,因此应用程序在我的完成处理程序准备好被调用时被暂停.我使用UIApplications共享实例来创建一个新的后台任务.这使我的应用程序有足够的时间来调用完成处理程序.然后我在收到通知后立即结束后台任务.
func saveURLToPhotosLibrary(url: NSURL, fileName: String) { //Returns id to later be passed into method that ends task. let backgroundID : Int = UIApplication.sharedApplication().beginBackgroundTaskWithName("Save to Photo Library Task") { print("Background task expired") } if let fileExtension = url.pathExtension { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let fileUnmanagedIDTag = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, nil) let fileIDTag = fileUnmanagedIDTag?.takeRetainedValue() if let fileUTType = fileIDTag { if UTTypeConformsTo(fileUTType, kUTTypeImage) { PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url) } else if UTTypeConformsTo(fileUTType, kUTTypeMovie){ PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url) } } else { print("Error getting type of file from download") } }) { (success, error) in if success { print("finished") self.sendLocalNotification(downloadSuccessful: true, error : nil, fileName: fileName) } else { if let error = error { self.sendLocalNotification(downloadSuccessful: false, error : error, fileName: fileName) } } //End background task here passing in id of task from earlier. UIApplication.sharedApplication().endBackgroundTask(backgroundID) } } }