So guys, i've been trying literally for the last week to get this to work.
I have a app that i need a UISwitch to turn on a local notification.
Standard for the switchs state is off, but i need to change this, on load, when the alarm is on.
I've tried scheduledLocalNotification, i've tried a BOOL and i've tried to set int isOn, to 1 when it's on and 0 when it's off, nothing seems to be working for me.
The if loop i use is in ViewDidLoad and looks like this:
if (isAlarmOn == 1) {
NSLog(@"You have notifications");
[setAlarm setOn:YES];
}
else{
NSLog(@"You have no notifications");
[setAlarm setOn:NO];
}
No matter which strategy i try to use, i doesn't seem to work, hope i can get some help from your guys.
Some more code:
The code for my setAlarm
- (IBAction)setAlarm:(id)sender {
NSArray *notificationArray=[[UIApplication sharedApplication] scheduledLocalNotifications];
if (setAlarm.on) {
[alarmStatus setText:@"Alarmen er tændt"];
//Metode til at undersøge om klokken før eller efter officiel solned
NSTimeInterval checkTime = [[astronomicalCalendar sunset] timeIntervalSinceDate:[NSDate date]];
NSLog(@"Alarmen er tændt");
if (checkTime >= 0) {
[self scheduleNotificationToday];
}
else{
[self scheduleNotificationTomorrow];
}
NSLog(@"antal alamer %@", notificationArray);
isAlarmOn = 1;
}
else {
//Metode til at slette alle notifikationer
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"Alarmen er slukket");
NSLog(@"antal alamer %@", notificationArray);
isAlarmOn = 0;
}
}
My UILocalNotification code
- (void)scheduleNotificationToday {
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
//Just some stuff determine which time to alert on.
UILocalNotification *notif = [[cls
alloc] init];
notif.fireDate = [gregorian dateByAddingComponents:traekFraDag toDate:[astronomicalCalendar sunset] options:0];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Nu går solen snart ned";
notif.alertAction = @"Show me";
notif.soundName = @"Waterline.caf";
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
Please tell me if there is any other part of the code you would like to see
but every time the app is unloaded from the memory the uiswitch shows the default state (off)
You are not storing the state of your switch in any persistant store. That is why it always shows as off if the app exits (dealloc is called for your class).
If you want to save the switch state even when the app is not in memory, then you'll need to save that value somewhere. You can use NSUserDefaults
for this if you want.