xcode 即使配置了正确的后台模式,iOS 后台获取也不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29596618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:58:03  来源:igfitidea点击:

iOS Background Fetch Not Working Even Though Correct Background Mode Configured

iosxcodeswiftbackground-process

提问by a432511

My app has background modes enabled with Background Fetch checked and I validated the plist includes the appropriate fetch mode.

我的应用程序启用了后台模式并选中了后台提取,并且我验证了 plist 包含适当的提取模式。

I have also configured the interval as follows:

我还按如下方式配置了间隔:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);
    return true;
}

And I have added the handler as follows:

我添加了如下处理程序:

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // Get some new data here
    completionHandler(UIBackgroundFetchResult.NewData);
}

I tried going to Debug->Simulate Background Fetch, but no matter what it never enters the performFetchWithCompletionHandler. I also tried to run the app under a scheme that has the "Launch due to a background fetch event" option checked. Running under this scheme simply launches the application as usual in the simulator with no call to performFetchWithCompletionHandler.

我尝试去调试->模拟后台获取,但无论如何它永远不会进入performFetchWithCompletionHandler. 我还尝试在选中“由于后台获取事件而启动”选项的方案下运行该应用程序。在此方案下运行只需像往常一样在模拟器中启动应用程序,无需调用performFetchWithCompletionHandler.

Any ideas?

有任何想法吗?

Thank you!

谢谢!

EDIT:This appears to be affecting the release version of my app as well so it may not be isolated to the simulator. I am running Swift 1.2.

编辑:这似乎也影响了我的应用程序的发布版本,因此它可能不会与模拟器隔离。我正在运行 Swift 1.2。

EDIT 2:My bug report was just closed because it is a duplicate of another bug report outlining the same issue. There is still no information confirming the issue is isolated to the simulator.

编辑 2:我的错误报告刚刚关闭,因为它是另一个概述同一问题的错误报告的副本。仍然没有信息确认问题与模拟器无关。

EDIT 3:No mention of a fix in the Xcode 6.4 Beta 2 release notes. :-(

编辑 3:在 Xcode 6.4 Beta 2 发行说明中没有提到修复。:-(

回答by Tobias

Here's the only way I've found to test background fetch.

这是我发现测试后台获取的唯一方法。

  1. Edit your scheme
  2. Select Run
  3. Select Options
  4. Check the "Launch due to background fetch event" option
  5. Plug-In your iOS Device and run the application on it. It does not work in the iOS Simulator.
  1. 编辑您的计划
  2. 选择运行
  3. 选择选项
  4. 选中“由于后台获取事件而启动”选项
  5. 插入您的 iOS 设备并在其上运行应用程序。它在 iOS 模拟器中不起作用。

Visual of Step 2-4

步骤 2-4 的视觉效果

回答by odemolliens

Take a look on the XCode release note: https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

查看 XCode 发行说明:https: //developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

"The Xcode menu command Simulator Background Fetch does not work.

“Xcode 菜单命令 Simulator Background Fetch 不起作用。

Use the menu command in iOS Simulator instead. (20145602)"

请改用 iOS 模拟器中的菜单命令。(20145602)"

回答by Vitaliy Gozhenko

You can create your own background fetch push notification to your real device via Houston

您可以通过Houston创建自己的后台获取推送通知到您的真实设备

  1. install Houston: Open Terminal app and use sudo gem install houston
  2. create PEM certificate for your app Generate .pem file Used to setup Apple PUSH Notification
  3. Get your device token from
  1. 安装休斯顿:打开终端应用程序并使用 sudo gem install houston
  2. 为您的应用程序创建 PEM 证书生成 .pem 文件用于设置 Apple 推送通知
  3. 获取您的设备令牌

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

  1. run Terminal and use
  1. 运行终端并使用

for usual push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line!"

对于通常的推送通知: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -m "Hello from the command line!"

for background fetch push notification: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -n

对于后台获取推送通知: apn push "<your_device_token>" -c /path/to/apple_push_notification.pem -n

More options for apnapp:

apn应用程序的更多选项:

  c.option '-m', '--alert ALERT', 'Body of the alert to send in the push notification'
  c.option '-b', '--badge NUMBER', 'Badge number to set with the push notification'
  c.option '-s', '--sound SOUND', 'Sound to play with the notification'
  c.option '-y', '--category CATEGORY', 'Category of notification'
  c.option '-n', '--[no]-newsstand', 'Indicates content available for Newsstand'
  c.option '-d', '--data KEY=VALUE', Array, 'Passes custom data to payload (as comma-delimited "key=value" declarations)'
  c.option '-P', '--payload PAYLOAD', 'JSON payload for notifications'
  c.option '-e', '--environment ENV', [:production, :development], 'Environment to send push notification (production or development (default))'
  c.option '-c', '--certificate CERTIFICATE', 'Path to certificate (.pem) file'
  c.option '-p', '--[no]-passphrase', 'Prompt for a certificate passphrase'

回答by Artem Stepanenko

In Xcode 8.3.2this problem doesn't exist. Debug->Simulate Background Fetchdoes its job. It even works on a simulator.

Xcode 8.3.2中不存在此问题。Debug->Simulate Background Fetch完成它的工作。它甚至可以在模拟器上运行。

回答by New To iOS Dev

This method is called.

调用此方法。

 func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

}

}

you forgot to write @escaping after completionHandler

你忘了在completionHandler之后写@escaping