IOS achievement notification in the background

I want to perform some tasks as soon as the user goes online, even if he is in the background. I use the Reachability class to test the Internet. But this class does not notify me when I am in the background. I know that people have asked this question before, but he had no solutions. If I use beginBackgroundTaskWithExpirationHandler. This gives me only 3-4 minutes, after that, if the network changes, I do not receive a notification. Please offer me something. I think this is possible because the regular mail application works this way, and my application only supports iOS7.

+4
source share
3 answers

You cannot receive notification of your classes when the application is in the background, you can use the new features to receive iOS 7. Here is a tutorial http://www.objc.io/issue-5/multitasking.html

+2
source

It is impossible to accomplish exactly what you want. You cannot base your assumptions on Apple applications, as Apple has access to processes / APIs that you do not have. Mail works like a daemon process on a device, and your application will never do this.

iOS 7. , , - .

+3

. :

Project settings -> Capabilities -> BackgroundMode -> Background Fetch

, , , application:didFinishLaunchingWithOptions:, :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}

UIApplicationBackgroundFetchIntervalMinimum - smallest sampling interval supported by the system.

And then, when the background fetch is started, you can check AppDelegateusing the method:

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];

    switch (status) {
        case NotReachable: {
            NSLog(@"no internet connection");
            break;
        }
        case ReachableViaWiFi: {
            NSLog(@"WiFi");
            break;
        }
        case ReachableViaWWAN: {
            NSLog(@"cellular");
            break;
        }
    }
    completionHandler(YES);
}

Support iOS 7.0 +

0
source

Source: https://habr.com/ru/post/1530244/


All Articles