programing

백그라운드 또는 포그라운드에서 응용 프로그램을 신속하게 탐지

lastcode 2023. 8. 15. 11:08
반응형

백그라운드 또는 포그라운드에서 응용 프로그램을 신속하게 탐지

백그라운드 모드인지 포그라운드인지 내 응용 프로그램의 상태를 알 수 있는 방법이 있습니까?감사해요.

[UIApplication sharedApplication].applicationState다음과 같은 응용 프로그램의 현재 상태를 반환합니다.

  • UIA 응용 프로그램 상태 활성
  • UIA 응용 프로그램 상태 비활성
  • UIA 응용 프로그램 상태 배경

또는 알림을 통해 액세스하려면 UIApplicationDid Beat ActiveNotification을 참조하십시오.

스위프트 3+

let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
    // background
} else if state == .active {
    // foreground
}

switch UIApplication.shared.applicationState {
    case .background, .inactive:
        // background
    case .active:
        // foreground
    default:
        break
}

목표 C

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
    // background
} else if (state == UIApplicationStateActive) {
    // foreground
}

스위프트 3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

                // background
            }
            else if state == .active {

                // foreground
            }

에서 이러한 관찰자 사용viewDidload당신의UIViewController:

let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

및 방법:

@objc func appMovedToBackground() {    
}

@objc func appMovedToForeground() {
}

스위프트 4

let state = UIApplication.shared.applicationState
        if state == .background {
            print("App in Background")
        }else if state == .active {
            print("App in Foreground or Active")
        }

누군가가 swift 3.0으로 그것을 원한다면,

switch application.applicationState {
    case .active:
        //app is currently active, can update badges count here
        break
    case .inactive:
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
        break
    case .background:
        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
        break
    default:
        break
    }

신속한 4를 위하여.

switch UIApplication.shared.applicationState {
case .active:
    //app is currently active, can update badges count here
    break
case .inactive:
    //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    break
case .background:
    //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    break
default:
    break
}

응용프로그램이 백그라운드로 입력되거나 포그라운드로 입력될 때 부울을 추가할 수 있습니다.앱 대리인을 사용하면 이 정보를 얻을 수 있습니다.

Apple 설명서에 따르면 응용 프로그램의 기본 Windows 속성이나 응용 프로그램의 활성 상태 속성을 사용할 수도 있습니다.

NSA 응용 프로그램 설명서

토론 앱의 스토리보드 또는 닙 파일 로드가 아직 완료되지 않은 경우 이 속성의 값은 0입니다.또한 앱이 비활성화되거나 숨겨져 있을 때는 0일 수 있습니다.

언급URL : https://stackoverflow.com/questions/38971994/detect-if-the-application-in-background-or-foreground-in-swift

반응형