source

iOS 앱이 백그라운드에 있는지 확인할 수 있는 방법이 있나요?

nicesource 2023. 4. 9. 21:42
반응형

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그리고.backgroundApple 의 메뉴얼은, 여기를 참조해 주세요.

쉐이크엘 아흐메드 덕분에 스위프트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

반응형