How to delay the shake notification - iphone

I want to delay the shake for 5 seconds because if the user continuously shakes the device, the response is showing null. So that is why I want to delay the shake until n unless response is alive.
Here's my code is
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
[FlurryAnalytics logEvent:#"User shaked to update"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CheckWeather" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"startWeatherNeue" object:nil];
if ( [super respondsToSelector:#selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}
}

in objective c i use sleep(6) for stop processing,
put sleep(6) befor your fired notification code:-
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
sleep(6);
[FlurryAnalytics logEvent:#"User shaked to update"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CheckWeather" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"startWeatherNeue" object:nil];
if ( [super respondsToSelector:#selector(motionEnded:withEvent:)] )
[super motionEnded:motion withEvent:event];
}
}
and then process stop for 6 second and then notification fire after 6 seconde may be its helpful for you
other
u also use NSTimer :-
in NSTimer u check your response array count >0 in if else condition and call method with 1 second when your response array > 0 then call NSNOtification method
here my example :- with NSTimer
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
self.TimeOfActiveUser = [NSTimer scheduledTimerWithTimeInterval:01.0 target:self selector:#selector(checkInfoString) userInfo:nil repeats:YES];
}
-(IBAction)checkInfoString
{
if([responsearray count]>0)
{
[FlurryAnalytics logEvent:#"User shaked to update"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CheckWeather" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"startWeatherNeue" object:nil];
if ( [super respondsToSelector:#selector(motionEnded:withEvent:)] )
{
[super motionEnded:motion withEvent:event];
}
}
else
{
NSLOG
}
}

To perform some selector later you can use:
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay

Related

motionEnded not called in appDelegate

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];
}

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 Get UIAccessibilityVoiceOverStatusChanged Notification

How to implement to get UIAccessibilityVoiceOverStatusChanged Notification?
I tried like below but nothing happens :
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(notified:) name:UIAccessibilityVoiceOverStatusChanged object:self];
That looks reasonable, except maybe object:self should be object:nil?
The other thing is to be sure your signature is correct:
- (void)voiceOverStatusChanged: (NSNotification *)notification;
I think you might try adding the observer in the awakeFromNib method with the right selector signature.
Something like this will work
- (void)awakeFromNib {
[super awakeFromNib];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(voiceOverChanged)
name:UIAccessibilityVoiceOverStatusChanged
object:nil];
[self voiceOverChanged];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIAccessibilityVoiceOverStatusChanged object:nil];
}
- (void)voiceOverChanged {
// Your actions here
}
you can get the UIAccessibilityVoiceOverStatusChanged Notification with the code
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(didChangeVoiceOverStatus:)
name:UIAccessibilityVoiceOverStatusChanged
object:nil];
}
- (void)didChangeVoiceOverStatus:(NSNotification *)notification {
if (UIAccessibilityIsVoiceOverRunning()) {
NSLog(#"VoiceOver is ON.");
} else {
NSLog(#"VoiceOver is OFF.");
}
}

Shake Detection iPhone 3.0 not working

I have a ViewController that works perfectly with a button that trigger an action. I would like to replace the button with a shake event so I've googled it around and created a ShakeDetector class that ineherits from UIView
and my implementation is as follow:
#implementation ShakeDetector
- (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:#"spin" object:self];
NSLog(#"sss");
}
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
#end
But I can't make it work... any help?
Thanks
Put :
-(BOOL)canBecomeFirstResponder
{
return YES;
}
and for your view :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewDidDisappear:animated];
}
You can also write it in viewWillAppear and viewWillDisappear

How to check for request time out , is there a notification I can set?

I want to check for whether the request has timed out after some period.
I know NSNotifiactionCenter ,but don't know how to set the request time out notification for it.
thanks.
You could just use a timer and cancel your notification request if not received by then?
e.g. taking Apple's reachability example:
- (void) startNotifier
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onReachabilityChanged:) name:#"kNetworkReachabilityChangedNotification" object:nil];
notified = NO;
[self performSelector:#selector(onRequestTimeout) withObject:nil afterDelay:5.0]; // 5 secs
}
- (void)onReachabilityChanged:(NSNotification *)note
{
// Do whatever on notification
notified = YES;
}
- (void) onRequestTimeout
{
if (!notified)
{
// Do whatever on request timeout
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"kNetworkReachabilityChangedNotification" object:nil];
}
}