简体   繁体   English

当用户按下主页按钮时如何编写不会暂停的代码

[英]How to write code that will not pause when user press home button

I want to implement image sync in my ios app. 我想在我的ios应用中实现图像同步。 for this what i have done is 为此,我所做的是

  1. take a photo 拍个照
  2. upload that photo to firebase 将该照片上传到firebase
  3. get the firebase stored url and send it to an api server so that server stores that info to a database 获取Firebase存储的url并将其发送到api服务器,以便服务器将该信息存储到数据库

It works ok as long as the app is running, but when I exit out from the app pressing home button everything pauses. 只要应用程序正在运行,它就可以正常工作,但是当我退出应用程序并按“主页”按钮时,所有内容都会暂停。

So how can run a code that will not pause if app goes to home page? 那么,如何运行在应用程序进入首页时不会暂停的代码?

As per this documentation, 根据本文档,

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) Apps that need to download and process new content regularly 支持互联网协议语音(VoIP)的应用程序需要定期下载和处理新内容的应用程序

  • 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. 声明服务可以使系统知道您使用的服务,但是在某些情况下,实际上是系统框架阻止了应用程序被挂起。

Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html 链接: https//developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Here is how to implement (taken from this answer by Ashley Mills ): 这是实现方法(摘自Ashley Mills的 回答 ):

 func doUpdate () { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { let taskID = beginBackgroundUpdateTask() var response: NSURLResponse?, error: NSError?, request: NSURLRequest? let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) // Do something with the result endBackgroundUpdateTask(taskID) }) } func beginBackgroundUpdateTask() -> UIBackgroundTaskIdentifier { return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({}) } func endBackgroundUpdateTask(taskID: UIBackgroundTaskIdentifier) { UIApplication.sharedApplication().endBackgroundTask(taskID) } 

[Swift3] This is kind of worked for me [Swift3]这对我有用

func doUpdate () {
    DispatchQueue.global(qos: .background).async {
        let taskID = self.beginBackgroundUpdateTask()
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5), execute: {
            print("printing after 10 min")
        })
        DispatchQueue.main.async {
            self.endBackgroundUpdateTask(taskID: taskID)
        }
    }
}


func beginBackgroundUpdateTask() -> UIBackgroundTaskIdentifier {
    return UIApplication.shared.beginBackgroundTask(expirationHandler: {})
}


func endBackgroundUpdateTask(taskID: UIBackgroundTaskIdentifier) {
    UIApplication.shared.endBackgroundTask(taskID)
}

Not sure what is the max time for this to complete since i see there is a expirationHandler 由于我看到有一个expirationHandler ,因此不确定完成此操作的最长时间是多少

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户按下主屏幕按钮时,如何使我的广播流应用程序继续唱歌? - How can I make my Radio Streaming Application keep singing when the user press the home button? 按下主屏幕按钮后视图消失 - The View has Disappear when press home button 按下主屏幕按钮在后台播放音乐 - Play music in background when home button is press 用户按下主屏幕按钮(iOS 8)时,WebView中的HTML页面不会触发“ visibilitychange”事件 - Html page in a WebView does not fire 'visibilitychange' event when user press home button (iOS 8) 在 iOS 4.3 中,当您按下主页按钮或按下开/关按钮时,我如何区分背景模式? - in iOS 4.3, how i can differentiate between the background mode, when you press the home button or when you press the on/off button? 如何长按触发iOS主页按钮 - How to trigger iOS home button long press 当我在Xcode调试区域中按下暂停按钮时,如何跳转到特定的断点? - How to jump to a specific breakpoint when I press the pause button in Xcode debug area? 当用户点击暂停按钮时,如何立即停止UISlider? - How to stop UISlider immediately when the user taps the pause button? 挂钩按钮按下 - Hook home button press 当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重新启动NSTimer.scheduledTimerWithTimeInterval? - How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM