Skip to content

Instantly share code, notes, and snippets.

@yurfuwa-chan
Created August 15, 2012 12:16
Show Gist options
  • Save yurfuwa-chan/3359646 to your computer and use it in GitHub Desktop.
Save yurfuwa-chan/3359646 to your computer and use it in GitHub Desktop.
LocalNotificationへの登録
- (void)viewDidLoad{
[super viewDidLoad];
//全notificationの解除
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//notificationを登録
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:[self getDiffFromTodayWithFormat:@"201208152113"]];
[self registNotification:date message:@"時間になったよ"];
}
//dateオブジェクトからnotificationの登録
-(void)registNotification:(NSDate *)date message:(NSString *)message{
UILocalNotification *localPush = [[UILocalNotification alloc] init];
localPush.timeZone = [NSTimeZone defaultTimeZone];
localPush.fireDate = date;
localPush.alertBody = message;
localPush.alertAction = @"アプリを起動";
[[UIApplication sharedApplication] scheduleLocalNotification:localPush];
}
//@"yyyyMMddHHmm"の形式で渡された日付と今の日付の差分を返す
-(float)getDiffFromTodayWithFormat:(NSString *)format{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyyMMddHHmm";
return [[df dateFromString:format] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment