私のアプリでは、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")
}
:
これを試してください。https://www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification – pedrouan
あなたの生成されたUILocalNotificationのuserInfoオブジェクトに識別子を保存することができます。 Appleのdocを参照してください:https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/index.html#//apple_ref/occ/instp/UILocalNotification/userInfo – firstinq
@pedrouan:返信ありがとうございます。通知がどのように起動されず、didRecivedNotificationメソッドが呼び出されないのかわかりません。返信ありがとう。 – Nitya