2010-10-04 31 views
26

Làm cách nào để phát hiện thấy ứng dụng vừa mới trở về từ "chế độ nền"? Ý tôi là, tôi không muốn ứng dụng của tôi tìm nạp dữ liệu (mỗi 60 giây) khi người dùng nhấn nút "home". Nhưng, tôi muốn thực hiện một số cập nhật "đặc biệt" lần đầu tiên ứng dụng ở chế độ nền trước.iphone 4 sdk: phát hiện trở về từ chế độ nền

Làm thế nào tôi có thể phát hiện hai sự kiện này:

  1. ứng dụng sẽ sang chế độ nền
  2. ứng dụng sẽ sang chế độ foreground

Cảm ơn trước.

François

Trả lời

48

Dưới đây là làm thế nào để lắng nghe cho các sự kiện như:

// Register for notification when the app shuts down 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillTerminateNotification object:nil]; 

// On iOS 4.0+ only, listen for background notification 
if(&UIApplicationDidEnterBackgroundNotification != nil) 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationDidEnterBackgroundNotification object:nil]; 
} 

// On iOS 4.0+ only, listen for foreground notification 
if(&UIApplicationWillEnterForegroundNotification != nil) 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFunc) name:UIApplicationWillEnterForegroundNotification object:nil]; 
} 

Lưu ý: Các if(&SomeSymbol) kiểm tra đảm bảo rằng mã của bạn sẽ làm việc trên iOS 4.0+ và cũng trên iOS 3.x - nếu bạn xây dựng so với iOS 4.x hoặc 5.x SDK và đặt mục tiêu triển khai thành iOS 3.x, ứng dụng của bạn vẫn có thể chạy trên thiết bị 3.x nhưng địa chỉ của các biểu tượng có liên quan sẽ là 0 và do đó sẽ không hỏi cho các thông báo không tồn tại trên các thiết bị 3.x (sẽ làm hỏng ứng dụng).

Cập nhật: Trong trường hợp này, các if(&Symbol) kiểm tra hiện nay là không cần thiết (trừ khi bạn thực sự cần hỗ trợ iOS 3 đối với một số lý do). Tuy nhiên, thật hữu ích khi biết kỹ thuật này để kiểm tra xem một API có tồn tại hay không trước khi sử dụng nó. Tôi thích kỹ thuật này hơn là kiểm tra phiên bản hệ điều hành vì bạn đang kiểm tra xem API cụ thể có hiện diện thay vì sử dụng kiến ​​thức bên ngoài về những API nào có trong phiên bản hệ điều hành nào không.

+0

cảm ơn vì câu trả lời nhanh! –

5

Nếu bạn thực hiện một UIApplicationDelegate, bạn cũng có thể móc vào các chức năng như một phần của các đại biểu:

- (void)applicationDidEnterBackground:(UIApplication *)application { 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
If your application supports background execution, called instead of applicationWillTerminate: when the user quits. 
    */ 
    NSLog(@"Application moving to background"); 
} 


- (void)applicationWillEnterForeground:(UIApplication *)application { 
    /* 
    Called as part of the transition from the background to the active state: here you can undo many of the changes made on entering the background. 
    */ 
    NSLog(@"Application going active"); 
} 

Đối với các tài liệu tham khảo Nghị định thư thấy http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Các vấn đề liên quan