本地通知错误iOS

本地通知错误iOS,ios,notifications,uilocalnotification,Ios,Notifications,Uilocalnotification,当最小化应用程序时,一切都很好,但是当我尝试部署正在进行的繁荣时,这个错误 *由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:'*-[\uu NSArrayM insertObject:atIndex:]:对象不能为零' -(void) applicationDidEnterBackground:(UIApplication *)application { NSDate *alertTime = [[NSDate date] dateB

当最小化应用程序时,一切都很好,但是当我尝试部署正在进行的繁荣时,这个错误 *由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:'*-[\uu NSArrayM insertObject:atIndex:]:对象不能为零'

-(void) applicationDidEnterBackground:(UIApplication *)application
{
        NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:5];
        UIApplication* app = [[UIApplication sharedApplication] init];
        UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kAllNews]];

        NSError *error = nil;

        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

        NSError *jsonParsingError = nil;

        sortArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

        if (notifyAlarm)
        {

        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"news"])
        {
            newsNew = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"news"]];
        }

        if (newsNew.count > sortArray.count) {

        notifyAlarm.fireDate = alertTime;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.repeatInterval = 0;
        notifyAlarm.soundName = @"Glass.aiff";
        notifyAlarm.alertAction = @"Gipoteza";
        notifyAlarm.alertBody = @"Добавлена новая новость";
        [app scheduleLocalNotification:notifyAlarm];
    }
  }
}

有几点建议:

第一,;您是否尝试将代码放在ApplicationWillResignative中

为什么您在代码中获得对UIApplication的引用?您已经将其作为方法的参数

尝试在代码中放置断点,以查看发生此错误的确切位置。我猜它发生在最后一行:[app scheduleLocalNotification:notifyAlarm],它发生的原因是您的notifyAlarm对象出于某种原因为nil。使用调试器逐步检查代码,查看是否确实设置了该对象

还有,;请注意,您的代码从停用到挂起的最长时间为5秒。暂停时;没有代码可以运行。例如,如果您的NSURLConnection请求由于某种原因需要很长时间才能响应,则在代码完成之前,您的应用程序将被挂起