iOS How to cancel a scheduled localnotification - iphone

I am writing an iPhone app in which I have three countdown timers, that each set a local notification when they are started by the user (IBAction).
My question is, how can I reference to an existing local notification elsewhere in the code? I have tried referencing it directly like this which crashes:
[[UIApplication sharedApplication] cancelLocalNotification:myNotification];
And like this after scheduling the notification and adding it to the user defaults:
In my scheduling method...
[myDefaults setObject:myNotification forKey:#"myNotification"];
...
And in my cancelling method...
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[[UIApplication sharedApplication] cancelLocalNotification:[myDefaults objectForKey:#"myNotification"]];
[myDefaults synchronize];
My app crashes with a SIGABRT error on the cancelLocalNotification line above. Can anybody tell me where I am going wrong?
Thanks in advance!

Ok, I've found a working solution to my issue after thinking it through step by step. I found I was going about the whole issue entirely the wrong way!
Essentially, I found you don't need any local notifications when the app is active (i.e. in foreground). So instead of setting the local notification when the timer is started, I set the relevant notifications up when the app is about to resign.
I put these observers in my viewDidLoad;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillResign) name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appIsActive) name:UIApplicationDidBecomeActiveNotification
object:nil];
and in my appWillResign method, I set up the notifications for any active timers. When the app is resumed, I simply cancel ALL the notifications.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
In short, you shouldn't need to reference the notifications elsewhere in the code. You only set them up when they are absolutely needed: when the app is backgrounded! So you really don't need to 'store' them anywhere for later use.
Thanks for your contribution #Learner, you helped put me on the right track! :D

It is difficult to predict but generally SIGABRT comes when you access an nil or released object.
So in [[UIApplication sharedApplication] cancelLocalNotification:myNotification]; `
it can be "myNotification" which is nil.
OR if [myDefaults objectForKey:#"myNotification"] returns nil.
Possible resason for that can be that NsUserDefaults stores only few data type as mentioned in following paragraph of class reference.
A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
What i can predict is that myNotification which is being stored is of type UILocalNotification. so probably storing of data is failing.
Please debug the code further and post if above cases are not working.

The fix is to sync the NSUserDefaults. I'm assuming you just set the object and did not synchronize it. If so the object will not actually save to NSUserDefaults, rather just to the local NSUserDefaults object. What you need to do is call [defaults synchronize] after setting the object.

Related

Validate if call was made after telprompt

I want to validate if user tapped on Call button or Cancel button after telprompt
Current code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", [personDetails valueForKey:#"phone"]]];
[[UIApplication sharedApplication] openURL:url];
How can I do so?
First telprompt: is not a documented URL schema and should not be used. Since Apple can change the way it used at any moment.
Second since data is passed back to your app, you will not be able to detect a if call was made. You might be able to detect if you use the CoreTelephony. But getting this to work require your app to run in the background and you might have to misuse some background mode for this which will make Apple reject your app.
Can you explain why you want to detect if there was a call made?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(called:) name:#"UIApplicationSuspendedNotification" object:nil];
-(void)called:(NSNotification *) notification
{
NSLog(#"Tapped Call button");
}
if Call button was tapped then application will be terminated and go into background so just add an observer for the UIApplicationSuspendedNotification notification.
For my case, I used tel:// instead of using telprompt:// and make my own UIAlertView. This way you could detect if the call option is tapped from the UIAlertView's delegate.

NSPersistentStoreDidImportUbiquitousContentChangesNotification not being notified

If an entry is changed in core data on another device, NSLog messages show that it's noticed the changes, but NSPersistentStoreDidImportUbiquitousContentChangesNotification is not being called. It takes until a save on my first device for it to know to update my tableview.
This is my code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(iCloudUpdates:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:nil];
Anybody know why this might not be working?
First option:
If your problem is a delay in updating both devices, that is normal behavior, Apple doesn't guarantee update timing, but it is usually fast.
If on the other side the problem is that iCloudUpdates method is not called make sure that signature is correct, should be :
-(void)iCloudUpdates:(NSNotification*)notification {
// do your stuff here
}
Second option:
at time of writing, iOS 5 has big big problem with iCloud and CoreData, I recently shipped my application without iCloud support.
If you want to know what is going on turn logging on for both CoreData and iCloud by putting:
-com.apple.CoreData.SQLDebug 1
-com.apple.coredata.ubiquity.logLevel 3
in your scheme manager, under run->arguments tab.
If you see some strange errors in 'destination' device that's the case of iCloud not working for being bugged.
I think the problem with your code is that in the "addObserver" you've set the object to nil. The object should be your persistentStoreCoordinator as shown below.
__weak NSPersistentStoreCoordinator *psc = self.context.persistentStoreCoordinator;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(iCloudUpdates:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:psc];

How do we prevent "CoreData could not fulfill a fault"?

We get "CoreData could not fulfill a fault" every once in a while. We have read through the Apple documentation but are unclear on what is allowed to be retained. We have been very careful about creating one context per thread, etc. However, one thing our app is doing is we are retaining NSManagedObjects on our UIViewControllers (usually via a NSArray or NSDictionary). I'm guessing what's going on is the object relationships are changing and we are not handling the appropriate notification.
Does anyone have any suggestions on the better design with regards to Core Data? When we get the error, I cannot see that we actually deleted anything from the context to cause the fault. Is it necessary to handle NSManagedObjectContextObjectsDidChangeNotification on our UIViewControllers if they are retaining state? Any suggestions would be appreciated.
You can register for change notifications in Core Data. This will allow you to update your managed objects when they change. See the Core Data Docs for more info. You're going to be interested in 2 methods to register and respond to changes:
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:(your NSManagedObjectContext)];
The mergeChanges selector (your method) will call the following method to synchronize any changes from other threads. It will look something like this:
- (void)mergeChanges:(NSNotification *)notification{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
// Merge changes into the default context on the main thread
[context performSelectorOnMainThread:#selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}

Getting variables from one view to another

I realise that there are other topics like this, but none of them really help. I'm trying to get variables from one view into another, but I have absolutely no idea how.
To give some backstory, my game is a fruit ninja like game where stuff goes on the screen and you have to slice it. If 3 sprites leave the screen unsliced, the game is over and it flips to the game over view screen with a button to go back. Additionally, this SHOULD go to the "flipSideView", which is the highscore, but my implementation of the flipSideView transition doesn't work. This isn't the main issue, the main issue is that I don't know how to get the score from the game in the mainView (which stores it as an int) into the flipSideView (which has the player name).
The main view changes to the gameOverView through this condition in the tick method (which performs regular checks and methods for the game)
if (lifeCounter < 1)
{
gameIsOver = YES;
[self showInfo:0];
[self viewGameOverScreen];
}
That goes to the gameOverView, which will sit there until the replay button is pressed with:
- (IBAction)replayAction:(id)sender
{
[self.delegate gameOverViewControllerDidFinish: self];
}
gameOverViewControllerDidFinish restarts the game and dismisses the view.
- (void) gameOverViewControllerDidFinish: (GameOverViewController *) controller
{
[self restart];
[self dismissModalViewControllerAnimated: YES];
}
The restart method just resets the 3 primary values in the main view (The score, the level, the lives).
As it restarts, the game should take the score and whatever name is stored in the text field in the flipSideView (which can be viewed at any one time gameplay) and store it somehow for future reference. It's not supposed to store it in a file yet because that's next week's task.
I don't wanna post the entire program due to plagiarism issues, but if there are additional parts that might make it easier to understand, I will definitely post them.
Thanks in advance,
Christian
Use Search
NSUserDefaults *currentScore = [NSUserDefaults standardUserDefaults];
[currentDefaults setFloat:yourScoreVariable forKey:#"HighScore"];
[currentDefaults synchronize];
Above to store the score.
Below to retrieve the score.
CGFloat fltHighScore;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
fltHighScore = [currentDefaults floatForKey:#"HighScore"];
Suggestion:
You could put the variable in your Controller class,where they both could access.
It is always better to have sharable data in Controller then views if data has to be shared among views.
Update the shareable data through delegate method.
You can use your app delegate for this. You need to declare your variable there, and then you can access this in the following way throughout your whole app:
TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *string = [appDelegate.Placemarks objectForKey:appDelegate.title];
Don't know if it's the best way tough...
Luck with it!

OCUnit testing NSNotification delivery

For a game I'm developing, I have several model classes that trigger notifications when their state changes. Then, the view subscribes to those notifications and can react on them.
I'm doing my unit tests for the model with OCUnit, and want to assert that the expected notifications were posted. For that, I'm doing something like this:
- (void)testSomething {
[[NSNotificationCenter defaultCenter] addObserver:notifications selector:#selector(addObject:) name:kNotificationMoved object:board];
Board *board = [[Board alloc] init];
Tile *tile = [Tile newTile];
[board addTile:tile];
[board move:tile];
STAssertEquals((NSUInteger)1, [notifications count], nil);
// Assert the contents of the userInfo as well here
[board release];
}
The idea is that the NSNotificationCenter will add the notifications to the NSMutableArray by calling its addObject: method.
When I run it, however, I see that addObject: is being sent to some other object (not my NSMutableArray) causing OCUnit to stop working. However, if I comment out some code (such as the release calls, or add a new unit test) everything starts working as expected.
I'm assuming this has to o with a timing issue, or NSNotificationCenter relying on the run loop in some way.
Is there any recommendation to test this? I know I could add a setter in Board and inject my own NSNotificationCenter, but I'm looking for a quicker way to do it (maybe some trick on how to replace the NSNotificationCenter dynamically).
Found the problem. When testing notifications you need to remove the observer after you have tested it. Working code:
- (void)testSomething {
[[NSNotificationCenter defaultCenter] addObserver:notifications selector:#selector(addObject:) name:kNotificationMoved object:board];
Board *board = [[Board alloc] init];
Tile *tile = [Tile newTile];
[board addTile:tile];
[board move:tile];
STAssertEquals((NSUInteger)1, [notifications count], nil);
// Assert the contents of the userInfo as well here
[board release];
[[NSNotificationCenter defaultCenter] removeObserver:notifications name:kNotificationMoved object:board];
}
If you fail to remove the observer, after a test runs and some local variables are released, the notification center will try to notify those old objects when running any subsequent test that triggers the same notification.
There are no timing issues or runloop related problems since everything in your code is non-concurrent and should be executed immediately. NSNotificationCenter only postpones notification delivery if you use an NSNotificationQueue.
I think everything is correct in the snippet you posted. Maybe there's an issue with the mutable array 'notifications'. Did you init and retain it correctly? Try to add some object manually instead of using the notification trick.
If you suspect your tests have timing issues - you may want to consider injecting your own notification mechanism into your board object (which is probably just a wrapper of the existing apple version).
That is:
Board *board = [[Board alloc] initWithNotifier: someOtherNotifierConformingToAProtocol];
Presumably your board object posts some notification - you would use your injected notifier in that code:
-(void) someBoardMethod {
// ....
// Send your notification indirectly through your object
[myNotifier pushUpdateNotification: myAttribute];
}
In your test - you now have a level of indirection that you can use for testing, so you can implement a test class the conforms to your AProtocol - and maybe counts up the pushUpdateNotification: calls. In your real code you encapsulate the code you probably already have in Board that does the notification.
This of course is a classic example of where MockObjects are useful - and there is OCMock which well let you do this without having to have a test class to do the counting (see: http://www.mulle-kybernetik.com/software/OCMock/)
your test would problably have a line something like:
[[myMockNotifer expect] pushUpdateNotification: someAttribute];
Alternatively you could consider using a delegate instead of notifications. There is a good pro/con set of slides here: http://www.slideshare.net/360conferences/nsnotificationcenter-vs-appdelegate.