How to set an Alarm in iOS?

后端 未结 4 1618
长发绾君心
长发绾君心 2020-12-06 03:04

I know this question has asked many times on StackOverflow but i couldn\'t able to set alarm in my app because i am very new to iOS? I am following this tutorial to

相关标签:
4条回答
  • 2020-12-06 03:46
    1. First on your xib, (or code) set the date picker mode: Time (Default is date & time)

    2. The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.

      UILocalNotification *localNotif = [[UILocalNotification alloc] init];
      [localNotif setFireDate:datePicker.date];
      [localNotif setRepeatInterval:NSDayCalendarUnit];
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
      

    PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:

    Local notifications in iOS.

    tutorial you posted on how to do this.

    0 讨论(0)
  • 2020-12-06 03:50
    + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
    

    Call this method with parameters and use this

     + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    
    //set the notification date/time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:day];
    
    [dateComps setMonth:month];
    
    [dateComps setYear:year];
    [dateComps setHour:hours];
    
    [dateComps setMinute:minutes];
    [dateComps setSecond:seconds];
    
    NSDate *notificationDate = [calendar dateFromComponents:dateComps];
    [dateComps release];
    
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = notificationDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    
    // Set notification message
    localNotif.alertBody = alertBody;
    // Title for the action button
    localNotif.alertAction = actionButtonTitle;
    
    localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
    
    //use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
    
    localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
    
    // Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
    localNotif.userInfo = infoDict;
    
    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
    }
    
    0 讨论(0)
  • 2020-12-06 03:51

    You can try this

    UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
    [todolistLocalNotification setFireDate:[lodatepicker date]];
    [todolistLocalNotification setAlertAction:@"Note list"];
    [todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [todolistLocalNotification setAlertBody:text_todolist];
    [todolistLocalNotification setHasAction:YES];
    
    0 讨论(0)
  • 2020-12-06 03:55

    You may need to change the style of the date picker to allow changing the time in addition to just the date.

    0 讨论(0)
提交回复
热议问题