为什么我不能在我的应用程序中使用swift 3中的后台模式?

时间:2022-06-02 21:08:42

I want to use background mode in my app so in the capabilities I ticked background fetch and notification But The problem is that just in the simulator every things are ok and in the real iPhone the app will freeze in the background

我想在我的应用程序中使用后台模式,所以在功能上我勾选了后台获取和通知但问题是,只是在模拟器中,每件事情都可以,在真正的iPhone中应用程序将在后台冻结

1 个解决方案

#1


1  

You can't fetch the the latest content from a server in background mode as Apple only allowed to fetch content in specific reasons eg.

您无法在后台模式下从服务器获取最新内容,因为Apple仅允许以特定原因获取内容,例如。

  • Implementing Long-Running Tasks
  • 实现长期运行的任务
  • For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

    对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。在iOS中,只允许在后台运行特定的应用类型:

    • Apps that play audible content to the user while in the background, such as a music player app
    • 在后台播放用户可听内容的应用,例如音乐播放器应用
    • Apps that record audio content while in the background
    • 在后台录制音频内容的应用程序
    • Apps that keep users informed of their location at all times, such as a navigation app
    • 可让用户随时了解其位置的应用,例如导航应用
    • Apps that support Voice over Internet Protocol (VoIP)
    • 支持互联网协议语音(VoIP)的应用
    • Apps that need to download and process new content regularly
    • 需要定期下载和处理新内容的应用
    • Apps that receive regular updates from external accessories
    • 从外部配件接收定期更新的应用程序

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

实现这些服务的应用程序必须声明它们支持的服务,并使用系统框架来实现这些服务的相关方面。声明服务可以让系统知道您使用哪些服务,但在某些情况下,系统框架实际上会阻止您的应用程序被挂起。

Instead of these you can fetch the content from server in short period of time eg. 1-2 mins depending of cpu usage

您可以在短时间内从服务器获取内容,而不是这些内容。 1-2分钟取决于CPU使用情况

func doUpdate() {
    DispatchQueue.global(qos: .default).async(execute: {() -> Void in
        self.beginBackgroundUpdateTask()
        var response: URLResponse? = nil
        var error: Error? = nil
        let responseData: Data? = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: response)
        // Do something with the result
        self.endBackgroundUpdateTask()
    })
}

func beginBackgroundUpdateTask() {
    backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withExpirationHandler: {() -> Void in
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
    backgroundUpdateTask = UIBackgroundTaskInvalid
}

#1


1  

You can't fetch the the latest content from a server in background mode as Apple only allowed to fetch content in specific reasons eg.

您无法在后台模式下从服务器获取最新内容,因为Apple仅允许以特定原因获取内容,例如。

  • Implementing Long-Running Tasks
  • 实现长期运行的任务
  • For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

    对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。在iOS中,只允许在后台运行特定的应用类型:

    • Apps that play audible content to the user while in the background, such as a music player app
    • 在后台播放用户可听内容的应用,例如音乐播放器应用
    • Apps that record audio content while in the background
    • 在后台录制音频内容的应用程序
    • Apps that keep users informed of their location at all times, such as a navigation app
    • 可让用户随时了解其位置的应用,例如导航应用
    • Apps that support Voice over Internet Protocol (VoIP)
    • 支持互联网协议语音(VoIP)的应用
    • Apps that need to download and process new content regularly
    • 需要定期下载和处理新内容的应用
    • Apps that receive regular updates from external accessories
    • 从外部配件接收定期更新的应用程序

Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

实现这些服务的应用程序必须声明它们支持的服务,并使用系统框架来实现这些服务的相关方面。声明服务可以让系统知道您使用哪些服务,但在某些情况下,系统框架实际上会阻止您的应用程序被挂起。

Instead of these you can fetch the content from server in short period of time eg. 1-2 mins depending of cpu usage

您可以在短时间内从服务器获取内容,而不是这些内容。 1-2分钟取决于CPU使用情况

func doUpdate() {
    DispatchQueue.global(qos: .default).async(execute: {() -> Void in
        self.beginBackgroundUpdateTask()
        var response: URLResponse? = nil
        var error: Error? = nil
        let responseData: Data? = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: response)
        // Do something with the result
        self.endBackgroundUpdateTask()
    })
}

func beginBackgroundUpdateTask() {
    backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withExpirationHandler: {() -> Void in
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
    backgroundUpdateTask = UIBackgroundTaskInvalid
}