ios - Swift - Best way to run timer in background for iphone apps -


i'm totally new ios development , working on iphone cooking app gives user choice of 3 'timer' options. first timer runs 6 mins, second 8.5 mins , last 11 mins.

once timer finishes counting down plays audio file , displays message within app screen. works perfectly, except i've discovered in testing timer stops running while user goes app (e.g. checking email, using safari, etc). obviously, defeats purpose of app user needs know when timer finished can next step (e.g. remove saucepan stove).

i've researched background modes , getting confused. seems literally have no reason (according apple) run app in background (i.e. it's not playing music, using locations services, etc). keep reading there's 10 min limit running in background otherwise.

i come across idea of local , remote notifications, page referred no longer exists on apple's developer site. i'm @ loss , confused.

is there way me app work in background 11 minutes? if so, how?


here's update. i've been trying head around local notifications , background tasks.

local notifications showed promise, i'm not sure how implement in scenario? how ensure right amount of time passes before notification appears/plays sound?

for example, user selects button 'soft boiled eggs' @ 12:00:00pm , app starts counter 6 mins. @ 12:01:20pm user reads email, taking 30 seconds before putting phone down @ 12:01:50 read paper. let's assume @ 12:02:50 phone goes lock mode, how ensure local notification triggers 3mins , 10secs later make whole 6mins , play sound file notifying user eggs ready.

background tasks may work scenario if can start , restart background tasks allow timer complete before playing sound.

below snippet of code (relating eggs example above) hope put app in context:

@ibaction internal func buttonsoft(sender: uibutton) {      counter = 360     timerdisplay.text = string("06:00")      timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: selector("updatecounter"), userinfo: "eggs done!!", repeats: true)       buttonsoft.alpha = 0.5     buttonmedium.alpha = 0.5     buttonhard.alpha = 0.5     buttonsoft.enabled = false     buttonmedium.enabled = false     buttonhard.enabled = false   }     @ibaction internal func buttonmedium(sender: uibutton) {      counter = 510     timerdisplay.text = string("08:30")      timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: selector("updatecounter"), userinfo: "eggs done!!", repeats: true)       buttonsoft.alpha = 0.5     buttonmedium.alpha = 0.5     buttonhard.alpha = 0.5     buttonsoft.enabled = false     buttonmedium.enabled = false     buttonhard.enabled = false   }  @ibaction internal func buttonhard(sender: uibutton) {      counter = 660     timerdisplay.text = string("11:00")      timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: selector("updatecounter"), userinfo: "eggs done!!", repeats: true)       buttonsoft.alpha = 0.5     buttonmedium.alpha = 0.5     buttonhard.alpha = 0.5     buttonsoft.enabled = false     buttonmedium.enabled = false     buttonhard.enabled = false   }   func stoptimer() {      if counter == 0 {         timer.invalidate()      } }   func updatecounter() {     counter--       let seconds = counter % 60     let minutes = (counter / 60) % 60     let strminutes = minutes > 9 ? string(minutes) : "0" + string(minutes)     let strseconds = seconds > 9 ? string(seconds) : "0" + string(seconds)     if seconds > 0 {         timerdisplay.text = "\(strminutes):\(strseconds)"     }      else {         stoptimer()         timerdisplay.text = string("eggs done!!")         soundplayer.play()     }  }  @ibaction func buttonreset(sender: anyobject) {     timer.invalidate()     stoptimer()     timerdisplay.text = string("choose eggs:")      buttonsoft.alpha = 1.0     buttonmedium.alpha = 1.0     buttonhard.alpha = 1.0     buttonsoft.enabled = true     buttonmedium.enabled = true     buttonhard.enabled = true  } 

in terms of running background tasks, i've come across following example of code.

to create background task:

func somebackgroundtask(timer:nstimer) {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), { () -> void in         println("do background task")          dispatch_async(dispatch_get_main_queue(), { () -> void in             println("update ui")          })     }) } 

and below line of code (i think) use timer run above function:

var timer = nstimer(timeinterval: 1.0, target: self, selector: "somebackgroundtask:", userinfo: nil, repeats: true) 

and below code stop all:

timer.invalidate() 

so, how adapt scenario? if isn't possible, how use local notifications in app?

or give on iphone version of app (my apple watch version seems work fine).

in word, no. can ask background time, recent versions of ios give 3 minutes.

if background sound playing app or navigation app allowed run in background longer, have ask permissions , app review board check.

the bottom line third parties can't timer app counts down arbitrary time longer 3 minutes.

you might want use timed local notifications. can make play sound when go off. search in xcode docs on uilocalnotification.


Comments