How to monitor NSSystemClockDidChangeNotification in iPhone SDK - iphone

I Need to know if the user change the system time in the springboard settings.so my background program can be notified. According to doc, NSSystemClockDidChangeNotification is the one choice.
but I can't receiving anything by this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSysTimeChanged:)
name:NSSystemClockDidChangeNotification
object:nil];
Where am I wrong? Any suggestions?

Your handler method must be implemented in the same class you add as observer and needs to look like this:
-(void) handleSysTimeChanged: (NSNotification*) notification
{
// ...
}

Related

In terms of iphone application, the way to reload displayed page automatically when a background app comes back to be active

I have developed an iphone application with Phonegap/Cordova v1.9.0.
I want to realize the following matter.
-When a background app comes back to be active(When the app icon is tapped), a displayed page is reloaded automatically-
Probably I should make some programs in a function, (void)applicationDidBecomeActive, in Appdelegate.m or MainViewController.m, but I have no idea what to do.
Please tell me how to solve this case.
You can use the NSNotification observer pattern. In your MainViewController.m file, and viewDidLoad, you can add an observer (registering for notifications):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
Then you must implement appDidBecomeActive: (you can give the selector any name, but you must implement a method of that name). In this example:
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(#"App became active");
}
When the app is resumed, and should this view controller be active, it will simply log that to the console. You can put any code you wish inside that method (in your case, refreshing a page).
Don't forget to remove the observer when the view controller is deallocated in the dealloc method. This will remove all observers for you.
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSLog(#"Removed all notification observers");
}
Are you really using version 1.9?
Try this:
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event, reload the page or content
}

Execute code after method has finished

I need to execute some code after I know the keyboard is hidden.
Ive been looking in to blocks but I'm just not understanding how they work enough to do this...
All I want to do is run [self hidekeyboard] then when that is complete (and the keyboard fully hidden) then I want to call a delegate.
What is the best way to handle this and how?
You want to use the UIKeyboardDidHide notification and run your code in there. Here is the link in the docs...
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(onKeyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
And the onKeyboardDidHide:
-(void)onKeyboardDidHide:(NSNotification *)notification
{
// execute what you want.
}
Register a listener for the UIKeyboardDidHideNotification using the NSNotificationCenter class.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardHidden:)
name:UIKeyboardDidHideNorification
object:nil];
- (void)keyboardHidden:(NSNotification *)notif
{
// do stuff
}
(Don't forget to remove the observer in - dealloc so that no messages will erroneously be sent to deallocated objects.)
You probably want to register to receive notifications of UIKeyboardDidHideNotification.
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

send application delegate message to current view without notification center

I want to display application delegate message such as "Application became active" (This is called when -applicationDidBecomeActive:application is called)
on Window.
One way is to use notification center like below:
AppDelegate.m
NSNotification *n = [NSNotification notificationWithName:#"AppBecameActive" object:self];
[[NSNotificationCenter defaultCenter] postNotification:n];
ViewController.m
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(showMessageAppBecameActive) name:#"AppBecameActive" object:nil];
This way is only way to show application delegate message ? Or, is there any other way such as property to look current view controller instance ?
Thank you for your kindness.
If you have access to the ViewController from your appDelegate. (I mean like #property or instance is reside in it) you can straight away send a message. If you do not have such access to it. write key value observer for a single ton and let your viewController receive the change.
Register below code in your viewController which wants notification.
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];
iOS framework posts a notification when your app become active, if you will register via above way, you can handle the notification in registered method (In this case yourMethod).

i want to send some notifications to observers when some event occurs. and i also want to know how observer catch/handle/receive that notification?

while reading cocoa fundamental guide i have gone through:
(void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
I am getting all theories, but actually i am looking for a real example where it is used ?
Can any one give some sample example.
Actually i want to send some notifications to observers when some event occurs.
and i also want to know how observer catch/handle/receive that notification ??
Thanks
To listen for notifications, add code similar to this in the instance that you want to receive notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doThisWhenNotificationRecd:)
name:#"SOME_NOTIFICATION_NAME"
object:nil];
When you want to post a notification:
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:#"SOME_NOTIFICATION_NAME" object:nil]];
I use code similar to this to update my UI while processing an in app purchase.

GameKit Notification when user change

i've some problem with game kit.
All work fine, the olny problem is the changement of user logged.
I want to know when the user signout or change account in gamecenter when my app is running.
I've use the next code for this kind of notification but it don't work
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self selector:#selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
I do not receive any notification, this cose in into view did load!
Any ideas??
Please Help ME!!
I've tryed to create a new project only whit gamecenter sign in and notification but the problem it's not solved :(
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {NSLog(#"SDADA %#",error);}];
NSLog(#"%#",[[GKLocalPlayer localPlayer]alias]);
}
-(void)authenticationChanged
{
NSLog(#"ooo");
}
what i forgot??
Under iOS 4.1 GKPlayerAuthenticationDidChangeNotificationName does not fire in the background under the simulator. I reported this as a bug to Apple and they replied that this notification will fire upon resume in iOS 4.2
The code seems to be right. Also check that your handler is similar to:
- (void) authenticationChanged
{
if ([GKLocalPlayer localPlayer].isAuthenticated)
// Insert code here to handle a successful authentication.
else
// Insert code here to clean up any outstanding Game Center-related classes.
}
Also, you probably want to register for notification in the init function and not in viewDidLoad
OK, now i know my problem but i've not the solution.
The problem is the field isAuthenticated of GKLocalPlayer, when the user logout this field do not change.
are There somethings i forget?
any ideas??
||||||THE PROBLEM IS THE SIMULTOR WITH DEVICE ALL WORK!