2013-08-01 110 views
10

我正在處理一個應用程序,如果終止將無法正常工作。它有一些後臺任務。如果應用已終止,我想顯示本地通知。有這樣做的應用程序,這意味着這是可行的。但我無法找到一個方法。當地通知應用程序終止

我試圖在applicationWillTerminate中設置本地通知:appdelegate的方法,以及在我的viewcontroller中添加了應用程序終止的通知,但是當應用程序實際終止時,沒有任何方法會被調用。

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    NSLog(@"terminated"); 
    UIApplication * app = [UIApplication sharedApplication]; 
    NSDate *date = [[NSDate date] dateByAddingTimeInterval:15]; 
    UILocalNotification *alarm = [[UILocalNotification alloc] init] ; 
    if (alarm) { 
     alarm.fireDate = [NSDate date]; 
     alarm.timeZone = [NSTimeZone defaultTimeZone]; 
     alarm.repeatInterval = 0; 
     alarm.alertBody = @"This app does not work if terminated"; 
     alarm.alertAction = @"Open"; 
     [app scheduleLocalNotification:alarm]; 
    } 

    [app presentLocalNotificationNow:alarm]; 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

任何幫助將是偉大的。

在此先感謝!

回答

3

當您的應用在後臺暫停時,您將不會收到任何通知,當它終止時。

iOS會爲您的應用程序進度發送一個kill -9信號,並且您的應用程序剛剛被殺死,這與用戶從快速啓動托盤中殺死您的應用程序時發生的情況相同。

Apple documentation

即使你使用的是iOS SDK 4及更高版本,你仍然必須 準備爲您的應用程序沒有任何通知被殺害開發你的應用程序。 用戶可以使用多任務界面顯式殺死應用程序。另外,如果內存變得受限,系統可能會從內存中刪除應用程序以騰出更多空間。暫停的應用程序不會收到 終止的通知,但如果您的應用程序當前正在後臺 狀態(並且未掛起)中運行,則系統會調用應用程序委託的applicationWillTerminate:方法 。您的應用程序無法通過此方法請求額外的後臺執行時間 。

+0

再怎麼其他應用程序一樣的睡眠,如果ü可以做到這一點? – Swati

+0

不知道,我只知道當系統殺死一個應用程序時,它不會被通知它,並且該進程剛剛停止並且內存被清除。 – rckoenes

+0

applicationWillTerminate的用法是什麼? – Swati

3

我和你自己有同樣的問題。但是,我發現如果我沒有設置fireDate,它就開始工作。

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
//Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground: 

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1 
    if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1){ 
     //Annoy The User - Set a badge 
     [application setApplicationIconBadgeNumber:1]; 

     //Try and alert the user 
     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     notification.alertBody = @"Tracking disabled. Tap to resume."; 
     notification.soundName = UILocalNotificationDefaultSoundName; 
     [application scheduleLocalNotification:notification]; 
    } 
#endif 
} 

我必須警告你,但它不適用於空的應用程序。它對我的應用程序有效,它在內存中有很多視圖,因此它必須與釋放其內存所花費的時間有關。

W¯¯

+2

是正確的。你可以簡單地在applicationWillTerminate中添加sleep(2): – imcc

10

應用程序可以在「未來」日期和可能被用來通知應用程序被終止了用戶的時間創建一個本地通知。如果他們然後點擊應用程序,他們可以重新啓動你的應用

這是在我的應用程序中使用/需要在info.plist中的藍牙中央(所以它會在後臺運行)。我假設你將配置你的應用程序在你的info.plist的後臺運行。

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    // Schedule an alarm here to warn the user that they have terminated an application and if they want to re-activate it. 

    NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:10]; // set a localnotificaiton for 10 seconds 

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


    // Clear out the old notification before scheduling a new one. 
    if ([oldNotifications count] > 0) 
     [app cancelAllLocalNotifications]; 

    // Create a new notification. 
    UILocalNotification* alarm = [[UILocalNotification alloc] init]; 
    if (alarm) 
    { 
     alarm.fireDate = theDate; 
     alarm.timeZone = [NSTimeZone defaultTimeZone]; 
     alarm.repeatInterval = 0; 
     alarm.soundName = @"sonar"; 
     alarm.alertBody [email protected]"Background uploads are disabled. Tap here to re-activate uploads." ; 

     [app scheduleLocalNotification:alarm]; 
    } 

} 
+0

嗨,我有一個類似的應用程序,我正在使用BLE。但是applicationWillTerminate方法永遠不會被調用。有什麼建議? – Amit

+0

你是如何測試applicationWillTerminate被調用的?你如何退出應用程序? –

+0

爲我工作。謝謝。 – viral

0

從這家按鈕,並刷卡應用程序按兩次殺應用程序總是調用applicationWillTerminate。如果您單擊並在後臺發送應用程序,然後再次按兩次以刷卡並終止應用程序,則可能不會每次都調用applicationWillTerminate

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     notification.userInfo = [NSDictionary dictionaryWithObject:@"killed.app" forKey:@"Killed"]; 
     notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; // Give some time for firing the notification at least 2 seconds. 
     notification.alertBody = @"App is killed."; 
     notification.soundName = UILocalNotificationDefaultSoundName; 
     notification.timeZone = [NSTimeZone systemTimeZone]; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
     sleep(1); // I noticed that adding sleep is required as it makes sure the notification is set before the app exits. 
} 
0

我可以安排在當地appWillTerminate通知,使用下面的代碼:

let localNotification = UILocalNotification.init() 
    localNotification.fireDate = Date.init(timeIntervalSince1970: Date().timeIntervalSince1970 + 1.5) 
    localNotification.timeZone = TimeZone.current 
    localNotification.repeatInterval = NSCalendar.Unit(rawValue: 0) 
    localNotification.alertBody = "Did terminate app" 
    localNotification.soundName = "sonar" 
    UIApplication.shared.scheduleLocalNotification(localNotification) 
    // block main thread 
    DispatchQueue.global().async { 
     sleep(1) 
     DispatchQueue.main.sync { 
      CFRunLoopStop(CFRunLoopGetCurrent()) 
     } 
    } 
    CFRunLoopRun()