Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> iOS

Cách phát hiện user nhấn Home button (hoặc vuốt Home indicator) trong iOS

Khi user nhấn Home button (iPhone có Home) hoặc vuốt lên Home indicator (iPhone Face ID), app chuyển sang background. Cần biết thời điểm này để: pause video, save state, stop timer, disconnect socket...

AppDelegate Lifecycle Methods

Trong AppDelegate.swift (hoặc SceneDelegate iOS 13+):

1. applicationWillResignActive

Gọi khi app sắp mất active state (cuộc gọi đến, Control Center, nhấn Home/vuốt Home indicator). App vẫn visible nhưng không nhận touch.


func applicationWillResignActive(_ application: UIApplication) {
    print("📱 App sẽ resign active - Pause tasks, throttle UI")
    // Pause timer, video, game loop
    // Save draft data
}

2. applicationDidEnterBackground

Gọi khi app hoàn toàn vào background (sau resignActive).


func applicationDidEnterBackground(_ application: UIApplication) {
    print("📱 App đã vào background - Save state, release resources")
    // Save user data, release memory, end background tasks
    // Có ~30s để hoàn tất trước khi bị suspend
}

Cách phát hiện user nhấn Home button (hoặc vuốt Home indicator) trong iOS

SceneDelegate (iOS 13+ Multi-window)

Mỗi scene (window) có lifecycle riêng:


// SceneDelegate.swift
func sceneWillResignActive(_ scene: UIScene) {
    print("Scene sẽ resign active")
}

func sceneDidEnterBackground(_ scene: UIScene) {
    print("Scene vào background")
}

Trong ViewController (NotificationCenter)

Nếu không truy cập AppDelegate, quan sát notification:


override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(appWillResignActive),
                                           name: UIApplication.willResignActiveNotification,
                                           object: nil)
    
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(appDidEnterBackground),
                                           name: UIApplication.didEnterBackgroundNotification,
                                           object: nil)
}

@objc func appWillResignActive() {
    print("VC: App sẽ resign active")
}

@objc func appDidEnterBackground() {
    print("VC: App vào background")
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

Sơ đồ lifecycle khi nhấn Home / Vuốt Home Indicator

Foreground (Active)
       ↓
applicationWillResignActive / sceneWillResignActive
       ↓ (user nhấn Home / vuốt lên)
Background (Inactive → Background)
       ↓
applicationDidEnterBackground / sceneDidEnterBackground

Phân biệt: Home Button vs Home Indicator

Thiết bị Hành động Gọi delegate
iPhone SE, 8, cũ (có Home button vật lý) Nhấn Home button 1 lần willResignActive → didEnterBackground
iPhone X trở lên (Face ID, Home indicator) Vuốt lên từ dưới cùng màn hình willResignActive → didEnterBackground
Tất cả Cuộc gọi đến / Control Center / Notification Center willResignActive (KHÔNG gọi didEnterBackground)

Lưu ý

  • Không thể detect "chính xác là nhấn Home button" - chỉ detect app chuyển state
  • Không thể ngăn chặn user thoát app (Home indicator gesture là system gesture)
  • Guided Access mode: Home button/vuốt bị disable
  • Background time limit: ~30s (iOS quyết định, có thể ít hơn)