我的應用程序不斷在Kiosk模式下運行。在特定時間每24小時一次,我需要將一些數據從核心數據同步到Web服務。每天在特定時間調用方法
我知道如何做同步件,但我不知道如何安排應用程序在每天的特定時間進行同步呼叫,例如,在上午02:45。
當應用程序不斷運行時是否可以這樣做?
我的應用程序不斷在Kiosk模式下運行。在特定時間每24小時一次,我需要將一些數據從核心數據同步到Web服務。每天在特定時間調用方法
我知道如何做同步件,但我不知道如何安排應用程序在每天的特定時間進行同步呼叫,例如,在上午02:45。
當應用程序不斷運行時是否可以這樣做?
使用本地通知。這裏是一個教程:http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/
希望這有助於u開始......
這還有:
有沒有辦法做到這一點,而不使用本地通知? – motionpotion
如果用戶沒有授予權限會怎麼樣? – kelin
想通了這一點得益於@lakesh提示。發佈解決方案以幫助某人,因爲我發現NSNotification示例起初很難理解。
在我的主視圖控制器我增加了以下方法:
- (void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 02];
[components setMinute: 45];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
notif.fireDate = dateToFire;
notif.timeZone = [NSTimeZone systemTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
這爲今天的通知消防日在上午02時45分和日常復發。
在viewDidLoad中在我的視圖控制器我調用上面的方法:
[self scheduleNotification];
在我執行以下操作的appdelegate:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
//call your method that you want to do something in your app
}
如果你這樣做,用戶是否會看到一條空白消息? – anoop4real
一個簡單的解決方案只是設置NSTimer
檢查當前日期每分鐘(或每秒)。如果當前日期大於所需日期,則啓動該方法並更新所需的日期。
當然有可能。 :)我無法具體說明如何,但它必須與警報邏輯類似,在特定時間觸發某些操作。 –
@motionpotion只是看看這個鏈接可能是你想要這個http://redth.codes/ios7-recipe-background-fetching/ – Leena
@motionpotion:如果你有解決方案,那麼請與我分享...我是嘗試相同的任務爲最後幾天...幫助我 –