15

終了すると機能しないアプリケーションに取り組んでいます。いくつかのバックグラウンド タスクがあります。アプリが終了した場合にローカル通知を表示したい。これを行うアプリケーションがあり、これは実行可能であることを意味します。しかし、私は方法を見つけることができません。

appdelegate の applicationWillTerminate: メソッドでローカル通知を設定しようとしましたが、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:.
}

どんな助けでも素晴らしいでしょう。

前もって感謝します !!!

4

5 に答える 5

14

アプリケーションは、アプリケーションが終了したことをユーザーに通知するために使用できる「将来の」日時にローカル通知を作成できます。次にアプリケーションをタップすると、アプリを再起動できます。

これは、info.plist で Bluetooth Central を使用/必要とするアプリで機能しています (したがって、バックグラウンドで実行されます)。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 =@"Background uploads are disabled. Tap here to re-activate uploads." ;

        [app scheduleLocalNotification:alarm];
    }

}
于 2014-02-11T22:06:55.627 に答える
6

アプリがバックグラウンドで中断されている場合は、終了しても通知を受け取りません。

iOS はアプリの進行状況に対して kill -9 シグナルを送信し、アプリは強制終了されます。これは、ユーザーがクイック起動トレイからアプリを強制終了した場合と同じです。

Appleのドキュメントから:

iOS SDK 4 以降を使用してアプリを開発したとしても、通知なしでアプリが強制終了されることに備えておく必要があります。ユーザーは、マルチタスク UI を使用してアプリを明示的に強制終了できます。さらに、メモリが制限されると、システムはメモリからアプリを削除して空き容量を増やす場合があります。中断されたアプリには終了が通知されませんが、アプリが現在バックグラウンド状態で実行されている (中断されていない) 場合、システムはアプリ デリゲートの applicationWillTerminate: メソッドを呼び出します。アプリは、このメソッドから追加のバックグラウンド実行時間を要求できません。

于 2013-08-01T08:02:35.967 に答える