0

開発中のアプリにローカル通知を追加する作業を行っています。2013 年 4 月 30 日ニューヨーク/東部時間の午後 11 時に通知を 1 つだけ設定しています。どうすればいいですか?複数の方法を試しましたが、どれも正しく機能しませんでした。これは私が現在使用しているものです(動作しません):

- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
                            objectForKey:@"setNotify"]]) {
    [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"setNotify"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSString *str = [NSString stringWithFormat:@"2013-04-23T18:22:00"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];
    NSDate *dte = [dateFormat dateFromString:str];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [cal setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];
    UIApplication* app = [UIApplication sharedApplication];
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
                                        init];
    if (notifyAlarm)
    {
        notifyAlarm.fireDate = dte;
        notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
        notifyAlarm.repeatInterval = 0;
        notifyAlarm.soundName = @"trumpet.m4a";
        notifyAlarm.alertBody = @"Message";
        [app scheduleLocalNotification:notifyAlarm];
    }
}
}
4

2 に答える 2

1

私が気づいたことの 1 つは、日付フォーマッターで YYYY を使用するつもりだったのではないかということです。Apple ドキュメントから

A common mistake is to use YYYY. yyyy specifies the calendar year whereas
YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.
In most cases, yyyy and YYYY yield the same number, however they may
be different. Typically you should use the calendar year.

これを変更すると、コードが機能し、通知が投稿されました。

于 2013-04-23T23:05:35.000 に答える