本地通知问题在ipad和iphone上发布

时间:2015-06-15 02:22:11

标签: ios objective-c iphone ipad

我的应用的本地通知存在问题。当我测试我的应用程序的通知,在iPhone上它每天发射一次,但每天在ipad 5x时间。这是我的代码:

NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];

NSInteger day = [comp day];
NSInteger month = [comp month];
NSInteger year = [comp year];
NSInteger hour = [comp hour];
NSInteger min = [comp minute];
NSInteger sec = [comp second];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: hour];
[components setMinute: min];
[components setSecond: sec];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToFire;
int i = arc4random() % 2;
if (i == 1) {
    localNotification.alertBody = @"Collect your FREE coins!";
} else {
    localNotification.alertBody = @"It's time to win";
}
localNotification.alertAction = @"View";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setRepeatInterval: kCFCalendarUnitDay];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"local notif = %@", localNotification);

这段代码中我的错误是什么?为什么它似乎正确地在iPhone上发出通知而不是在iPad上发布通知?

1 个答案:

答案 0 :(得分:0)

如果您的iPad操作系统版本> 8.0然后你必须得到用户的许可。在" didFinishLaunchingWithOptions"中写下代码。 (AppDelegate.m)

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}