【问题标题】:Sending Data To Server While App is Running in Background in iOS 7在 iOS 7 中应用程序在后台运行时向服务器发送数据
【发布时间】:2014-01-18 11:17:29
【问题描述】:

我希望从在 iOS 7 后台运行的应用程序向我的服务器发送一些数据(位置坐标)。我为此目的使用 NSURLSessions。
我对我应该为此目的使用的会话类型感到困惑:
默认会话:我们可以在后台创建和使用默认会话吗?如果是,它可以优雅地处理网络故障。
后台会话:我可以使用后台会话将数据作为 NSData 对象上传吗?

PS:在这两种情况下处理网络故障的一些代码将非常感激。

【问题讨论】:

    标签: ios7 background-process data-transfer nsurlsession


    【解决方案1】:

    我建议你使用后台会话。

    您可以使用默认会话或常规 NSURLConnection,并使用最多 10 分钟的后台到期标识符来完成您的任务

    后台任务标识符文档

    https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:

    在后台任务中使用 NSURLConnection(您还可以获得 iOS 6 支持)

    NSURLConnection best practise when enter background

    根据新标准,您可以使用后台会话来处理下载/服务器上传。

    Apple 关于处理后台下载的文档

    https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW1

    Apple 的一个简单项目展示了这项技术

    https://developer.apple.com/library/ios/samplecode/SimpleBackgroundTransfer/Listings/SimpleBackgroundTransfer_APLViewController_m.html#//apple_ref/doc/uid/DTS40013416-SimpleBackgroundTransfer_APLViewController_m-DontLinkElementID_7

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多