开发者

How to update the userdefaults every 1 minute to check whether new alarm is added in iPhone?

I am creating an application. When the user picks time from the picker it is saved in userdefaults. When a new alarm is added the existing alarm should not be overrided from the userdefaults.

The userdef开发者_如何学Pythonaults should check in the background every 1 min that whether any of the alarms current time is equal to the userdefaults time.

If it is equal then an alertview should be displayed. If an alarm completes ringing then that should be deleted from userdefaults.


I don't think this is how it should be done on the iPhone.

when your app is in the background it won't do anything and can also be killed at any time. Also on mobile devices (even when you have real background abillities like on android) you should be very prudent using background processes as the are killing your battery life.

I think what you need is local notifications: http://iphonesdkdev.blogspot.com/2010/04/local-push-notification-sample-code-os.html

This will allow you to schedule your alarm without needing any background task.

Edit: to save your notification info you can do something like this.

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]];

This way you can save the first notification and you can easily save multiple notification by increasing the indexOfNotification


I am afraid you are taking the wrong path. I will use local notifications for this purpose. Any way, if you are trying to implement it with user defaults, you have to use an NStimer which fires every minute. But this works only when your application is running unlike the local notification.


Take 3 variables in your .h file and 2 method:

IBOutlet UIDatePicker *dtPickerTime;
NSMutableArray *arrTimePreference;
NSTimer *theTimer;

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

And place below code into your .m file:

- (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];
}

Change as per your requirements.

If you want alarm in your whole app then place the code for time and CheckForUpdate method in your app delegate file. And also one code is remains for delete the value when it rings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜