2013-02-05 7 views
0

내 앱에서 헤드폰 잭을 모니터링하고 싶습니다. 코드가 있습니다.하지만 앱이 활성화되어있을 때만 작동합니다. 앱이 비활성 상태입니다. 그게 가능하니?앱이 비활성 상태 일 때 헤드폰 잭 모니터링

내 테스트에서는 AppDelegate에서 모니터링 코드를 입력하고 잭을 뽑으면 잭에 "NSLog"가 발생하고 다른 NSLog를 꽂으면 사용자가 내 앱보다 "전원 버튼"이 "비활성"상태이고 모니터링 코드가 작동하지 않습니다.

앱이 비활성 상태 인 경우에도 작동하는 백그라운드 작업을 만들 수 있습니까?

+0

, 당신에 대한 그 괜찮 앱? –

+0

앱이 포 그라운드로 돌아 왔을 때 헤드폰 잭의 상태를 확인하십시오. – rmaddy

+0

답변 해 주셔서 감사합니다. @ FabianKreiser가 작동 할 수도 있지만 "배경 작업"에 대해서는 잘 모릅니다. 헤드폰 잭을 뽑을 때마다 알림을 시작해야하며 백그라운드 작업이 가능합니까? –

답변

0

앱이 백그라운드 작업을 사용하여 백그라운드로 들어간 후 최대 10 분 동안 앱을 실행할 수 있습니다. iOS 앱 프로그래밍 가이드의 Background Execution and Multitasking 섹션을 살펴보세요.

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{ 
     // clean up any unfinished task business, your app is going to be killed if you don't end the background task now 

     [application endBackgroundTask:backgroundTask]; 
     backgroundTask = UIBackgroundTaskInvalid; 
    }]; 

    // start the long-running task and return immediately. 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     // Monitor the head phone jack here. 
     // If this happens asynchronously, you don't need to dispatch this block, your app will continue to run as normal, only modifying or accessing its UI while it's in background mode is prohibited. 

     // End the background task if you're done. If this is never the case, your expiration handler will be called after ten minutes. 
     [application endBackgroundTask:backgroundTask]; 
     backgroundTask = UIBackgroundTaskInvalid; 
    }); 
} 

당신은 전송하여 사용자에게 통지 할 수있는 UILocalNotification :

- (void)notifyUserWhilePerformingBackgroundTask 
{ 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.alertBody = @"Headphone removed!"; 

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; 
} 
당신은 그것을 위해 백그라운드 작업을 사용할 수 있지만, 어떤 배경 작업이 최대 10 분 후 종료됩니다
+0

대단히 고맙습니다. 시도해 보겠습니다. "미완성 된 과제 비즈니스를 정리하는 부분 만 이해하지 못합니다. 이제 백그라운드 작업을 끝내지 않으면 앱이 죽을 것입니다 ..." "배경 작업"? 그 당시 앱에 배경 작업이 없다면 –

+0

그것은 만료 처리기 블록 안에 있습니다. 그 블록은 10 분이 지나고 배경 작업을 아직 끝내지 않은 채로 호출됩니다. –