Created
August 15, 2012 12:16
-
-
Save yurfuwa-chan/3359646 to your computer and use it in GitHub Desktop.
LocalNotificationへの登録
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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