首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何每1分钟更新一次userdefault,查看iPhone中是否新增告警?

如何每1分钟更新一次userdefault,查看iPhone中是否新增告警?
EN

Stack Overflow用户
提问于 2011-08-30 13:38:29
回答 3查看 276关注 0票数 1

我正在创建一个应用程序。当用户从选取器中选择时间时,它被保存在userdefault中。添加新报警时,不应覆盖用户默认设置中的现有报警。

用户默认应每隔1分钟在后台检查是否有任何报警当前时间等于用户默认时间。

如果相等,则应显示警报视图。如果报警完成振铃,则应将其从用户默认设置中删除。

EN

回答 3

Stack Overflow用户

发布于 2011-08-30 14:27:32

我不认为这是在iPhone上应该做的事情。

当你的应用程序在后台时,它不会做任何事情,也可能在任何时候被杀死。同样,在移动设备上(即使你有真正的背景能力,比如在android上),你应该非常谨慎地使用后台进程,因为它们会扼杀你的电池寿命。

我认为你需要的是本地通知:http://iphonesdkdev.blogspot.com/2010/04/local-push-notification-sample-code-os.html

这将允许您在不需要任何后台任务的情况下安排您的闹钟。

编辑:为了保存你的通知信息,你可以这样做。

代码语言:javascript
复制
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

int indexOfNotification = indexOfNotification; 

// saving the notification message
[prefs setObject:@"notification message" forKey:[NSString stringWithFormat:@"notificationmessage%i", indexOfNotification]];

// saving the date
[prefs setInteger:[date timeIntervalSince1970] forKey:[NSString stringWithFormat:@"notificationtimestamp%i", indexOfNotification]];

这样,您可以保存第一个通知,并且可以通过增加indexOfNotification轻松保存多个通知

票数 3
EN

Stack Overflow用户

发布于 2011-08-30 14:33:19

恐怕你走错路了。为此,我将使用本地通知。无论如何,如果您试图使用用户默认设置来实现它,则必须使用每分钟触发一次的NStimer。但这只在您的应用程序运行时才起作用,而不像本地通知。

票数 0
EN

Stack Overflow用户

发布于 2011-08-31 12:37:18

取.h文件中的3个变量和2个方法:

代码语言:javascript
复制
IBOutlet UIDatePicker *dtPickerTime;
NSMutableArray *arrTimePreference;
NSTimer *theTimer;

- (IBAction)btnStoredInPreferencesPressed:(id)sender;
- (void)CheckForUpdate;

并将以下代码放入.m文件中:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    arrTimePreference = [[NSMutableArray alloc] init];

    theTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(CheckForUpdate) userInfo:nil repeats:YES];
}

#pragma mark -
#pragma mark Private Methods

- (void)CheckForUpdate
{
    [theTimer invalidate];
    theTimer = nil;

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arrMyTimeCheck = [[NSMutableArray alloc] init];
    arrMyTimeCheck = [prefs objectForKey:@"AllValues"];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"hh:mm"];
    NSString *strCurTime = [dateFormat stringFromDate:[NSDate date]];

    for( int j = 0 ; j < [arrMyTimeCheck count] ; j++)
    {
        if([strCurTime isEqualToString:[arrMyTimeCheck objectAtIndex:j]])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test"
                                                            message:@"Alarm"
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
        }
    }
    theTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(CheckForUpdate) userInfo:nil repeats:YES];
}

#pragma mark -
#pragma mark IBAction Methods

- (IBAction)btnStoredInPreferencesPressed:(id)sender
{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"HH:mm"];
    NSString *strTemp = [dateFormat stringFromDate:[dtPickerTime date]];

    NSMutableArray *newArray = [[NSMutableArray alloc] init];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    newArray = [prefs objectForKey:@"AllValues"];

    if(newArray == nil)
    {
        newArray = [[NSMutableArray alloc] init];
        [newArray addObject:strTemp];
    }
    else
    {
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        for(int i = 0 ; i < [newArray count] ; i++)
        {
            [tempArray addObject:[newArray objectAtIndex:i]];
        }
        [tempArray addObject:strTemp];
        newArray = [[NSMutableArray alloc] init];
        newArray = tempArray;
    }

    [prefs setObject:newArray forKey:@"AllValues"];
    [prefs synchronize];

    //The Commented Code is Just For Varification That it is Stored Or Not
    //NSMutableArray *testArray = [[NSMutableArray alloc] init];

    //testArray = [prefs objectForKey:@"AllValues"];

    //NSLog(@"Total Count is := '%d'",[testArray count]);

    //for(int i = 0 ; i < [testArray count] ; i++)
    //{
    //    NSLog(@"Values Are :- '%@'",[testArray objectAtIndex:i]);
    //}

    [self CheckForUpdate];
}

根据您的需求进行更改。

如果你想在你的整个应用程序中报警,那么将时间和CheckForUpdate方法的代码放在你的应用程序委托文件中。还有一个代码是用于在振铃时删除该值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7239095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档