简体   繁体   中英

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

I want to implement image sync in my ios app. for this what i have done is

  1. take a photo
  2. upload that photo to firebase
  3. get the firebase stored url and send it to an api server so that server stores that info to a database

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:

  • 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

  • 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

Here is how to implement (taken from this answer by 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM