swift - iOS - How can I schedule something once a day? -
i know there nstimer.scheduledtimerwithinterval
which used this:
override func viewdidload() { super.viewdidload() var timer = nstimer.scheduledtimerwithtimeinterval(0.4, target: self, selector: selector("update"), userinfo: nil, repeats: true) } func update() { // cool }
but assume view var timer
living must living too, ie: can't close view (am right?)
how can schedule once day in ios, eg: everyday @ 8pm send notification?
in android
i've achieved following way:
i've used thing called
alarmmanager
, similar iosscheduledtimerwithinterval
large intervals, running inside background service.at startup (boot), there background service setups alarm again. covers case when android device shut down (and background service shut down too)
so, in ios, there scheduledtimerwithinterval
large intervals?
will need set again interval if iphone/ipad rebooted?
yes, use nstimer
, app has running either in foreground or background. apple quite particular allowing types of apps continue run in background (in effort make sure don't have apps randomly running on own prerogative , killing our batteries in process and/or affecting our performance while using device).
when "notification", mean notifying user of something?
in case, alternative here create
uilocalnotification
, user notification (assuming they've granted app permission perform notifications), presented when app not running.for example, register local notifications:
let application = uiapplication.sharedapplication() let notificationtypes: uiusernotificationtype = .badge | .sound | .alert let notificationsettings = uiusernotificationsettings(fortypes: notificationtypes, categories: nil) application.registerusernotificationsettings(notificationsettings)
and schedule repeating notification:
let notification = uilocalnotification() notification.firedate = ... notification.alerttitle = ... notification.alertbody = ... notification.repeatinterval = .calendarunitday application.schedulelocalnotification(notification)
for more information, see local , remote notification programming guide.
or mean initiating process, such fetching data remote server.
if want app fetch data if app isn't running, can use background fetch. see fetching small amounts of content opportunistically in app programming guide ios.
note, background fetch, don't specify when data retrieved, rather system check data @ time of own choosing. reportedly factors in considerations ranging how user uses app, how requests see if there data result in there being new data retrieve, etc. have no direct control on timing of these background fetches.
Comments
Post a Comment