B3TA
B3TA

Reputation: 489

Class Not Being Called Swift iOS

I'm working on my first app for OSX 10.8 using Swift. I want to be able to have the state of the battery dictate the text in Today pulldown menu. That aspect of the code works, but I am very frustrated as my class never gets called. It is in a separate supporting script called 'DeviceMonitor.swift'. Thanks for the help! Code:

import Foundation
import UIKit

class BatteryState {

    var device: UIDevice

    init() {
        self.device = UIDevice.currentDevice()
        println("Device Initialized")
    }

    func isPluggedIn(value: Bool) {
        let sharedDefaults = NSUserDefaults(suiteName: "group.WidgetExtension")

        let batteryState = self.device.batteryState

        if (batteryState == UIDeviceBatteryState.Charging || batteryState == UIDeviceBatteryState.Full){
            let isPluggedIn = true
            println("Plugged In")
            sharedDefaults?.setObject("Plugged In", forKey: "stringKey")
        }

        else {
            let isPluggedIn = false
            println("Not Plugged In")
            sharedDefaults?.setObject("Not Plugged In", forKey: "stringKey")
        }
        sharedDefaults?.synchronize()
    }
}

Upvotes: 0

Views: 313

Answers (1)

Ian MacDonald
Ian MacDonald

Reputation: 14010

Unless you have done it somewhere else in your code, it looks like you haven't registered to receive UIDeviceBatteryLevelDidChangeNotification events. You should also ensure that the current UIDevice has batteryMonitoringEnabled as YES.

Upvotes: 3

Related Questions