반응형
iOS 앱이 백그라운드에 있는지 확인할 수 있는 방법이 있나요?
앱이 백그라운드에서 실행되고 있는지 확인하고 싶습니다.
입력:
locationManagerDidUpdateLocation {
if(app is runing in background){
do this
}
}
App 위임자가 상태 전환을 나타내는 콜백을 가져옵니다.그것을 바탕으로 추적할 수 있습니다.
또한 UIApplication의 applicationState 속성은 현재 상태를 반환합니다.
[[UIApplication sharedApplication] applicationState]
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
//Do checking here.
}
이것은 문제를 해결하는 데 도움이 될 수 있습니다.
아래의 코멘트를 참조해 주세요.비액티브는 매우 특별한 케이스이며, 앱이 포그라운드로 기동하고 있는 것을 의미할 수 있습니다.목표에 따라 '배경'을 의미할 수도 있고 의미하지 않을 수도 있습니다.
스위프트 3
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
}
Swift 버전:
let state = UIApplication.shared.applicationState
if state == .Background {
print("App in Background")
}
스위프트 5
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
//MARK: - if you want to perform come action when app in background this will execute
//Handel you code here
}
else if state == .foreground{
//MARK: - if you want to perform come action when app in foreground this will execute
//Handel you code here
}
응용 프로그램 상태에 대해 "문의"하는 대신 콜백을 수신하고 싶은 경우 다음 두 가지 방법을 사용하여AppDelegate
:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"app is actvie now");
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"app is not actvie now");
}
스위프트 4 이상
let appstate = UIApplication.shared.applicationState
switch appstate {
case .active:
print("the app is in active state")
case .background:
print("the app is in background state")
case .inactive:
print("the app is in inactive state")
default:
print("the default state")
break
}
Swift 4.0 확장으로 접근이 용이함:
import UIKit
extension UIApplication {
var isBackground: Bool {
return UIApplication.shared.applicationState == .background
}
}
앱 내에서 액세스하려면:
let myAppIsInBackground = UIApplication.shared.isBackground
다양한 상태에 대한 정보를 찾고 있는 경우(active
,inactive
그리고.background
Apple 의 메뉴얼은, 여기를 참조해 주세요.
쉐이크엘 아흐메드 덕분에 스위프트5에서 제가 할 수 있었던 일은
switch UIApplication.shared.applicationState {
case .active:
print("App is active")
case .inactive:
print("App is inactive")
case .background:
print("App is in background")
default:
return
}
누군가에게 도움이 되었으면 좋겠어=)
언급URL : https://stackoverflow.com/questions/5835806/is-there-any-way-to-check-if-ios-app-is-in-background
반응형
'source' 카테고리의 다른 글
NSMutable Array를 셔플하는 가장 좋은 방법은 무엇입니까? (0) | 2023.04.09 |
---|---|
Swift Language 추상 함수 (0) | 2023.04.09 |
왜 환경인가?Exit()는 더 이상 프로그램을 종료하지 않습니까? (0) | 2023.04.09 |
Bash 쉘 스크립트에서 1~10 사이의 난수 생성 (0) | 2023.04.09 |
작업 대기 또는 예외 속성에 액세스하여 작업 예외가 관찰되지 않았습니다.그 결과 관찰되지 않은 예외는 (0) | 2023.04.09 |