2

请帮忙提供建议。出于某种原因,真实 iOS 设备上的后台获取不起作用。通过 Xcode ( Debug -> Sumulate Background Fetch) 运行时,一切正常。但是当我在真机上安装应用时,这个功能就不起作用了。

我将澄清以下内容:

  1. 我不会杀死应用程序,而只是将其最小化。
  2. Background App Refresh设置在应用程序的设置和整个手机的设置中都启用。
  3. 我等了一天多——什么也没发生。

在我看来是什么原因:

  1. 我通过TestFlight.
  2. iPhone 上没有安装 SIM 卡。
  3. 在我的应用程序中,文件的标准名称AppDelegate更改为BaseDelegate,其位置从更改MyApp/MyApp/AppDelegate.swiftMyApp/MyApp/base/BaseDelegate.swift(应用程序已正确配置为该文件的新名称和位置)。它会以某种方式影响吗?

我做了什么:

  1. 我在项目设置中打开了Background fetch模式。
  2. 我将以下代码添加到info.plist
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
</array>
  1. 以下代码已添加到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 中。

4

0 回答 0