motionEnded not called in appDelegate - iphone

I want to integrate shake feature throughout the app. So I am doing everything in appDelegate. I need to push a viewController, I am able to push in motionBegan, but i wanted to do it motionEnded. yes motion ended does work in a view controller, but in app delegate it is not being called.
Doing as
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder{
return YES;
}
motionEnded not called
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake){
NSLog(#"motionEnded called");
}
}
motionBegan called
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if(event.subtype==UIEventSubtypeMotionShake){
NSLog(#"motionBegan called");
}
}

you could basically register your viewController for applicationDidBecomeActiveNotification or any depending on your needs
for example in your viewController's viewDidLoad method you could register it for notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMethod)
name:UIApplicationDidBecomeActiveNotification object:nil];
and implement this method in your class, your myMethod will call everytime your application will become active
-(void) myMethod(){
// do your stuff
}
finally un-register viewController from the notification in dealloc method
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Related

nsnotification not working in skscene

I am writing a sprite kit game and I have in app purchase in my game. I implement the in
app purchase in file IAPHelper.m(IAPHelper.m is a subclass of NSObject), and if the
purchase success, it will post a nsnotification,the code is like:
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(#"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
}
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {
NSLog(#"post a notification");
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:productIdentifier userInfo:nil];
}
And in MyScene.m(MyScene.m is a subclass of SKScene), I add an observer for the notification:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ok:) name:IAPHelperProductPurchasedNotification object:nil];
}
return self;
}
- (void)ok:(NSNotification *)notification {
NSLog(#"Ok, I got it");
}
And What I am trying to do is IAPHelper.m post the notification, and MyScene.m receive it and respond to it. But the result is MyScene.m seems not receiving the notification and thus the code
- (void)ok:(NSNotification *)notification {
NSLog(#"Ok, I got it");
}
is not being called. but if I put the observer in my ViewController.m( subclass of UIViewController), it works. My question is: can skscene receive notification from any object? And if it can, how to implement it.
Many Thanks if anyone can help me with this problem.

what method within a ViewController's class can I call to check when it has been brought to the foreground?

what method within a ViewController's class can I call to check when it has been brought to the foreground?
For example Im looking at a page on my application and I decide to close the application and go back to it later. When I go back to it the same view as I was looking at was on the screen. However... As soon as I open the application I want to segue over to another view.
How can I do this?
Currently trying this:
- (void) applicationDidBecomeActive:(NSNotification*) notification
{
[self checkActivity];
// Do your stuff here
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
- (void)checkActivity{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(#"Checking if re-authentication required...");
if([[defaults objectForKey:#"shouldgotologin"] isEqualToString:#"yes"]){
NSLog(#"View Should go to login...performing segue");
[defaults setObject:#"no" forKey:#"shouldgotologin"];
[defaults synchronize];
[self performSegueWithIdentifier:#"backtologin" sender:self];
} else {
NSLog(#"Should go to login is not true.");
}
}
Register your view controller to observe UIApplicationWillEnterForegroundNotification:
1) Inside view controller's init method:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
2) Inside view controller's dealloc method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
3) Also, have your view controller implement this method:
- (void) applicationWillEnterForeground:(NSNotification*) notification
{
// This method will be called just before entering the foreground;
// Do your stuff here
}
If the timing of UIApplicationWillEnterForegroundNotification doesn't suit you, check all the available notifications for UIApplication here:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
Jump To ApplicationDelegate File, you will find following methods.
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
Remember, It is not the viewController who receives the notifications related to the Application states like willResignActive,didEnterBackground,willEnterForeground. ApplicationDelegate object is going to handle those notifications. So, Try putting your logic in above methods.
Hope that helps. If not, add your queries using comments below my answer.

iOS TableView not reloading if current view is tableViewController

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
NSLog(#"local notifications count = %d", [[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}
This is the method from app delegate, and I need to reload the table view when a notification arrives.
How can I implement the reloadData, since Xcode won't accept if I write "[TableViewController.tableView reloadData];"?
//Just do one thing, as you got the notification , post on more notification as follows in the method ....
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RELOAD_DATA"object:nil];
}
//the add observer in viewDidLoad of that view controller where your table is added..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveTableNotification:)
name:#"RELOAD_DATA"
object:nil];
//and make a method in the same class
- (void)receiveTableNotification:(NSNotification *)pNotification{
[your_table_view reloadData];
}
//now remove obser in dealloc in your view controller class where you add observer ..
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
instead of TableViewController.tableView use [self.viewController.tableView reloadData];
If your current view Controller is not the tableView, consider using NSNotificationCenter to post a reload notification
use NSNotificationCenter postNotification: mechanism for posting a reloadTable notification which you can catch in your class having tableViewController by observer to fire reloadData for your tableview
Please call this in app deligate within the call backfunction
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
UINavigationController *navControl = [[[self tableViewController] viewControllers] objectAtIndex:lastObject]; //
id Obj = [[navControl viewControllers] lastObject];
if([Obj isKindOfClass:[Required class]])
{
[Obj reloadDataOfTable];// made an function to desired class.
}
}

Can't get shake events on iPhone

I am not being able to get shake events on iPhone.
I have followed other questions here with no result. I also tried following the GLPaint example from Apple, but it seems exactly like my source code, with a small diference. GLPaint's source code /works/, mine /doesn't/.
So, here it is what I have:
Controller.m
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(shakeEnded) name:#"shake" object:nil];
}
ShakingEnabledWindow.m
- (void)shakeEnded {
NSLog(#"Shaking ended.");
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake ) {
// User was shaking the device. Post a notification named "shake".
[[NSNotificationCenter defaultCenter] postNotificationName:#"shake" object:self];
NSLog(#"Shaken!");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
}
My XIB has a window, which is a ShakingEnabledWindow and an object, my Controller.
I am running out of ideas here, hope someone can give me a hand. :)
Documentation of NSNotificationCenter says:
addObserver:selector:name:object:
notificationSelector Selector that
specifies the message the receiver
sends notificationObserver to notify
it of the notification posting. The
method specified by
notificationSelector must have one and
only one argument (an instance of
NSNotification).
So your shakeEnded method is wrong as it takes no parameters. It should look:
- (void)shakeEnded:(NSNotification*)notiication {
NSLog(#"Shaking ended.");
}
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(shakeEnded:) name:#"shake" object:nil];
}
In viewDidAppear, become the first responder:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
And make sure you can be first responder:
- (BOOL)canBecomeFirstResponder {
return YES;
}
Then you can implement the motion detection.
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventTypeMotion){
//there was motion
}
}
I think you are incorrectly checking the motion type. You need to check against event.subtype instead of motion:
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if ( event.subtype == UIEventSubtypeMotionShake ) {
// Put in code here to handle shake
}
}

How to receive a notification when a view will appear?

When I'm doing a half-page curl modal transition:
How can I tell when the page has been restored back to it's current state? I want to call something when the "settings" view has been closed.
I tried to use viewWillAppear:(BOOL)animated but it doesn't seem to get called when closing the view. Any ideas?
You can register an NSNotificationCenter observer on your master view and post the notification on your background view. And instead of viewWillAppear you can use viewDidLoad.
// EDIT: sample code to get a touch gesture in a given rect
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[event allTouches]count] == 1) {
UITouch *t = [[touches allObjects]lastObject];
CGPoint p = [t locationInView:self.view];
if (p.y < 200) NSLog(#"above 200");
}
}
In your viewDidLoad, register a Notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateView:)
name:#"updateRootView"
object:nil];
Now this is the notification that we call
- (void) updateView:(NSNotification *) notification
{
/* notification received after the page is uncurled */
}
The calling method:
- (void) unCurlPage
{
// All instances of TestClass will be notified
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateRootView" object:self];
}
And don't forget to dealloc the notification
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
ViewWilAppear is a UIViewController message/method. If you are only changing views, it won't get called. What does the code you are using to close the settings view look like?
Edit
It sounds like you need to refactor a bit. Assuming all this is handled by the parent UIViewController for this settings view, you could implement something like:
- (void)settingsPanelOpen {
// present the modal
// hook to inform of opening (if necessary)
}
- (void)settingsPanelClose {
// dismiss modal
// hook to inform of closing
}
Then settingsPanelClose could have a hook into it if you need to know when the settings closes.
The other thing you could do is subclass UIViewController as SettingsViewController and override the viewDidDisappear: method to kick off a SettingsDidSave notification or otherwise inform your app that it has closed.