How keep NSTimer when application entering background?

后端 未结 3 1955
执笔经年
执笔经年 2020-12-15 11:53

I\'m here because a didn\'t find any solutions for my issue :(

I\'m doing an simple application in which i have to send (by socket) some informations to a server (li

相关标签:
3条回答
  • 2020-12-15 11:59

    From the Apple docs:

    Implementing Long-Running Background Tasks For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

    • Apps that play audible content to the user while in the background, such as a music player app
    • Apps that keep users informed of their location at all times, such as a navigation app
    • Apps that support Voice over Internet Protocol (VoIP)
    • Newsstand apps that need to download and process new content
    • Apps that receive regular updates from external accessories

    Unless your app falls into one of these categories (and it sounds like it doesn't), then it won't be able to run while in the background.

    0 讨论(0)
  • 2020-12-15 12:23

    The iOS App Programming Guide lists the activities for which your app is allowed to remain active in the background. If you plan on distributing your app through the app store, your app will have to perform one of the listed activities. If you're just writing something for yourself, you might be able to piggyback your functionality on one of the allowed activities.

    0 讨论(0)
  • 2020-12-15 12:25

    You need to read the guide on how to run tasks in the background:

    Background Execution and Multitasking

    Here is my applicationDidEnterBackground for one of my apps. When I put it to the background, it does some disk cache maintenance:

    - (void)applicationDidEnterBackground:(UIApplication *)application {
    
    //As we are going into the background, I want to start a background task to clean up the disk caches
    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
    
            __block UIBackgroundTaskIdentifier background_task; //Create a task object
    
            background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
                [application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
                //System will be shutting down the app at any point in time now
            }];
    
            //Background tasks require you to use asyncrous tasks
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //Perform your tasks that your application requires                
    
                //I do what i need to do here.... synchronously...                
    
                [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
                background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
            });
        }
    }
    

    }

    0 讨论(0)
提交回复
热议问题