How can I identify UILocalNotification and snooz it again? - iphone

I am developing an alarm app and having some issues and snooz and repeatation of alarms, I am making a Class having name, time and other alarm options below
int alarm_id;
NSString *nameOfAlarm;
NSString *timeOfAlarm;
NSString *repeatAlarm;
NSString *soundOfAlarm;
NSString *snoozOfAlarm;
NSString *soundFadeInOfAlarm;
NSString *volumeOfAlarm;
NSString *vibrationOfAlarm;
Now I am saving above values to my Sqlite database, and at same time I am setting notification to iOS, when notification fires the, then I want to snooz and also want same alarm on different days,
I don't want exact code, but a concept or view how to do that ?
Help .

You can use UILocalNotification's userInfo to store a unique identifier for each notification.
Take a look at the UILocalNotification documentation.

Related

iOS7 device get restarted when using 'saveEvent:span:commit:error:' method

I am developing an application which copies appointment records from my app to devices native calendar. I am using the following code to do that.
NSString *eventIde = nil;
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKCalendar *calendarDef = [eventStore defaultCalendarForNewEvents];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
[event setCalendar:calendarDef];
//set values to this event. like title, notes, startDate, endDate, location
NSError *err1 = nil;
BOOL isStoredd = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err1];
if(isStoredd){
eventIde = [NSString stringWithString:event.eventIdentifier];
}
Here I am creating EKEvents for each appointment in my app, sets appropreate values, and saves the event to event store. This is done simultaniously for about 200 records.It was working fine in iOS6, when i updated the ipad to iOS7 it causes the device to restart. I tried the same with record count 50, then also same issue occured.
Sometimes it shows an over memorry problem, sometimes shows 'Terminating in response to SpringBoard's termination'. I need the eventIdentifier to enter into my database for further use. But everytime this issue occures and device restarts.
Can anybody help me with this problem? What is special with iOS7 eventstore?
Or please advice any solution to overcome this issue.
Thanks
The first section of the Calendar and Reminders programming guide on Apple.com has your answer.
You do not allocate the store multiple times unless you have more than one Calendar, like FB events.
I highly recommend Apple's guides, they're easier than 3rd party books oftentimes.

How do you extract part of an NSUserDefault to put into a UITableView, UIButton text or even a UILabel?

I have cached the contents of a url using the following method and have saved the text as an NSUserDefault as I need to access it in another UIViewController.
This is the code that I am using to download the contents of a URL and save as an NSUserDefault and this is the code that I am using to load the contents of that NSUserDefault into a UITableView.
//Downloading URLcontents and save to an NSUser Default
NSString *url = [[NSString stringWithFormat:#"http://www.pharmacon.site50.net/database/1.xml"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:#"one"];
//Loading an NSUserDefault and placing it in a UITextView
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:#"one"];
textView.text=myTxtFile;
Here is what a typical XML file looks like that I have downloaded;
<item>
<din>1</din>
<category>Drugs</category>
<name>Paracetamol</name>
<phonetic>Pa-ra-ce-ta-mol</phonetic>
<spoken>Paracetamol.mp3</spoken>
<description>
Paracetamol is a drug commonly used for headaches and minor aches and pains.
</description>
<purpose>
Paracetamol is best used for minor to moderate aches and pains and can also be used to treat mild headaches.
</purpose>
<author>Sam Vale</author>
<editor>Harrison Fuller</editor>
</item>
Now, what I want to be able to do is read this NSUserDefault but actually extract just the text in the name region (so in this case, "Paracetamol) and then put that into the text of a UITableView cell, as the text of a button or a UILabel (choose whichever you reckon would be best or what you know).The reason I want to do this is cause I am going to display results that I download off the database in a UITableView.
Any help would be greatly appreciated.
You'll need NSXMLParser for this. There's plenty of examples on how to use it to parse XML available on the internet.

Is there any way to add identifier or tag custom event added in iCal?

I am setting a reminder in my app. I have added a custom event using EKEvent to iCal. Now when I retrieve events from iCal I get all the events present on that day. Is there any way to get/retrieve events added through my app only, I tried eventIdentifier property of EKEvent but it is a readonly property.
Can anybody help???
You could loop through all of the calendar events that match a specific date but that is not the preferred method. Each event is created with a unique eventIdentifier property. When you save the event you can copy the eventIdentifier and next time you want to modify that specific event you can use the EKEventStore eventWithIdentifier Method to load your Event.
A sample might look like this
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSError *err;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
//modify all the event properties you would like then save
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
self.calendarEventID = event.eventIdentifier;
[eventStore release];
Later if you want to retrieve the saved event from the previous code you could do the following
//self.calendarEventID is a NSString property declared in the .h file and synthesized in .m
EKEvent *myEvent = [eventStore eventWithIdentifier:self.calendarEventID];
Kludge:
I had a similar problem with an AppleScript I made for setting iCal alarms; I wanted to be able to identify and delete the events my script had made on the next pass.
I couldn't find any tag-like properties for iCal events, so I ended up using the location property, which is a string; I set it to " " and searched for that. (Caveat: The alarm message includes the location at the end, surrounded by parens, so this glops things up a bit.)
If you need the location property for other purposes in your app, you still might be able to add some identifying character sequence. Or maybe you can use some other property you don't otherwise need.

Detecting changed user defaults

I have an iPhone app and have implemented local notifications. The app has a page in Settings that allows a user to schedule notifications a specified number of days in advance of an event. To make sure that changes to the settings take effect every time a user activates the app, I have the following in my app delegate:
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] synchronize];
[self rescheduleAllNotificationsWithUserNotification:NO];
}
The problem is that the call to rescheduleAllNotificationsWithUserNotification: takes a few seconds and the app feels a little sluggish on start.
I only need to call rescheduleAllNotificationsWithUserNotification: if any of the settings have been changed. Is there a way to detect if the user has changed any of the settings between app activations so I can avoid always rescheduling the notifications?
I think you might be looking for the NSUserDefaultsDidChangeNotification notification.
You can register to listen to this notification and be informed whenever the user's preferences change.
You should use a way to put this method call in another thread is possible. performSelectorInBackground is an easy way to do it:
[self performSelectorInBackground:#selector(rescheduleAllNotificationsWithUserNotification:) withObject:NO];
That should help you get rid of the laggy performance. You could even use ^blocks, as you seem to be on iOS 4.
What I have done in the past, if there are not too many preference values to monitor, is to have alternate versions, sort of "the last time I ran" versions. One set of values is accessible through the Settings application but the other is only set from within the application.
NSString *name = (NSString *) CFPreferencesCopyAppValue( (CFStringRef) #"name_preference", kCFPreferencesCurrentApplication );
NSString *nameLastTime = (NSString *) CFPreferencesCopyAppValue( (CFStringRef) #"name_last_execution", kCFPreferencesCurrentApplication );
// Need obvious checks for empty or missing entries, etc., etc.
if( ![nameLastTime isEqual: name] ) {
// Store the new name as the name at last execution...
CFPreferencesSetAppValue( (CFStringRef) #"name_last_execution", (CFStringRef) name, kCFPreferencesCurrentApplication );
CFPreferencesAppSynchronize( kCFPreferencesCurrentApplication );
[self rescheduleAllNotificationsWithUserNotification:NO];
}
It is not real elegant with fancy object-oriented call-back methods, etc., but it gets the job done cheaply and reliably.

localization in iOS notifications?

I have my app running nicely and it uses local notifications.
I have decided to internationalize the app now and have everything working just fine except notifications which were set on a language before changing the language on the device.
I populate the messages in the notification from an array which contains localized strings, so I figured that when the user changes the language of the device the string in the notification would also change but I was wrong.
How best to tackle this issue? Should my NSString text also be NSLocalizationString ?
My notification code:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [alertTimes objectAtIndex:i];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSString *text = [alertText objectAtIndex:i];
// Notification details
localNotif.alertBody = text;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Should my NSString text also be NSLocalizationString?
Yes, I would do that.
Replace [alertTimes objectAtIndex:i] with NSLocalizedString(#"alertTimes",[alertTimes objectAtIndex:i]). I am assuming that you storing strings in the alertTimes array that match your localized string.
Always use localizedUserNotificationString(forKey:arguments:) when localizing local notifications.
The "gotcha" with localization is that the static string from NSLocalizedString would not work well because there is the possibility where the user might switch a language after notification had been scheduled. It would result in the wrong language get displayed in a notification alert.
For example, a notification is scheduled in en-US with English copy string been set, before the notification gets triggered the user switched the language to jp since a static string was generated using NSLocalizedString there is no way it can be changed before notification get triggered.
localizedUserNotificationString(forKey: arguments:) should be used to localize string text for a notification content, https://developer.apple.com/documentation/foundation/nsstring/1649585-localizedusernotificationstring. Where a localized string value is created dynamically from a localized string resource when the notification is about to be displayed.
Just came across the same issue and found that
Apple Docs for UNMutableNotificationContent say to use the NSString. localizedUserNotificationString(forKey:arguments:) function, which defers loading the localized string till the notification shows up. That way the string will be properly localized even if the user changes languages between scheduling the notification and the notification being delivered.
"The strings you display in a notification alert should be localized for the current user. Although you can use the NSLocalizedString macros to load strings from your app’s resource files, a better option is to specify your string using the localizedUserNotificationString(forKey:arguments:) method of NSString. The localizedUserNotificationString(forKey:arguments:) method delays the loading of the localized string until the notification is delivered. Thus, if the user changes language settings before a notification is delivered, the alert text is updated to the user’s current language instead of the language that was set when the notification was scheduled."`