从本地通知启动时,iOS应用程序崩溃

时间:2012-08-29 01:48:40

标签: ios local uilocalnotification

我已为我的应用设置了每日提醒本地通知。当应用程序在后台时,它工作得很好。但是,当应用程序关闭/未运行时,当用户点击通知中的LAUNCH时,应用程序将启动主屏幕,然后返回主屏幕。

我在这里尝试过很多代码解决方案,大多数涉及这些方面:

- (BOOL)application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification) {
        // handle your notification here.
    }
}

但无论我把它放在哪里,应用程序都会启动,但现在变为黑色 这是我的代码,任何人都可以帮我弄清楚这里要做什么? 请不要,我正在使用Dreamweaver / phonegap来构建应用程序。

//
//  assignmentAppDelegate.m
//  assignment
//
//  Created by Noel Chenier on 23 December, 2011.
//  Copyright  2011. All rights reserved.
//

#import "assignmentAppDelegate.h"
#import "PhoneGapViewController.h"

@implementation assignmentAppDelegate

- (id) init
{   
    /** If you need to do any extra app-specific initialization, you can do it here
     *  -jm
     **/
    return [super init];
}

/**
 * This is main kick off after the app inits, the views and Settings are setup here.
 */

- (void)applicationDidFinishLaunchingWithOptions:(UIApplication *)application
{   
{ 
     UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}


    [[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge  |
     UIRemoteNotificationTypeAlert   |
     UIRemoteNotificationTypeSound];

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults =[NSDictionary dictionaryWithObject:@"NO" forKey:@"enableNotifications"];
    [defaults registerDefaults:appDefaults];
    [defaults synchronize];   
    [ super applicationDidFinishLaunching:application ];
}
 - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
}


-(id) getCommandInstance:(NSString*)className
{
    /** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
     *  own app specific protocol to it. -jm
     **/
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enableNotifications"]) { 
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
    NSDate *currentDate = [NSDate date];

    NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
                                                   fromDate:currentDate];

    NSDateComponents *temp = [[NSDateComponents alloc]init];

    [temp setYear:[dateComponents year]];
    [temp setMonth:[dateComponents month]];
    [temp setDay:[dateComponents day]];
    [temp setHour: 9];
    [temp setMinute:00];

    NSDate *fireTime = [calender dateFromComponents:temp];
    [temp release];

    // set up the notifier 
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    localNotification.fireDate = fireTime;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    localNotification.alertBody = @"Don't Forget Your 365 Day Photo Challenge!";
    localNotification.alertAction = @"LAUNCH";

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    localNotification.repeatInterval = NSMinuteCalendarUnit;
    localNotification.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
    [localNotification release];
}
else {[[UIApplication sharedApplication] cancelAllLocalNotifications];}
    return [super getCommandInstance:className];
}


/**
 Called when the webview finishes loading.  This stops the activity view and closes the imageview
 */
- (void)webViewDidFinishLoad:(UIWebView *)theWebView 
{
    return [ super webViewDidFinishLoad:theWebView ];
}

- (void)webViewDidStartLoad:(UIWebView *)theWebView 
{
    return [ super webViewDidStartLoad:theWebView ];
}

/**
 * Fail Loading With Error
 * Error - If the webpage failed to load display an error with the reson.
 */
- (void)webView:(UIWebView *)theWebView didFailLoadWithError:(NSError *)error 
{
    return [ super webView:theWebView didFailLoadWithError:error ];
}

/**
 * Start Loading Request
 * This is where most of the magic happens... We take the request(s) and process the response.
 * From here we can re direct links and other protocalls to different internal methods.
 */
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}


- (BOOL) execute:(InvokedUrlCommand*)command
{
    return [ super execute:command];
}

- (void)dealloc
{
    [ super dealloc ];
}

@end

2 个答案:

答案 0 :(得分:0)

对于本地通知,您需要在以下内容中添加以下内容:

- (void)applicationDidFinishLaunchingWithOptions:(UIApplication *)application
{ 
     UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
}

答案 1 :(得分:-1)

事实证明......

我不确定它为什么会崩溃,但是如果遇到这个问题的其他任何人遇到此问题,通知会在您的构建进行重新启动时启动应用程序....它只是在它构建时似乎不起作用/运行调试或其他状态。

所以当你运行时,运行RELEASE并且它应该可以正常工作!

相关问题