如何在iOS 13中支持低数据模式?

时间:2019-07-04 10:45:45

标签: ios swift networking ios13

iOS 13中引入了“低数据模式”。请参见“设置”部分Apple's iOS 13 overview

  

Low Data Mode

我找不到与此有关的任何开发人员文档。

这是第三方应用开​​发者可以选择使用的as suggested by MacRumors吗?还是在未连接到Wi-Fi as suggested by AppleInsider的情况下只是暂停后台活动?

4 个答案:

答案 0 :(得分:2)

首先,您需要将URLSessionNSURLSession)配置为允许或禁止打开expensiveconstrained网络连接。

您可以通过将URLSession的相应属性allowsExpensiveNetworkAccessallowsConstrainedNetworkAccess更改为falseNO)来实现。

如果URLSessionTask导致的错误是NSURLErrorNotConnectedToInternet错误,其中包含NSURLErrorNetworkUnavailableReasonKey(Objective-C)中的userInfo个条目,或者是一个URLError非零NetworkUnavailableReason属性集(Swift),则需要采取相应措施。

这些原因可能是:

  • expensive
  • constrained
  • cellular

cellular的原因自iOS 7以来就存在,因此这并不是什么新鲜事物,但整个reason枚举都是,因为Apple今年简化了连接类型。

答案 1 :(得分:2)

要确定iOS当前是否处于低数据模式,可以使用网络库:

import Network // Put this on top of your class

let monitor = NWPathMonitor()

monitor.pathUpdateHandler = { path in

    if path.isConstrained {
        // Path uses an interface in Low Data Mode.
    }
    else if path.isExpensive {
        // Path uses an interface that is considered expensive, such as Cellular or a Personal Hotspot.
    }
}

monitor.start(queue: DispatchQueue.global(qos: .background))

答案 2 :(得分:1)

Using **Combine** in URLSession you can support **LowData** mode in iOS 13. 

Steps
- Provide two different resources for high resolution and low resolution(low data mode) 
- If statusCode == 200 (low data mode is disabled in setting).
- If error.networkAvailableReason == .constrained (low data mode is enable in settings)

Checkout the apple WWDC video for more info [https://developer.apple.com/videos/play/wwdc2019/712/][1], GOTO time 16:00 onwards in video.

答案 3 :(得分:0)

这是Xamarin中针对感兴趣的人的解决方案:

NWPathMonitor monitor = new NWPathMonitor();
monitor.SetUpdatedSnapshotHandler(path =>
    {
        if (path.Status == NWPathStatus.Satisfied)
        {
            if(path.IsConstrained)
            {
             // Path uses an interface in Low Data Mode.
            }
        }
    });
monitor.SetQueue(CoreFoundation.DispatchQueue.DefaultGlobalQueue);
monitor.Start();