2016-08-31 4 views
3

私のアプリでは、bundleVersion Stringを使ってアプリケーションのバージョンをチェックする機能を実装しました。さて、私は毎日午前8時にこの機能を実行したいと考えています。これはバックグラウンドに入っていないキオスクベースのアプリです。したがって、アプリは常にアクティブになります。swiftを使って特定の時間に関数を実行する方法

私はUILocalnotificationを使用してその時刻の通知をスケジュールしています。今、私のアプリは他のUILocalnotificationも持っています。私はどのように私は、アプリケーションの代理人didReceiveLocalNotification()メソッドで通知を識別することができますか分からない。通知をスケジュールする

私の方法は、任意のアイデアをいただければ幸いです

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) 

} 

を下回っています。このSOリンクを参照してください毎日特定の時間のために

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") 

    } 

+0

これを試してください。https://www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification – pedrouan

+0

あなたの生成されたUILocalNotificationのuserInfoオブジェクトに識別子を保存することができます。 Appleのdocを参照してください:https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html#//apple_ref/occ/instp/UILocalNotification/userInfo – firstinq

+0

@pedrouan:返信ありがとうございます。通知がどのように起動されず、didRecivedNotificationメソッドが呼び出されないのかわかりません。返信ありがとう。 – Nitya

答えて

0

はあなたが定期的に任意のタスクを実行するために助けることができる方法に続いて、私は検索機能を提供するために定期的にWebサービスを呼び出すために、このメソッドを使用しました:Repeating local notification daily at a set time with swift

希望します。

+0

毎日午前8時には機能を実行する必要があります。 NSTimerで、私は間隔を指定する必要があります。時間間隔を与えることは実現可能ではない。 – Nitya

+0

参照リンクでコードを更新し、これを使用してタイマーをトリガします –

関連する問題