Meonardo
Meonardo

Reputation: 302

How to detect I am using the Internet on iOS?

First, I am not asking internet reachability.
I have a lot internet requests in different places in my App. I want to show the ActivityIndicator on status bar, but I don't want to set

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES/NO

every time.
Is there a way I can detect my App is using the Internet?(Hook Methods or something?)
Thanks a lot!

Upvotes: 0

Views: 74

Answers (2)

NSDeveloper
NSDeveloper

Reputation: 1640

If you use AFNetworking, then AFNetworkActivityIndicatorManager is a good choice. You can implement similar function like that.

Upvotes: 0

Tommy
Tommy

Reputation: 100652

Sadly — unless I'm behind the times — it's your own responsibility to keep track of this; there's no getter for quantity of network communications in flight and neither is there a notification or object you could observe to watch for changes.

Supposing you were just using NSURLSession, you might implement a function, YAURLCompletionForCompletion that takes a block of the form void (^)(NSURL *location, NSURLResponse *response, NSError *error) as a parameter and that increments a global activity count and returns another block of the same form which calls the original and then decrements the counter.

Display the spinner whenever the counter is non-zero. Always use YAURLCompletionForCompletion to wrap whatever completion handlers you pass when creating NSURLSessionDataTasks.

Upvotes: 1

Related Questions