3

In my app, i have implemented functionality to check app version using bundleVersion String. Now, i want to run this function everyday at 8:00 a.m. This is kiosk based app which does not go into background. So, app would be active all the time.

I am using UILocalnotification to schedule a notification for that time. Now, my app has other UILocalnotification as well. I am not sure how can i identify notifications in app delegate didReceiveLocalNotification() method.

My method to schedule notification is below

func scheduleNotification() {

    //UIApplication.sharedApplication().cancelAllLocalNotifications()
    let notif = UILocalNotification()


    let calendar = NSCalendar.currentCalendar()
    let date = NSDate()

    var calendarComponents = NSDateComponents()

    calendarComponents = calendar.components([.Day,.Month,.Year], fromDate: date)

    let day = calendarComponents.day
    let month = calendarComponents.month
    let year = calendarComponents.year

    calendarComponents.day = day
    calendarComponents.month = month
    calendarComponents.year = year

    calendarComponents.hour = 8
    calendarComponents.second = 0
    calendarComponents.minute = 0
    calendar.timeZone = NSTimeZone.systemTimeZone()
    let dateToFire = calendar.dateFromComponents(calendarComponents)


    notif.fireDate = dateToFire
    notif.timeZone = NSTimeZone.systemTimeZone()
    notif.repeatInterval = NSCalendarUnit.NSWeekdayCalendarUnit


    UIApplication.sharedApplication().scheduleLocalNotification(notif)

}

Any idea would be appreciated.

Shades
  • 5,568
  • 7
  • 30
  • 48
Nitya
  • 449
  • 1
  • 8
  • 24
  • Try this https://www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification – pedrouan Aug 31 '16 at 17:53
  • Well, you can save a identifier in userInfo object in your generated UILocalNotification. See apple doc: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html#//apple_ref/occ/instp/UILocalNotification/userInfo – firstinq Aug 31 '16 at 18:35
  • @pedrouan: Thanks for reply. I don't know how notification is not getting fired and didRecivedNotification method not called. – Nitya Aug 31 '16 at 18:45

1 Answers1

-1

Following method could help you to execute any task at regular interval , i used this method to call webservice at regular interval to provide searching functionality :

let debounceTimer : NSTimer?
func test() {

        if let timer = debounceTimer {

            timer.invalidate()

        }

        debounceTimer = NSTimer(timeInterval: 0.3, target: self, selector: Selector("putmethodnamewhichneedstocall"), userInfo: nil, repeats: false)

        NSRunLoop.currentRunLoop().addTimer(debounceTimer!, forMode: "NSDefaultRunLoopMode")

    }

For specific time daily Refer to this SO link :Repeating local notification daily at a set time with swift

Hope it helps.

Community
  • 1
  • 1
Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • thanks for reply. I need to run function daily at 8:00 am. With NSTimer , i need to specify the interval. It wouldn't be feasible to give time interval. – Nitya Aug 31 '16 at 19:03
  • updated code with reference link , use this to trigger timer accordingly – Shobhakar Tiwari Aug 31 '16 at 19:14