iSara
iSara

Reputation: 929

how to set NSTimer for alarm application?

I am new in iPhone application development. Now i am developing alarm application for iPhone. In this application i am selected data from UIDataPicker. Then i applied to NSLocalNotification firedate with in alarm button action. It is working first time. Then second time agin click that button i again also working, but time also same. It is wrongly working.

Here i think i need to us NSTimer. I don't know how to use NSTimer, and also it is working background application also how to set this timer.

following developed code for alarm notification.

        - (void) saveButtonAction:(id)sender {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            Resource *resourceLoader = [[Resource alloc] init];

            NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];
            NSString *musicName;
            NSDate *selectedDateTime = [[NSDate alloc]init];
            if (prefDic && [prefDic objectForKey:@"alarmtime"]) {
                //firstVal_textField.text = [prefDic objectForKey:@"value1"];
                NSLog(@"saravanan %@",[prefDic objectForKey:@"alarmtime"]);
                selectedDateTime = [prefDic objectForKey:@"alarmtime"];
                NSLog(@"saravanan periyasamy %@", selectedDateTime);

                musicName = [NSString stringWithFormat:@"%@%@",[prefDic                objectForKey:@"alarmmusic"],@".wav" ];

            } 
            UILocalNotification *notif = [[cls alloc] init];
            //notif.fireDate = [datePicker date];
            notif.fireDate = selectedDateTime;
            notif.timeZone = [NSTimeZone defaultTimeZone];

            notif.alertBody = @"Alarm";
            notif.alertAction = @"Show me";
            //notif.repeatInterval = 0;
            //notif.soundName = UILocalNotificationDefaultSoundName;
            notif.soundName = musicName;
            notif.applicationIconBadgeNumber = 1;
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"saravanan"
                                                                    forKey:kRemindMeNotificationDataKey];
            notif.userInfo = userDict;
            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            [notif release];
}
}
}

Upvotes: 0

Views: 548

Answers (1)

Prabhjot Singh Gogana
Prabhjot Singh Gogana

Reputation: 1408

The set button is wired up to run a method called scheduleNotification in the view controller which uses the UILocalNotification class to schedule a notification. The code looks as follows:

   (void )scheduleNotification
  {
 [reminderText resignFirstResponder];
 [[ UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@ "UILocalNotification" );
  if (cls != nil)
    {
 UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [datePicker date];
    notif.timeZone = [ NSTimeZone defaultTimeZone];
    notif.alertBody = @ "Did you forget something?" ;
    notif.alertAction = @ "Show me" ;
    notif.soundName = UILocalNotificationDefaultSoundName ;
       notif.applicationIconBadgeNumber = 1 ;
 NSDictionary *userDict = [       NSDictionary dictionaryWithObject:reminderText.text
          forKey:kRemindMeNotificationDataKey];
    notif.userInfo = userDict;
      [[ UIApplication sharedApplication] scheduleLocalNotification:notif];
    [notif release];
}
}

Upvotes: 1

Related Questions