2013-07-11 7 views
6

आईओएसआवेदन को कम करने के बाद हम विधि को कॉल कर सकते हैं?

क्या आवेदन को कम करने के बाद हम विधि को कॉल कर सकते हैं?

उदाहरण के लिए, 5 सेकंड बाद applicationDidEnterBackground: कहा जाता था।

मैं इस कोड का उपयोग, लेकिन test विधि कॉल नहीं है

- (void)test 
{ 
    printf("Test called!"); 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    [self performSelector:@selector(test) withObject:nil afterDelay:5.0]; 
} 
+0

** AppDelegate.m फ़ाइल में अपने तरीके ** 'applicationWillResignActive' और उसके बाद' applicationDidEnterBackground' कहा जाता है। – Rohan

+0

धन्यवाद। लेकिन मेरा मतलब है, जब यह पृष्ठभूमि में है – Rubinc

+0

पर एक नज़र डालें [मैं आईफोन होम स्क्रीन प्रोग्रामशैली के स्क्रीनशॉट कैसे ले सकता हूं] (http://stackoverflow.com/q/13459682/593709) –

उत्तर

6

आप पृष्ठभूमि कार्य एपीआई के बाद आप backgrounded किया गया है (जब तक उपयोग कर सकते हैं एक विधि कॉल करने के रूप में अपने काम के लिए नहीं ले करता है बहुत लंबा - आमतौर पर ~ 10 मिनट अधिकतम अनुमत समय है)।

आईओएस पृष्ठभूमि के समय टाइमर आग नहीं देता है, इसलिए मुझे पता चला है कि ऐप पृष्ठभूमि से पहले पृष्ठभूमि थ्रेड भेज रहा है, फिर उस थ्रेड को सोने के लिए डालने पर, टाइमर के समान प्रभाव पड़ता है।

अपने अनुप्रयोग प्रतिनिधि के - (void)applicationWillResignActive:(UIApplication *)application विधि में निम्नलिखित कोड डालें:

// Dispatch to a background queue 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

    // Tell the system that you want to start a background task 
    UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ 
     // Cleanup before system kills the app 
    }]; 

    // Sleep the block for 5 seconds 
    [NSThread sleepForTimeInterval:5.0]; 

    // Call the method if the app is backgrounded (and not just inactive) 
    if (application.applicationState == UIApplicationStateBackground) 
     [self performSelector:@selector(test)]; // Or, you could just call [self test]; here 

    // Tell the system that the task has ended. 
    if (taskID != UIBackgroundTaskInvalid) { 
     [[UIApplication sharedApplication] endBackgroundTask:taskID]; 
    } 

}); 
+0

विन्नी, यह मदद है! धन्यवाद! – Rubinc

+0

आपका स्वागत है! –

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

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