简体   繁体   中英

AFNetworking reachability listener across entire app

I need to have offline capability in my app, so I allow the user to create certain objects to offline if there is no connection. Then when there is a connection again I upload the data to my server.

Currently I just check for reachability in applicationDidBecomeActive and then upload the data, but I would prefer to also know when my app is back online so that I can upload it then as well.

Using AFNetworking 2 , is it possible to have a "global" reachability monitor or notification, so that I can know when the app is back online so I can upload?

Add the AFNetworkReachabilityManager to your app delegate, and use following:

[AFNetworkReachabilityManager.sharedManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
{
    switch (status)
    {
        case AFNetworkReachabilityStatusNotReachable:
            DDLogDebug(@"Not reachable");
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
             AFNetworkReachabilityStatusReachableViaWWAN:
            DDLogDebug(@"Reachable");
            break;
        default:
            DDLogDebug(@"Unknown status!");
            break;
    }
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM