{"id":1233,"date":"2018-10-30T23:10:00","date_gmt":"2018-10-30T22:10:00","guid":{"rendered":"https:\/\/www.albertopasca.it\/whiletrue\/?p=1233"},"modified":"2018-10-04T14:59:26","modified_gmt":"2018-10-04T12:59:26","slug":"swift-battery-level-and-charger-status","status":"publish","type":"post","link":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/","title":{"rendered":"Swift &#8211; Battery level and charger status"},"content":{"rendered":"<p>Apple offer the <em>batteryState<\/em>\u00a0in the <strong>UIDevice<\/strong> class:<\/p>\n<p><!--more--><\/p>\n<pre>UIDevice.current.batteryState<\/pre>\n<p>You cannot read the <strong>batteryState<\/strong> <em>directly<\/em>, you need first to enable the\u00a0<strong>isBatteryMonitoringEnabled<\/strong>:<\/p>\n<pre>UIDevice.current.<strong>isBatteryMonitoringEnabled<\/strong> = true<\/pre>\n<div class=\"topic-container section-content row\">\n<div id=\"topic-content\" class=\"topic-content column large-9 medium-9 small-12\">\n<section id=\"discussion\" class=\"section\">\n<div class=\"formatted-content\">\n<p>As Apple said:<\/p>\n<blockquote><p>Enable battery monitoring if your app needs to be notified of changes to the battery state, or if you want to check the battery charge level.<\/p>\n<p><strong>The default value of this property is\u00a0<code class=\"code-voice\">NO<\/code><\/strong>, which:<\/p>\n<ul>\n<li>Disables the posting of battery-related notifications<\/li>\n<li>Disables the ability to read battery charge level and battery state<\/li>\n<\/ul>\n<\/blockquote>\n<\/div>\n<\/section>\n<\/div>\n<\/div>\n<section id=\"see-also\" class=\"contenttable section alt-light-container row\">\n<div class=\"alt-light\">\n<div class=\"section-content\">Setting this to true enable you to get all of these statuses:<\/div>\n<\/div>\n<pre>public enum <strong>BatteryState<\/strong> : Int {\r\n    case unknown   \/\/ <strong>called if the monitor is not enabled<\/strong>\r\n    case unplugged \/\/ without charger attached\r\n    case charging  \/\/ with charger attached\r\n    case full      \/\/ at 100%\r\n}<\/pre>\n<p>Here a simple function to read all these values:<\/p>\n<pre>func batteryStatus() {\r\n  UIDevice.current.<strong>isBatteryMonitoringEnabled<\/strong> = true\r\n\r\n  switch UIDevice.current.<strong>batteryState<\/strong> {\r\n    case .unknown:\r\n      print(\"BATTERY: UNKNOWN!\")\r\n    case .<strong>charging<\/strong>:\r\n      print(\"BATTERY: charging...\")\r\n    case .full:\r\n      print(\"BATTERY: full\")\r\n    case .<strong>unplugged<\/strong>:\r\n      print(\"BATTERY: unplugged...\")\r\n  }\r\n}<\/pre>\n<p>If you want also to know the battery level, you can use <strong>batteryLevel<\/strong>.<\/p>\n<p>The battery level is a range\u00a0from <strong>0.0<\/strong> (fully discharged) to <strong>1.0<\/strong> (100% charged).\u00a0 This property become <strong>-1.0<\/strong> if\u00a0the battery monitor is not enabled.<\/p>\n<pre>print(\"BATTERY level: \\(UIDevice.current.batteryLevel)\")<\/pre>\n<p class=\"p1\">that&#8217;s all.<\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Apple offer the batteryState\u00a0in the UIDevice class:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[133,753,755,754,756,633,759,717,138,761,760,758,46,216,757],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-generic","tag-apple","tag-battery","tag-battery-status","tag-batterylevel","tag-charger","tag-device","tag-full","tag-ios12","tag-iphone","tag-isbatterymonitoringenabled","tag-monitor","tag-plugged","tag-swift","tag-uidevice","tag-unplugged"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift - Battery level and charger status - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift - Battery level and charger status - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"Apple offer the batteryState\u00a0in the UIDevice class:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-30T22:10:00+00:00\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"Swift &#8211; Battery level and charger status\",\"datePublished\":\"2018-10-30T22:10:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/\"},\"wordCount\":141,\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"keywords\":[\"Apple\",\"battery\",\"battery status\",\"batteryLevel\",\"charger\",\"device\",\"full\",\"ios12\",\"iPhone\",\"isBatteryMonitoringEnabled\",\"monitor\",\"plugged\",\"swift\",\"UIDevice\",\"unplugged\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/\",\"name\":\"Swift - Battery level and charger status - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"datePublished\":\"2018-10-30T22:10:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-battery-level-and-charger-status\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift &#8211; Battery level and charger status\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift - Battery level and charger status - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/","og_locale":"en_US","og_type":"article","og_title":"Swift - Battery level and charger status - while (true) {\u00a0}","og_description":"Apple offer the batteryState\u00a0in the UIDevice class:","og_url":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2018-10-30T22:10:00+00:00","author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/#article","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/"},"author":{"name":"Alberto Pasca","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"Swift &#8211; Battery level and charger status","datePublished":"2018-10-30T22:10:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/"},"wordCount":141,"publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"keywords":["Apple","battery","battery status","batteryLevel","charger","device","full","ios12","iPhone","isBatteryMonitoringEnabled","monitor","plugged","swift","UIDevice","unplugged"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/","url":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/","name":"Swift - Battery level and charger status - while (true) {\u00a0}","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website"},"datePublished":"2018-10-30T22:10:00+00:00","breadcrumb":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.albertopasca.it\/whiletrue\/swift-battery-level-and-charger-status\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"Swift &#8211; Battery level and charger status"}]},{"@type":"WebSite","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website","url":"https:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":2,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1258,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1233\/revisions\/1258"}],"wp:attachment":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}