Xcode- Alert if iPhone not charging [duplicate] - xcode

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to know when the device is charging?
I am kind of a noob at Xcode so sorry if I make I fool out of my self. I am trying to make an app and one of the functions is to check if the iphone/ipod/ipad is charging or not. If not charging I want it to play a sound perhaps and flash LEDs. Also if it is charging, I want to make it display text such as "Device Charging :) " . By the way I am using the method with the FlipsideViewController, but these features will be in the MainView. I have looked at various examples and have seen the one below as well as many test applications, but I don't know how to use it. Thank you in advance!!
Code:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
//Device is connected (charging or fully charged)
}

It sounds like you might want to start with some tutorials on writing iPhone apps because the code above looks ok (though I haven't tried this specifically). If you don't understand how to do some of the things I describe below (or some of the terms are unfamiliar), I'd recommend getting a good book or checking out some tutorials:
Book: iOS 6 SDK Development (Amazon)
Tutorials: Ray Wenderlich has many good tutorials on his site
Apple Documentation is quite good
There's no point in just writing the code for you because you presumably want to learn how to program and you won't learn much if someone just does it for you.
Here are some pointers on how to think about this, though the behavior you desire isn't completely obvious from what you've said.
If I understand you you want two different actions to happen when the main view is displayed.
To start with, why not get it to set the text string in a field to match the state of charging / not charging?
So you'd add a UILabel to the main view .xib file in Xcode and add an outlet to it in the view controller so that you can set it's text at runtime.
Then you'll want to call the first line above somewhere once to start monitoring (parameter is: YES) and again with NO to stop monitoring once the view goes away. If you look at the documentation for UIViewController at developer.apple.com near the top you'll see a list of methods that UIViewController implements. In this case you probably want to override (create your own version of) viewWillAppear and viewWillDisappear and put the setBatteryMonitoringEnabled call with YES and NO into these two methods respectively.
So that takes care of enabling / disabling battery status monitoring. Now you need to test the state and take action (this is call to batteryState in your code above).
One question about design which isn't obvious from your description is if you want to do this once when the view appears or if you want it to continually monitor the state.
If you want to do it once, you could put the call to the above in viewDidAppear, say, and then use the outlet to the UILabel to set the message in the label to "Charging" or "Not Charging" based on the result from the batteryState method.
If you want something that watches for changes in the state of charging then you need to subscribe to the notification and put your code to change the UILabel (or whatever you finally do in response to a change) in the handler for that notification.
You figure this out by reading the documentation for UIDevice at developer.apple.com or in Xcode's Organizer window's Documentation section. If you read the documentation for the UIDevice method batteryMonitoringEnabled you can see that the "See Also" section includes two entries for the notifications you can subscribe to to find out when the Level or the State changes: UIDeviceBatteryLevelDidChangeNotification and UIDeviceBatteryStateDidChangeNotification. To learn how to subscribe to notifications you'll want to look at NSNotificationCenter documentation.
Then once you get this working you can add your fancier alerts (be careful about putting them up repeatedly!) and sounds (though playing a sound when the battery isn't charging will use battery which is somewhat questionable.. just make sure not to do it repeatedly perhaps).
good luck!

Add this to the appDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryStateDidChange:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}
- (void)batteryStateDidChange(NSNotification*)notif {
// check the status here.
// See if it is charging, or not and respond to the change.
}
(edited to make code more explicit)

Related

How do I create an action after a specific number of touches on a screen?

I am making an app that has to save the screen, like a screenshot, when the screen is tapped or touched a specific number of times. I have tried all of the solutions that other users have suggested that are associated with my question, but nothing helps...
I will appreciate all suggestions. :)
Thanks
The following blog post does a good job of explaining the built-in option for recognizing multiple taps in a row (and explains the shortcomings): Detecting tap and double-tap with Gesture Recognizers.
If you need more customized logic than is provided by the built-in gesture recognizers, you will either be implementing your own custom subclass of UIGestureRecognizer or you will be adding your logic into the UIResponder (superclass of UIViewController, UIView, etc.) callbacks for tap input: touchesBegan:withEvent:, touchesMoved:withEvent:, and touchesEnded:withEvent:.
I have more experience with the latter method (not UIGestureRecognizer). The UITouch events passed to the various UIResponder callbacks each contain information about touch location and touch timing. You could use this information in combination with a NSTimer to determine if the user taps twice (or more) within a certain amount of time. If the timer fires before the second (or nth touch), then you can consider it a single touch event.
I don't know if that is the best way to do this, but it is certainly more granular control than the built-in UIGestureRecognizers provide you.

How do i get NSPopover to be first responder from the NSStatusbar?

My question is simple and easy; How do i get NSPopover to be first responder from the NSStatusbar?
I'm asking for a short code, not a link to a big XCode-project. Thanks.
Though you've probably solved your question by now, you can do this:
[yourPopover becomeFirstResponder];
If you want more control, just setup your containing controller for a NSPopoverDelegate.
[yourPopOver setDelegate:self]; /* Don't forget including the <NSPopoverDelegate> in your headerfile */
Then you can use 5 functions to gain more control over your popover.
– popoverShouldClose:
– popoverWillShow:
– popoverDidShow:
– popoverWillClose:
– popoverDidClose:

How to force TouchesEnded

The user drags the game piece (the i-th image view) to its target location. The program counts, sees that all the items are in the correct places, announces "Game Over" and sets userInteractionEnabled to NO.
Great, except that if the user's finger is still down on the game piece, the user can drag the piece back out of the target area by accident. "Game Over" is showing but the piece is no longer in the correct place.
Is there a way to force a touchesEnded (not detect a touchesEnded) so that the contact with the game piece is (effectively) broken when the piece is in its final destination (i.e. so that the user can't accidentally pull it out of position)?
userInteractionEnabled = NO does not seem to take effect until the touch is released.
I'm not aware of a way to force touchesEnded.
Obviously one way to handle this is for your application to maintain state that indicates that the game is over and guard against moving any game pieces when in that state.
You might try beginIgnoringInteractionEvents, though I doubt this will do what you are looking for. I really think managing state in your application that ensures that you will do the right thing, not moving the piece once the end state has been reached, is the way to go.
From Apple's Event Handling Guide for iOS:
Turning off delivery of touch events for a period. An application can
call the UIApplication method beginIgnoringInteractionEvents and later
call the endIgnoringInteractionEvents method. The first method stops
the application from receiving touch events entirely; the second
method is called to resume the receipt of such events. You sometimes
want to turn off event delivery while your code is performing
animations.
I found this question after wanting to do a very similar action within my own application. I wanted the touchesEnded: method to be run immediately after clicking on the moving action (like a touchDown action method), but did not know how to achieve this. In the end this is what I did and it WORKED! :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self touchesEnded:touches withEvent:event];
});
}
This worked perfectly!

- (void)swipeWithEvent:(NSEvent *)event not working on Lion?

I'm writing a simple cocoa program that should use the swipe gesture.
I've implemented in my NSView subclass the method swipeWithEvent: but when i try the program the method is never called. rotateWithEvent: method works instead.
I'm using a Xcode 4.1 on Mac OS 10.7 Lion.
Is there a difference between rotateWithEvent: and swipeWithEvent: ?? Why the first is called when I'm under the view and do a rotate gesture and the second in the same condition is never called if i do the swipe gesture?
Update :
I built also a simple project only to check the swipeWithEvent: and rotateWithEvent: methods but the behavior is the same.
Take a look at this sample code I wrote https://github.com/oscardelben/CocoaNavigationGestures
I think it would be helpful if you posted your code, reduced down to the bare essentials if possible.
One thing to look at is to make sure the method signature exactly matches the definition. In this case it should be:
- (void) swipeWithEvent: (NSEvent*) event
{
NSLog( #"A swipe happened" );
}
Make sure your definition matches this. Since you have a rotateWithEvent: that is working correctly this is probably unlikely but sometimes a typo can creep in.
Another thing you can do is to make a sample project that does nothing but respond to a swipe by logging (or whatever). This can help identify if there is something else ii your code or view hierarchy that is getting in the way.

Cocoa forControlEvents:WHATGOESHERE

In Cocoa/Objective-C if I have created a button programmatically, what do I put in for my control event?
[btnMakeChar addTarget:self action:#selector(makeChar:) forControlEvents:WHAT GOES HERE?];
In iOS you can write it like so forControlEvents: UIControlEventTouchUpInside
I can't find anything to show what I would use for just cocoa, not cocoa touch
I'm not sure if I understand you correctly, but if you're programming a Mac,
[theHappyButton setTarget:self];
[theHappyButton setAction:#selector(doStuff)];
it is two separate lines, rather than the one combined line of code on an iPhone.
I hope that is what you were after??
To find it in the doco: choose on the 10.6 doco (not iOS) and search on "setAction:". You'll see it in NSControl Class Reference. NSButton is of course a subclass of NSControl.
The method you're asking about does not exist in Cocoa, so nothing goes there. Cocoa controls have a single target with a single action, and either use a different addTarget:-type method for each kind of action or expect a delegate object that will handle all the events they generate.

Resources