How to set alarm for selected days in iphone?

梦想的初衷 提交于 2019-12-30 07:06:33

问题


I want to set alarm for selected days of the week.

It may be a weekday (mon, tue, wed, thu, or fri), weekend(sat, sun) or daily.

How can i do this using local notifications?


回答1:


If you have the time which you have to fire notification every day, you should do this

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    for (NSMutableArray * arrDay in self.namaztimes) {
        NSLog(@"Alarm Array: %@", arrDay);
        int count=[arrDay count];
        if(!count){
            continue;
        }

        int day =0;
        int month=0;
        int year=0;
        int hour =0;
        int minutes=0;

        //  NSArray *arrDates=[[NSMutableArray alloc] initWithCapacity:1];
        for ( int i=0;i<3;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            if (i==0) {
                day = [dayTime intValue];    
            }else if(i==1){
                month = [dayTime intValue];                    
            }else if(i==2){
                year = [dayTime intValue];    

            }
        }
        for ( int i=3;i<count;i++) {
            NSString * dayTime=[arrDay objectAtIndex:i ];
            hour = [[dayTime substringToIndex:2] intValue];
            minutes = [[dayTime substringFromIndex:3] intValue];

            [components setDay:day];
            [components setMonth:month];
            [components setYear:year];
            [components setMinute:minutes];
            [components setHour:hour];


            NSDate *myNewDate = [calendar dateFromComponents:components];

            [self scheduleNotificationForDate:myNewDate];

        }
    }

    [components release];
    [calendar release];

then from here it will connect to the main notification firing method
[self scheduleNotificationForDate:myNewDate];

-(void) scheduleNotificationForDate: (NSDate*)date {
    /* Here we cancel all previously scheduled notifications */
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate);

    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Your Notification Text"; //[NSString stringWithFormat:@"%@",date];
    localNotification.alertAction = NSLocalizedString(@"View details", nil);

    /* Here we set notification sound and badge on the app's icon "-1" 
     means that number indicator on the badge will be decreased by one 
     - so there will be no badge on the icon */

    localNotification.repeatInterval = NSDayCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = -1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}



回答2:


I know this question was asked long ago, i am adding this because it will helpful for some one.

If your want to notify for selected days then follow this tutorial. It will helpful.



来源:https://stackoverflow.com/questions/10780517/how-to-set-alarm-for-selected-days-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!