0

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
  • What do you want to do in the background? Your app is only allowed to keep running in the background in very specific cases. For example, these include playing audio, getting location updates, or fetching the latest content from a server. – CodingYoshi Jul 09 '17 at 08:33
  • I want to fetching the latest content from a server – Saeed Rahmatolahi Jul 09 '17 at 08:40

1 Answers1

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.

  • 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:

    • 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.

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

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
}
Malik
  • 3,763
  • 1
  • 22
  • 35
Optimus
  • 800
  • 5
  • 16
  • @Optimus I want to use local notification with jason and This will work in the simulator But in the Real iPhone I have some problems ! so when should I use your codes in my app ? in app delegate or in ViewDidLoad in ViewController ? or I am wasting my time and the only way for me is to use push notification? – Saeed Rahmatolahi Jul 10 '17 at 05:55
  • @SaeedRahmatolahi you can use local notification but if user didn't click on notification then you can't fetch data from server . The above code can be use in appDelegate or singleton class and the code will work but for some time, please find below link https://stackoverflow.com/questions/28275415/how-long-does-apple-permit-a-background-task-to-run – Optimus Jul 10 '17 at 07:35
  • @Optimus ok I read this link and (I used notification function in my viewDidLoad so it will run automatically) I want to check every minutes server for getting new notification and after 24 hours this function will finish! In the simulator It will work But In real Devices It doesn't work anymore So Do you think the only way for me is to using push notification? – Saeed Rahmatolahi Jul 10 '17 at 07:41
  • Yeah you can use notifications – Optimus Jul 10 '17 at 10:45
  • So please help me or write a link here to teach me how to use push notification from beginning to the last step – Saeed Rahmatolahi Jul 10 '17 at 10:46