簡體   English   中英

從本地通知啟動時,iOS App崩潰

[英]iOS App crashes when launched from local notification

我已經為我的應用設置了每日提醒本地通知。 該應用程序在后台運行時效果很好。 但是,當應用程序關閉/未運行時,當用戶在通知中點擊“啟動”時,應用程序將啟動主屏幕,然后返回主屏幕。

我在這里嘗試了許多代碼解決方案,其中大多數涉及以下方面:

- (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

對於本地通知,您需要在中添加以下內容:

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

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

事實證明...

我不確定為什么會崩潰,但是如果其他任何人遇到此問題,則通知將在您的構建版本發布時啟動該應用程序。。。調試或其他狀態。

因此,當您運行時,請運行RELEASE,它應該可以正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM