简体   繁体   English

应用终止后继续NSURLConnection

[英]Continue a NSURLConnection after the App has been terminated

I would like to know how it is possible to continue a async NSURLConnection, which has been started in the foreground, when the app will be terminated. 我想知道当应用程序终止时如何继续在前台启动的异步NSURLConnection。 Currently I am starting a NSURLConnection when the app goes in the background. 目前,当应用程序在后台运行时,我正在启动NSURLConnection。 This works fine as long as the user is slower than the connection, when he wants to terminate the app. 只要用户要终止应用程序,只要用户的速度比连接慢,就可以正常工作。 But when the user is quicker than it, the connection can't be established. 但是,如果用户比它快,则无法建立连接。 Here is my code: 这是我的代码:

// AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    AnObject *newObject = [[AnObject alloc] init];
    [newObject InactiveApp];
}

// AnObject.m
- (void)InactiveApp
{
    self.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

    // setting up the NSURLRequest
    // [...]

    dispatch_async(dispatch_get_main_queue(), ^
    {
        [NSURLConnection connectionWithRequest:request delegate:self];
    });
}
// delegate functions, endBackgroundTask-closing, etc. is following

Unfortunately this is not working and I would like to know whether someone knows another way to fix it. 不幸的是,这不起作用,我想知道是否有人知道另一种解决方法。 There has to be a way which is similar like Snapchat or WhatsApp is doing it, because when you write an message and terminate the app right after pressing send, the message will be delivered. 必须有一种类似Snapchat或WhatsApp的方式,因为当您编写消息并在按send后立即终止应用程序时,消息将被传递。

The only way I could imagine is to do it with a background fetch but I think that is not the best solution, due to the fact that I just want to make one single connection when the App is send to the background. 我可以想象的唯一方法是通过后台获取来实现,但是我认为这不是最佳的解决方案,因为当应用程序发送到后台时,我只想建立一个连接。

I agree with Andy, that you should pursue NSURLSession and a background NSURLSessionConfiguration . 我同意Andy的观点,您应该追求NSURLSession和背景NSURLSessionConfiguration See downloading content in the background section of the App Programming Guide for iOS: Background Execution . 请参阅《适用于iOSApp编程指南:后台执行》的后台部分中的下载内容。

By the way, the idea in your question will work fine (especially if you need support for iOS versions prior to 7.0, where NSURLSession and its background sessions are not available). 顺便说一句,问题中的想法会很好地起作用(特别是如果您需要支持7.0之前的iOS版本,而NSURLSession及其后台会话不可用)。 Two observations regarding your code snippet: 有关您的代码段的两个观察结果:

  1. The way you've written it, would appear that your AnObject would be prematurely deallocated when it falls out of scope and your app would therefore fail when it tried to call the delegate methods. 您编写它的方式似乎是在AnObject超出范围时会过早地重新分配它,因此您的应用程序在尝试调用委托方法时将失败。 Make sure to maintain a strong reference to AnObject . 确保对AnObject保持强烈引用。

  2. Don't forget to call endBackgroundTask when the download is done. 下载完成后,请不要忘记调用endBackgroundTask Likewise (and more subtly), the timeout handler should end the background task, too. 同样(更巧妙),超时处理程序也应结束后台任务。 See the Executing Finite Length Task section of the aforementioned App Programming Guide . 请参阅上述《 App编程指南》中的“ 执行有限长度任务”部分。

By the way, you mention requests continuing after the app is terminated. 顺便说一句,您提到的请求在应用终止后仍在继续。 If a user manually terminates an app, that kills both background tasks contemplated in your question as well as background NSURLSession tasks. 如果用户手动终止应用程序,则会杀死问题中考虑的后台任务以及后台NSURLSession任务。 These are intended to gracefully handle continuing tasks if the app leaves foreground, not if the user manually terminates the app. 它们旨在在应用程序离开前台时(而不是在用户手动终止应用程序时)优雅地处理继续的任务。 The NSURLSession approach gracefully handles terminations due to memory pressure, but not manual termination. NSURLSession方法可以NSURLSession地处理由于内存压力导致的终止,但不能手动终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用终止后发送本地通知 - Sending local notifications after the app has been terminated 应用终止后,使用NSNotification在UIViewController中处理UILocalNotification - Handle UILocalNotification in UIViewController using NSNotification after the app has been terminated 终止后,通过推送通知启动应用程序 - Launch an app with push notification after it has been terminated 如果应用程序已终止,将调用performFetchWithCompletionHandler - Will performFetchWithCompletionHandler be called if the app has been terminated 有没有办法通知服务器该应用程序已被终止? - Is there a way to notify the server that the app has been terminated? 如何检测股票音乐应用程序是否已终止? - How can I detect if the stock music app has been terminated? 应用程序在 IOS 中崩溃 - 客户端已被终止 - App is crashing in IOS - The client has already been terminated 应用终止后删除本地通知 - Removing Local Notifications when app has been terminated 应用终止后无法处理本地通知 - Unable to Handle Local Notification when app has been terminated 应用终止后运行代码...可能吗? (Xcode 8,Swift 3) - Run code after the app has terminated… Possible? (Xcode 8, Swift 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM