Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

iphone - Local notification on application termination

I am working on an application which will not work if terminated. It has some background tasks. I want to show a local notification if the app is terminated. There are applications which do this which means this is doable. But I am not able to find out a way.

I have tried to set up a local notification in applicationWillTerminate: method of appdelegate as well as added a notification of app termination in my viewcontroller but none of the methods get called when app is actually terminated.

- (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:.
}

Any help would be great.

Thanks in Advance !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Applications can create a local notification at a "future" date and time which could be used to notify the user that the application was terminated. If they then tap the application they can then restart your app.

This is working in my app that uses/requires Bluetooth Central in the info.plist (so it will run in the background). I assume that you will have configured your application to run in the background in your info.plist as well.

- (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];
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...