objective-c - Swift - 复制完成处理程序

标签 objective-c swift background appdelegate completionhandler

我一直在关注 Appcoda 的教程:http://www.appcoda.com/background-transfer-service-ios7/ 但写得很快。我遇到了这行代码,我无法 swift 开始工作

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

    // Check if all download tasks have been finished.
    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
        if ([downloadTasks count] == 0) {
            if (appDelegate.backgroundTransferCompletionHandler != nil) {
                // Copy locally the completion handler.
                void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;

                // Make nil the backgroundTransferCompletionHandler.
                appDelegate.backgroundTransferCompletionHandler = nil;

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // Call the completion handler to tell the system that there are no other background transfers.
                    completionHandler();

                    // Show a local notification when all downloads are over.
                    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                    localNotification.alertBody = @"All files have been downloaded!";
                    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
                }];
            }
        }
    }];
}

我不能正确的部分是:

void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler

我有 appDelegate.backgroundTransferCompletionHandler 变量,但我不知道如何将它分配给 void(^completionHandler)()。 void(^completionHandler)() 无法被 swift 识别。

在此方面的帮助将不胜感激。

最佳答案

您可能应该处理应用程序委托(delegate),因为这是 completionHandler 闭包的定义所在。您可能会将 backgroundTransferCompletionHandler 属性定义为一个可选的闭包,可能类似于:

var backgroundTransferCompletionHandler: (() -> ())?

func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
    backgroundTransferCompletionHandler = completionHandler

    // do whatever else you want (e.g. reinstantiate background session, etc.)
}

然后,您在问题中引用的代码的 Swift 再现将获取该 completionHandler 的本地副本,如下所示:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

let completionHandler = appDelegate.backgroundTransferCompletionHandler

然后调用它:

completionHandler?()

关于objective-c - Swift - 复制完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28056444/

相关文章:

ios - iOS后台流程

objective-c - 有没有使用 ALAssetsLibrary 快速检查用户访问权限的方法?

iphone - Core-Plot - 制作 CPscatterPlot 动画

objective-c - 无法从一个特定屏幕问题导航仅在 iOS 11 中

ios - 将 CSS 框阴影转换为 iOS 阴影

swift - “MKMapRectIsNull”已被属性 'MKMapRect.isNull' 取代

swift GCD : Why signal handler doesn't work in function

ios - 在 iOS Swift 中获取所有 UIViewControllers 列表

html - 使用动画创建动态变化的背景图像

css - Webkit 浏览器不显示背景图像/填充?