• Bonjour Visiteur. Bienvenue sur les nouveaux forums de MacGeneration. La peinture est encore fraiche, quelques boulons doivent être resserrés, plus d’informations demain !

Run Application in background


Nouveau membre
30 Janvier 2016
United Kingdom
I want to be alerted when the battery level has changed. I can get the device level. Everything is ok so far. The problem is what do I do to keep themselves notified when the application is running in the background? I arrived "external accessory communication" in the section on capacities in Xcode. Will it be enough to get notified when the battery level changed if the application is in the background?

Bloc de code:
 (void) viewDidLoad {

    [super viewDidLoad];
    [[UIDevice currentDevice] setBatteryMonitoringEnabled: YES];
    [[NSNotificationCenter defaultCenter] addObserver: self selector:selector (batteryChanged :) name: UIDeviceBatteryLevelDidChangeNotification object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector:selector (batteryChanged :) name: UIApplicationDidEnterBackgroundNotification object: nil];
- (Void) batteryChanged (NSNotification *) notification {
    * UIApplication application = [UIApplication sharedApplication];
    if ([[UIDevice currentDevice] respondsToSelector:selector (isMultitaskingSupported)])
        NSLog (@ "; Multitasking Supported");
        __block UIBackgroundTaskIdentifier background_task;
        background_task = [beginBackgroundTaskWithExpirationHandler app: ^ {
            background_task = UIBackgroundTaskInvalid;
        UIDevice MyDevice * = [UIDevice currentDevice];
        [MyDevice setBatteryMonitoringEnabled: YES];
        int state = [MyDevice batteryState];
        batLeft int = (float) [MyDevice BatteryLevel] * 100;
// If (batLeft <68) {
// [PlaySoundFile self];
} //
        [self playSoundFile];
        NSLog (@ "; beginBG called");
        [endBackgroundTask app: background_task];