2015-09-03 51 views
1

大家好, 我想每天早晨10.00 AM更新本地數據庫。應用程序正在運行或死亡或在後臺無關緊要。有什麼機制在IOS像服務類在Android或任何東西?每天早上10點撥打一個方法

我知道我們可以通過使用本地通知來實現此目的,但有沒有辦法在不使用本地通知的情況下執行此操作?

+0

首先我們不能更新如果我們的應用程序沒有運行,以及如何使用localNotification實現這一點,因爲更新過程不能完成,直到你點擊通知和應用程序打開。 – baydi

+0

@baydi請仔細閱讀上面的問題第一....我非常清楚本地通知是如何工作的... http://stackoverflow.com/questions/16653991/calling-a-method-at-specific-time-每天......請參閱此鏈接.... –

回答

-1

你不能做到這一點..

由於蘋果不允許運行任何方法不斷甚至應用程序在後臺...

唯一的辦法是使用本地通知。

我知道你是沒有什麼用本地通知,但我保持代碼可能會有所幫助......

ViewController.m

UIApplication* app = [UIApplication sharedApplication]; 
    NSArray* oldNotifications = [app scheduledLocalNotifications]; 

    if ([oldNotifications count] > 0) 
     [app cancelAllLocalNotifications]; 

    NSCalendar* myCalendar = [NSCalendar currentCalendar]; 
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
              fromDate:[NSDate date]]; 

    [components setHour: 10]; 
    [components setMinute: 00]; 
    [components setSecond: 0]; 
    NSDate *alertTime = [myCalendar dateFromComponents:components]; 

    NSLog(@"@@@@@@@@@@@@Local Notification Set Time is:%@",alertTime); 

    UILocalNotification *notifyAlarm = [[UILocalNotification alloc] 
        init]; 
    if (notifyAlarm) 
    { 
     notifyAlarm.fireDate = alertTime; 
     notifyAlarm.userInfo=[[NSDictionary alloc]initWithObjectsAndKeys:@「ViewController」,@「Home」, nil]; 
     //notifyAlarm.applicationIconBadgeNumber=1; 
     notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; 
     notifyAlarm.repeatInterval = NSHourCalendarUnit; 
     notifyAlarm.soundName = @"iPhone_Tritone.mp3"; 
     notifyAlarm.alertBody = @「Calling Method」; 
     [app scheduleLocalNotification:notifyAlarm]; 
    } 

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    NSLog(@"@@@@@@@@@@Did Receive Local [email protected]@@@@@@@@@@ is:%@",notification.userInfo); 

    [self callingUrMethod]; 
} 

否則,您可以通過使用推送通知來實現它在服務器每天上午10點基於iPhone設備時間...

+0

感謝您重播....如果您可以找到任何解決方案,然後plz與我分享。 –

相關問題