반응형
백그라운드 또는 포그라운드에서 응용 프로그램을 신속하게 탐지
백그라운드 모드인지 포그라운드인지 내 응용 프로그램의 상태를 알 수 있는 방법이 있습니까?감사해요.
[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 속성이나 응용 프로그램의 활성 상태 속성을 사용할 수도 있습니다.
토론 앱의 스토리보드 또는 닙 파일 로드가 아직 완료되지 않은 경우 이 속성의 값은 0입니다.또한 앱이 비활성화되거나 숨겨져 있을 때는 0일 수 있습니다.
언급URL : https://stackoverflow.com/questions/38971994/detect-if-the-application-in-background-or-foreground-in-swift
반응형
'programing' 카테고리의 다른 글
mysqld는 간단한 쿼리를 실행할 때 신호 6을 받았습니다. (0) | 2023.08.15 |
---|---|
시스템 호출 소스 코드는 어디에서 찾을 수 있습니까? (0) | 2023.08.15 |
모범 사례 - 문자열의 첫 번째 문자를 소문자로 변환 (0) | 2023.08.15 |
JQuery 또는 JavaScript:앵커 태그 하이퍼링크를 클릭하는 동안 시프트 키를 눌렀는지 여부를 어떻게 확인합니까? (0) | 2023.08.15 |
promise.race() 사용법 이해 (0) | 2023.08.10 |