2014-03-12 2 views
-1

저는 iOS 개발의 초보자입니다. 저는 현재 대학 프로젝트에 배터리 유틸리티 앱을 개발 중입니다. 내 설정 열에는 배터리가 완전히 충전되었을 때 알림을 수신할지 여부를 사용자가 켜거나 끄도록 할 수있는 옵션이 있습니다. 스위치가 켜져있을 때 로컬 알림을받는 문제가 있습니다. 배터리 수준 == 1.0 일 때 스위치를 끄고 다시 켜려고 할 때만 알림을 볼 수 있습니다. 누군가가 나를 도울 수 있다면 정말 고맙습니다. (무효)가 notifyFullyCharged {UISwitch가 켜져있을 때 지역 알림을받는 방법?

float batterylevel = [UIDevice currentDevice].batteryLevel; 

if(batterylevel == 1.0){ 

    // Schedule the notification 
    UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
    //localNotification.fireDate = pickerDate; 
    localNotification.alertBody = @"Unplug your device. Your battery has been fully charged."; 
    localNotification.alertAction = @"Slide to view"; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 

}

답변

0
- {

if([self.notifyFullyChargedSwitch isOn]) 
{ 
    float batterylevel = [UIDevice currentDevice].batteryLevel; 

    [UIDevice currentDevice].batteryMonitoringEnabled = YES; 

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 

    [self notifyFullyCharged]; 
} 

}

notifySwitched (무효) -

: :) 여기

내 코드입니다

코드는 실제로 스위치를 처음으로 켰을 때만 옵저버를 추가합니다. 다음 코드를 초기화 메서드 예제로 이동합니다. - ViewDownload, 스위치 상태에 관계없이 관찰자를 추가해야합니다.

[UIDevice currentDevice].batteryMonitoringEnabled = YES; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 

그런 다음 batteryLevelChanged 메서드에서 경고를 표시하기 전에 스위치 상태 조건을 확인해야합니다.

희망이 도움이됩니다.

+0

답변 해 주셔서 감사합니다. 이제 앱으로 돌아올 때만 알림보기를 표시하여 알림을 받았습니다. 그러나 응용 프로그램이 비활성 상태 일 때 알림 메시지를 표시 할 수 없으며 알림 센터에서 알림을받지 못했습니다. 이는 전화가 잠겨있을 때를 의미합니다. :( – user3378060

관련 문제