0

に次のコードがありますViewController.m

-(void) checker {
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    [notification setAlertBody: @"Im your local notification"];
    [notification setFireDate: [NSDate dateWithTimeIntervalSinceNow: 1]];
    [notification setTimeZone: [NSTimeZone defaultTimeZone]];
    [UIApplication setScheduledLocalNotifications: [NSArray arrayWithObject: notification]];
}

最後の行で警告が生成されます。

クラス メソッド '+setScheduledLocalNotifications' が見つかりません (戻り型のデフォルトは id です)

処理中にエラーが発生します。通知をインスタンス化するにはどうすればよいですか? 私が言ったように、私は新しいので、完全な回答を提供していただければ幸いです。

編集: 60 秒ごとに繰り返され、通知を送信する関数を呼び出すタイマーがあります。

timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(checker) userInfo:nil repeats:YES];

/* ... */
-(void)checker {
    NSLog(@"Notification routine");
    UIApplication* app = [UIApplication sharedApplication];
    NSArray *oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.(if needed)
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
    if (alarm) {
        alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.alertBody = @"Msg to show";
        [app scheduleLocalNotification:alarm];
    }
}

ログは 1 分間に 1 回しか発火していませんが、oldNotificationsカウントは増加しません。

4

2 に答える 2

2
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

エラー メッセージの「+」は、インスタンス メソッドではなくクラス メソッドを呼び出そうとしていることを意味します。この[UIApplication sharedApplication]呼び出しは、アプリケーション オブジェクトのシングルトン インスタンスを返します。これにより、そのメソッドをインスタンス メソッドとして呼び出すことができます。

また、 UIApplicationのドキュメントを読む必要があります。通知オブジェクトを呼び出しscheduleLocalNotification:てスケジュールを設定するだけです。これにより、スケジュール メソッドがオブジェクトをコピーするため、メモリをオブジェクトに解放できます。

于 2013-02-06T20:48:02.927 に答える
1

これが私のやり方です。

UIApplication* app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.(if needed)
if ([oldNotifications count] > 0)
    [app cancelAllLocalNotifications];

// Create a new notification.
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
if (alarm)
{
    alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60];//afterone hour
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    alarm.repeatInterval = 0;
    alarm.soundName = @"alarmsound.caf";
    alarm.alertBody = @"Msg to show";
    [app scheduleLocalNotification:alarm];
}

アップデート:

- (void)scheduleAlarmForDate:(NSDate*)theDate andBody:(NSString*)Body
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray *oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];//Using ARC no autorelease
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"alarmsound.caf";
        alarm.alertBody = Body;
        [app scheduleLocalNotification:alarm];
    }
}

このメソッドを 2 回呼び出すと、

 NSDate *fireDate1 = [NSDate dateWithTimeIntervalSinceNow:4.0];
[self scheduleAlarmForDate:fireDate1 andBody:@"My alertBody 1"];

NSDate *fireDate2 = [NSDate dateWithTimeIntervalSinceNow:6.0];
[self scheduleAlarmForDate:fireDate2 andBody:@"My alertBody 2"];

最初のアラームはキャンセルされます,,

于 2013-02-06T20:48:48.607 に答える