Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:57:41+00:00 2026-06-16T09:57:41+00:00

Possible Duplicate: How to know when the device is charging? I am kind of

  • 0

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)
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:57:42+00:00Added an answer on June 16, 2026 at 9:57 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Xcode 4.4.1, iOS 5.1: Xcode cannot run using the selected device My
Possible Duplicate: iOS 5.1 with Xcode 4.3.1: [UIColor colorWithPatternImage:] strange behavior only on device
Possible Duplicate: Determine if running on a rooted device I would like to know
Possible Duplicate: create smart device cab through msbuild I know MSBUild struggles to build
Possible Duplicate: How to know if Process.Start() is successful? I have a process similar
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: Java String.equals versus == I know it' a dumb question but why
Possible Duplicate: Servlets: doGet and doPost I know doGet() request is appended to the
Possible Duplicate: Rename Oracle Table or View I know you can use ALTER TABLE
Possible Duplicate: how to upload folder with php I know we can upload single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.