2010-10-04 20 views
26

मैं कैसे पता लगा सकता हूं कि एक ऐप अभी "पृष्ठभूमि मोड" से वापस आया है? मेरा मतलब है, मैं नहीं चाहता कि मेरा ऐप डेटा (प्रत्येक 60 सेकंड) प्राप्त करे जब उपयोगकर्ता "होम बटन" दबाए। लेकिन, मैं पहली बार ऐप अग्रभूमि मोड में कुछ "विशेष" अपडेट करना चाहता हूं।आईफोन 4 एसडीके: पृष्ठभूमि मोड से वापसी का पता लगाएं

मैं इन दो घटनाओं कैसे पता लगा सकते हैं:

  1. एप्लिकेशन पहले से अग्रभूमि मोड के लिए जा रहा

धन्यवाद

  • एप्लिकेशन पृष्ठभूमि मोड के लिए जा रहा।

    // 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]; 
    } 
    

    नोट::

    फ़्राँस्वा

  • उत्तर

    48

    यहाँ कैसे इस तरह की घटनाओं के लिए सुनने के लिए है if(&SomeSymbol) चेकों सुनिश्चित करें कि आपका कोड iOS पर 4.0+ और भी आईओएस 3.x पर काम करेंगे - अगर आप का निर्माण आईओएस 4.x या 5.x एसडीके के खिलाफ और आईओएस 3.x पर तैनाती लक्ष्य सेट करें, आपका ऐप अभी भी 3.x उपकरणों पर चल सकता है लेकिन प्रासंगिक प्रतीकों का पता शून्य होगा, और इसलिए यह पूछने की कोशिश नहीं करेगा अधिसूचनाओं के लिए जो 3.x डिवाइस पर मौजूद नहीं हैं (जो ऐप को क्रैश करेगा)।

    अद्यतन: इस मामले में, if(&Symbol) चेकों अब बेमानी हैं (जब तक आप वास्तव में किसी कारण से आईओएस 3 का समर्थन करने की जरूरत है)। हालांकि, यह जांचने के लिए एक एपीआई मौजूद है या नहीं, यह जांचने के लिए इस तकनीक को जानना उपयोगी है। मैं ओएस संस्करण का परीक्षण करने से इस तकनीक को पसंद करता हूं क्योंकि आप यह जांच रहे हैं कि ओएस संस्करणों में कौन से एपीआई मौजूद हैं, इसके बाहरी ज्ञान का उपयोग करने के बजाय विशिष्ट एपीआई मौजूद है या नहीं।

    +0

    धन्यवाद देखो! –

    5

    आप एक UIApplicationDelegate लागू हैं, तो आप भी कार्यों में प्रतिनिधि के रूप में भाग हुक कर सकते हैं:

    - (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"); 
    } 
    

    प्रोटोकॉल संदर्भ के लिए त्वरित जवाब के लिए http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

    संबंधित मुद्दे