2011-12-22 2 views
0

나는 맞춤형 알람 앱인 앱을 만들었습니다. UILocalNotification은 내가 선택한 시간에 UIDatePicker에 호출해야하지만 정확한 타이밍에 호출하지는 않습니다. 예를 들어, 알람을 위해 오후 2 시로 시간을 선택 했으므로 알림은 오후 2시에서 2시 01 분 사이에 호출됩니다. 그러나 확실하지는 않습니다 ... 그것은 임의의 시간 지연을 제공합니다. 내 UITableView에서 그것은 당신이 볼 수있는 설명이 잘못된 것입니다 표시됩니다. 나는 인도에서 왔으므로 GMT 타이밍을 보여 주지만 수정 될 수 있습니까?UILocalNotification의 인보 케이션 지연

내 전체 코드는 다음과 같습니다. - ----------------------------- AppDelegate.m 파일 : ---- --------------------------

@synthesize window,viewController,timeViewController; 
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey"; 
#pragma mark - 
#pragma mark === Application Delegate Methods === 
#pragma mark - 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"Mayank"]; 
    if(x == 1) 
    { 
     timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil]; 
     timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = timeViewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     timeViewController.view.frame = frame; 
     [self.window addSubview:timeViewController.view]; 
    } 
    else 
    { 
     [[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"Mayank"]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to set the Default Alarm?" message:@"at 4:20 PM" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil]; 
     [alert show]; 
     [alert release]; 
    } 
    sleep(1); 
    [self.window makeKeyAndVisible]; 
    application.applicationIconBadgeNumber = 0; 
    // Handle launching from a notification 
    UILocalNotification *localNotification = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (localNotification) { 
     NSString *reminderText = [localNotification.userInfo 
            objectForKey:kRemindMeNotificationDataKey]; 
     [viewController showReminder:reminderText]; 
    } 
    return YES; 
} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex == 0) 
    { 
     timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil]; 
     timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = timeViewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     timeViewController.view.frame = frame; 
     [self.window addSubview:timeViewController.view]; 
    } 
    if(buttonIndex == 1) 
    { 
     viewController = [[SetAlarmViewController alloc]initWithNibName:@"SetAlarmViewController" bundle:nil]; 
     viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
     CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame; 
     CGRect frame = viewController.view.frame; 
     frame.origin = CGPointMake(frame.origin.x,frame.origin.y + statusBarFrame.size.height); 
     viewController.view.frame = frame; 
     [self.window addSubview:viewController.view];  
    } 
} 
- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification { 
    NSString *reminderText = [notification.userInfo 
           objectForKey:kRemindMeNotificationDataKey]; 
    [viewController showReminder:reminderText]; 
    application.applicationIconBadgeNumber = 0; 
} 

----------------- ------------ mainViewController.m 파일 : ------------------------------

@implementation SetAlarmViewController 
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType; 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

    appDelegate = (The420DudeAppDelegate *)[[UIApplication sharedApplication] delegate];  
    eventText.returnKeyType = UIReturnKeyDone; 

// datePicker.minimumDate = [NSDate date]; 
    NSDate *now = [NSDate date]; 
    [datePicker setDate:now animated:YES]; 
    eventText.delegate = self; 
    index = 0; 
} 

-(void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:YES]; 
    [self.tableview reloadData]; 
} 

- (IBAction) scheduleAlarm:(id) sender { 
    [eventText resignFirstResponder]; 

// Get the current date 
    NSDate *pickerDate = [self.datePicker date]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = pickerDate; 
// NSLog(@"%@",localNotif.fireDate); 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
// NSLog(@"%@",localNotif.timeZone); 

    // Notification details 
    localNotif.alertBody = [eventText text]; 

    // Set the action button 
    localNotif.alertAction = @"Show me"; 
    localNotif.repeatInterval = NSDayCalendarUnit; 
    localNotif.soundName = @"jet.wav"; 
    // Specify custom data for the notification 
    NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text 
                 forKey:kRemindMeNotificationDataKey]; 
    localNotif.userInfo = userDict; 

    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release]; 

    [self.tableview reloadData]; 
    eventText.text = @""; 

    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil]; 
    [self presentModalViewController:viewController animated:YES]; 
} 

#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [[[UIApplication sharedApplication] scheduledLocalNotifications] count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    index = indexPath.row; 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!!!" 
                 message:@"Are you sure you want to Delete???" delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles:@"Ok",nil]; 
    [alertView show]; 
    [alertView release]; 

} 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    UILocalNotification *notify = [notificationArray objectAtIndex:index]; 

    if(buttonIndex == 0) 
    { 
     // Do Nothing on Tapping Cancel... 
    } 
    if(buttonIndex ==1) 
    { 
     if(notify) 
      [[UIApplication sharedApplication] cancelLocalNotification:notify]; 
    } 
    [self.tableview reloadData]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Configure the cell... 

    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row]; 

    [cell.textLabel setText:notif.alertBody]; 
    [cell.detailTextLabel setText:[notif.fireDate description]];  
    return cell; 
} 


- (void)showReminder:(NSString *)text { 
    /* 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" 
    message:@"hello" delegate:self 
    cancelButtonTitle:@"OK" 
    otherButtonTitles:nil]; 
    [alertView show]; 
    [self.tableview reloadData]; 
    [alertView release]; 
    */ 

    NSString *path = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; 
    player.numberOfLoops = -1; 
    [player play]; 

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil]; 
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    [actionSheet showInView:self.view]; 
// [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]]; 
    [actionSheet release]; 

} 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    [player stop]; 
    if(buttonIndex == 0) 
    { 
     NSLog(@"OK Tapped"); 
    } 
    if(buttonIndex == 1) 
    { 
     NSLog(@"Cancel Tapped"); 
    } 
} 

이 그림은 내 앱보기 표시 :

Alarm App View

답변

0

여기 문제는 NSDatePicker가 선택한 시간과 함께 현재 시간의 초를 반환하는 것과 관련이 있다고 생각합니다. NSDatePicker가 반환 한 날짜부터 초를 제거한 다음 알람 및 로컬 알림에 사용해야합니다.

NSDate *selectedDate = [datePicker date]; // you don't need to alloc-init the variable first 
NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *dc = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate]; 
selectedDate = [cal dateFromComponents:dc]; 

// 이제 당신은 알림에 훨씬 더 정확성을 받아야 알람 에 대해 0 초와 함께있는 NSDate을 가지고 있지만, 나는 그들이이 두 번째 분할에 정확히 보장하고 생각하지 않습니다.

0

명심하십시오. datepicker의 날짜는 GMT와 일치합니다. 혼자서 자신의 시간대로 변환해야합니다. 이것은 귀하의 경우에 문제가 될 수 있습니다.

+0

나는 .... 그 문제를 해결하는 방법을 알고 싶습니까 ??? 그건 그렇고 내 주요 질문이 아니야. – mAc

+0

그것은 단지 팁이었습니다. – samfisher

1
NSDate *pickerDate = [self.pickerTime date]; 
// Break the date up into components 
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:pickerDate]; 
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:pickerDate]; 
// Set up the fire time 
NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
[dateComps setDay:[dateComponents day]]; 
[dateComps setMonth:[dateComponents month]]; 
[dateComps setYear:[dateComponents year]]; 
[dateComps setHour:[timeComponents hour]]; 
[dateComps setMinute:[timeComponents minute]]; 
**[dateComps setSecond:00];** 
NSDate *itemDate = [calendar dateFromComponents:dateComps]; 
localNotification.fireDate = itemDate; 
관련 문제