WWDC Quick Look 💓 By SwiftGGTeam
Optimize for 5G networks

Optimize for 5G networks

Watch original video

Highlight

iOS 15 automatically manages network interface selection at the system level through “Auto Switch 5G” and “Smart Data Mode”. Developers should stop making judgments based on network type and useallowsExpensiveNetworkAccessandallowsConstrainedNetworkAccessLet applications adapt to different network environments.

Core Content

From 2G to 5G, each generation of mobile networks brings new application possibilities.2G made SMS and MMS possible, 3G put the internet in your pocket, and 4G made high-definition video calls and real-time multiplayer gaming a daily occurrence.5G has theoretical speeds of up to 4Gbps, latency as low as 7 milliseconds, and allows more devices to be connected simultaneously.

But 5G also brings new challenges.5G networks use more power than LTE, have uneven coverage, and users are likely to be on metered plans.If developers simply use 5G as “faster Wi-Fi”, users may face the risk of rapid battery loss or excessive traffic.

Apple built two system-level technologies into iOS 15 to solve this problem:

Automatic Switch to 5G: The system continuously evaluates the relative performance of Wi-Fi, LTE and 5G and automatically selects the best network interface for the current task.Switch to 5G when downloading large files, use LTE for background low-data tasks, and prioritize cellular when public Wi-Fi is unsafe.

Smart Data Mode: Detects network congestion and automatically migrates to the interface of least resistance.In crowded places such as stadiums and conference centers, the system will migrate the connection from congested Wi-Fi to 5G to ensure that the application’s network service is not interrupted.

This means that developers do not need and should no longer write code to determine “is it currently Wi-Fi or 5G”.The system has already made this decision, and developers only need to pay attention to whether the network is “expensive” or “restricted”.

Detailed Content

Stop making judgments based on network type

(09:18) Common practices in the past:

// Do not do this
if currentNetworkType == .wifi {
    // Download HD video
} else if currentNetworkType == .cellular {
    // Download only thumbnails
}

This approach will prevent applications from obtaining the best experience in the 5G era.5G can be up to 10 times faster than public Wi-Fi, and the system supports multi-path networks.If the user is connected to Wi-Fi and 5G at the same time, the system may select 5G as the data transmission channel.

The correct approach is to determine the network characteristics through the properties of the URLSession or Network framework:

let config = URLSessionConfiguration.default
config.allowsExpensiveNetworkAccess = true
config.allowsConstrainedNetworkAccess = false
let session = URLSession(configuration: config)

Key points:

  • allowsExpensiveNetworkAccess: Controls whether requests are allowed on expensive networks (such as metered cellular)
  • allowsConstrainedNetworkAccess: Controls whether requests are allowed to be performed on restricted networks (such as low data mode)
  • These two properties replace the traditional network type check
  • The system automatically derives “Expensive” and “Restricted” status based on the user’s cellular plan and data mode settings

Get the most out of 5G using advanced frameworks

(09:46) Apple’s advanced networking framework has been optimized for 5G:

// AVFoundation automatically adapts to high 5G bandwidth
let playerItem = AVPlayerItem(url: highQualityVideoURL)
let player = AVPlayer(playerItem: playerItem)
// The system automatically requests higher-bitrate video streams on 5G

// URLSession automatically uses more aggressive caching policies on 5G
let config = URLSessionConfiguration.default
config.allowsExpensiveNetworkAccess = true
let session = URLSession(configuration: config)

// Large file download task
let downloadTask = session.downloadTask(with: largeFileURL)
downloadTask.resume()

Key points:

  • AVFoundationUnder 5G, higher quality video streams will be automatically requested, similar to the behavior of Apple TV and Apple Music
  • CallKitDelay optimization for VoIP under 5G
  • URLSessionandNetworkThe framework has been tuned to automatically exploit the high bandwidth of 5G
  • Apple News will cache articles for offline reading under 5G, iCloud photos will be synced under 5G, and system backup will be automatically performed over 5G when Wi-Fi is unavailable

Adapt to restricted and expensive network paths

(10:39) Users can choose three data modes in the settings:

Data modelNetwork characteristicsApplicable scenarios
Allow More Data on 5GInexpensive, similar to Wi-FiUnlimited data plan
StandardExpensiveOrdinary data plan
Low Data ModeRestrictedRoaming or traffic constraints

Check network properties in the Network framework:

import Network

let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
    if path.isConstrained {
        // Low Data Mode: pause nonessential downloads and reduce image quality
        self.reduceDataUsage()
    } else if path.isExpensive {
        // Expensive network: ask the user whether to continue the large download
        self.promptForExpensiveDownload()
    } else {
        // Non-expensive network: use normally and preload content
        self.enableAggressivePrefetching()
    }
}
monitor.start(queue: DispatchQueue.global())

Key points:

  • NWPathofisExpensiveIndicates whether the current network path is expensive (usually refers to metered cellular)
  • NWPathofisConstrainedIndicates whether it is in low data mode
  • These two attributes are the only basis for judging network service availability
  • If the application implements “expensive” based policies, settings should be provided for users to override

Introduction to 5G network types

(05:15) There are two main modes of 5G deployment:

  • NSA (Non-Standalone): Based on the existing LTE core network, using both LTE and 5G links to schedule traffic, supporting sub-7GHz and millimeter waves
  • SA (Standalone): Completely based on the new 5G core network, also supports sub-7GHz and millimeter waves, and has lower latency than LTE

The measured speed of Apple Park reaches 3Gbps, which is 20 times that of LTE, and the ping delay is only 7 milliseconds.But 5G consumes more power than LTE, and the system automatically balances performance and power consumption.

Core Takeaways

  • Adaptive Media Quality: In video playback applications, dynamically adjust video bitrate based on isExpensive and isConstrained. Users on unlimited 5G plans can automatically play 4K, standard mode can play 1080p, and Low Data Mode can reduce quality to 720p or pause automatic playback. Entry APIs: NWPathMonitor + AVPlayerItem.preferredPeakBitRate.

  • Smart preloading: In news or social apps, preload the next few pages of content and high-definition images when the network is not expensive; stop preloading when an expensive or restricted network is detected, and only load content explicitly requested by the user.useallowsExpensiveNetworkAccessControl backendURLSessionbehavior.

  • 5G Low Latency Multiplayer: Develop real-time battle games that take advantage of 5G’s 7ms latency.CooperateNetworkframedNWConnectionEstablish QUIC connections to reduce handshake delays.In crowded places like stadiums, the system automatically switches to 5G to avoid Wi-Fi congestion.

  • Background large file transfer strategy: Used for uploading and downloading large files in cloud disk or file synchronization applicationsURLSessionDownloadTaskbackground mode.CooperateallowsExpensiveNetworkAccessProperties to let the user select “Allow cellular sync” in settings.Automatically turns on full-speed sync when 5G is detected and the network is inexpensive.

Comments

GitHub Issues · utterances