• 本地消息使用(UILocalNotification,闹钟功能)


    //时间换成NSString

    - (void)randomPickerView:(DatePickView*)randomPickerView selectDate:(NSDate*)selectDate

    {

        //07:01

    //    NSLog(@"ssss %@", selectDate);

        NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

        NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

        [calendar setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

        [comps setTimeZone:[NSTimeZonetimeZoneWithName:@"GMT"]];

    NSInteger unitFlags = NSEraCalendarUnit |

    NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit |

    NSWeekCalendarUnit |

    NSWeekdayCalendarUnit |

    NSWeekdayOrdinalCalendarUnit |

    NSQuarterCalendarUnit;

    comps = [calendar components:unitFlags fromDate:selectDate];

    int hour = [comps hour];

    int min = [comps minute];

        NSString *tempTimeStr = [NSString stringWithFormat:@"%02d:%02d", hour, min];

    //    self.timeStr = tempTimeStr;

        NSLog(@"time :%@", tempTimeStr);

        [self.recodeSwitchDic setObject:tempTimeStr forKey:kLotteryBetWarnTimeKey]; 

        [pool release];

    }

    //清除本地通知

    - (void)cancelLocalNotification

    {

        NSArray *localNotifications = [[UIApplicationsharedApplication] scheduledLocalNotifications];

        for(UILocalNotification *notification in localNotifications)

        {

            NSLog(@"old localNotification:%@", [notification fireDate]);

            [[UIApplicationsharedApplication] cancelLocalNotification:notification];

        }

    }

    //设置本地通知

    - (void)setLocalNotificationWithWeek:(NSString*)localWeek withContent:(NSString*)content

    {

        NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

        

        NSMutableDictionary* mutableDic = [[NSUserDefaults standardUserDefaults] objectForKey:kLotteryBetWarnKey];

        if(!mutableDic)

        {

            return;

        }

        NSArray *clockTimeArray = [[mutableDic objectForKey:kLotteryBetWarnTimeKey] componentsSeparatedByString:@":"];

    NSDate *dateNow = [NSDate date];

    NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];

        //    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

        //    [comps setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    NSInteger unitFlags = NSEraCalendarUnit |

    NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit |

    NSWeekCalendarUnit |

    NSWeekdayCalendarUnit |

    NSWeekdayOrdinalCalendarUnit |

    NSQuarterCalendarUnit;

    comps = [calendar components:unitFlags fromDate:dateNow];

    [comps setHour:[[clockTimeArray objectAtIndex:0] intValue]];

    [comps setMinute:[[clockTimeArray objectAtIndex:1] intValue]];

    [comps setSecond:0];

    //------------------------------------------------------------------------

        //    NSString* clockMode = @"一、五";

    Byte weekday = [comps weekday];

    NSArray *array = [localWeek componentsSeparatedByString:@""];

    Byte i = 0;

    Byte j = 0;

    int days = 0;

    inttemp = 0;

    Byte count = [array count];

    Byte clockDays[7];

    NSArray *tempWeekdays = [NSArray arrayWithObjects:@"", @"", @"", @"", @"", @"", @"", nil];

    //查找设定的周期模式

    for (i = 0; i < count; i++) {

    for (j = 0; j < 7; j++) {

    if ([[array objectAtIndex:i] isEqualToString:[tempWeekdays objectAtIndex:j]]) {

    clockDays[i] = j + 1;

    break;

    }

    }

    }

    for (i = 0; i < count; i++) {

        temp = clockDays[i] - weekday;

    days = (temp >= 0 ? temp : temp + 7);

    NSDate *newFireDate = [[calendar dateFromComponents:comps] dateByAddingTimeInterval:3600 * 24 * days];

    UILocalNotification *newNotification = [[UILocalNotificationalloc] init];

    if (newNotification) {

    newNotification.fireDate = newFireDate;

    newNotification.alertBody = content;//设置弹出对话框的消息内容

                if([[mutableDic objectForKey:kLotteryBetWarnSongKey] isEqualToString:@"1"])

                    newNotification.soundName = UILocalNotificationDefaultSoundName;//设置声音

    newNotification.alertAction = @"确定";//设置弹出对话框的按钮

    newNotification.repeatInterval = NSWeekCalendarUnit;//周期为每星期

                newNotification.applicationIconBadgeNumber = 1;//设置BadgeNumber

                

    NSDictionary *userInfo = [NSDictionarydictionaryWithObject:@"1"forKey:@"betLotteryWarnClock"];

    newNotification.userInfo = userInfo;

    [[UIApplicationsharedApplication] scheduleLocalNotification:newNotification];//记录

    }

    NSLog(@"Post new localNotification:%@", [newNotification fireDate]);

    [newNotification release];

    }

        [pool release];

    }

  • 相关阅读:
    CodeSmith注册错误的解决方法
    我是“坚守者”还是"背叛者"?
    拿什么留住你,我的程序员
    去除HTML代码得函数
    页面之间传递参数得几种方法
    nhibernate source code analyzed (abstract classes in nhibernate2.0)
    Web 2.0时代RSS的.Net实现
    Visual Studio.net 2003安装提示重启问题
    开放思路,综合考虑,心胸开阔,做一个合格的项目经理
    了解实际开发中 Hashtable 的特性原理 .NET, JAVA, PHP
  • 原文地址:https://www.cnblogs.com/swallow37/p/2845761.html
Copyright © 2020-2023  润新知