CenoX
CenoX

Reputation: 306

Get a battery status

I'm making a game.

If phone's battery under 60%, the yield decrement is 60 - current battery%

So, My question is: Can I get an information about battery % in iOS?

Upvotes: 0

Views: 333

Answers (3)

Arbitur
Arbitur

Reputation: 39081

If you wa t to get the Battery charge use this [UIDevice currentDevice].batteryLevel

It will return a float 0.0 - 1.0.

Upvotes: 2

user2824854
user2824854

Reputation:

iOS Developer Library

UIDevice Class Reference

You can download sample from here.

Upvotes: 2

Jano
Jano

Reputation: 63667

NSArray *stateArray = @[@"Battery state is unknown", 
                        @"Battery is not plugged into a charging source", 
                        @"Battery is charging", 
                        @"Battery state is full"];

NSString *status = [NSString stringWithFormat:@"Battery state: %@, Battery level: %0.2f%%",
                    stateArray[[UIDevice currentDevice].batteryState],
                    [UIDevice currentDevice].batteryLevel * 100];

NSLog(@"%@", status);

Upvotes: 2

Related Questions