请帮忙提供建议。出于某种原因,真实 iOS 设备上的后台获取不起作用。通过 Xcode ( Debug -> Sumulate Background Fetch
) 运行时,一切正常。但是当我在真机上安装应用时,这个功能就不起作用了。
我将澄清以下内容:
- 我不会杀死应用程序,而只是将其最小化。
- 该
Background App Refresh
设置在应用程序的设置和整个手机的设置中都启用。 - 我等了一天多——什么也没发生。
在我看来是什么原因:
- 我通过
TestFlight
. - iPhone 上没有安装 SIM 卡。
- 在我的应用程序中,文件的标准名称
AppDelegate
更改为BaseDelegate
,其位置从更改MyApp/MyApp/AppDelegate.swift
为MyApp/MyApp/base/BaseDelegate.swift
(应用程序已正确配置为该文件的新名称和位置)。它会以某种方式影响吗?
我做了什么:
- 我在项目设置中打开了
Background fetch
模式。 - 我将以下代码添加到
info.plist
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
- 以下代码已添加到
AppDelegate.swift
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
var message = UserDefaults.standard.string(forKey: "fetch") ?? "nil"
message = "\(message) | \(Helper.getStringFromDate(Helper.getCurrentDate()))"
UserDefaults.standard.set(message, forKey: "fetch")
// Sending local notification ...
// Receiving data from the server ...
completionHandler(.newData)
}
为了测试应用程序,我发送了一个本地通知,并将时间保存在 UserDefaults 中。