How to indicate network activity in the status bar

I use Alamofire for networking on my iOS app. I need to run this application in iOS 7+. I want to indicate network activity in the status bar , so I created this structure:

struct ActivityManager {

    static var activitiesCount = 0

    static func addActivity() {
        if activitiesCount == 0 {
            UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        }

        activitiesCount++
    }

    static func removeActivity() {
        if activitiesCount > 0 {
            activitiesCount--

            if activitiesCount == 0 {
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            }
        }
    }

}

But I do not know where to turn in addActivity()and methods removeActivity(). I do not want to write them with every request. I want them to be called automatically with every request.

I also tried to use the net NSURLSessionand NSURLSessionTaskand expand them:

extension NSURLSessionTask {

    func resumeWithActivity() {
        ActivityManager.addAction()
        self.resume()
    }

}


public extension NSURLSession {

    func OwnDataTaskWithRequest(request: NSURLRequest!, ownCompletionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDataTask! {

        return self.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
            ActivityManager.removeAction()
            ownCompletionHandler!(data, response, error)
        })
     }

}

Then I used them as follows:

var session: NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)

session.OwnDataTaskWithRequest(request) { (data, response, error) -> Void in
    // some logic here            
}.resumeWithActivity()

But this approach does not work. In iOS 7, the extension is NSURLSessionnot displayed. I created an error report for this report (with a sample project ).

, ? Alamofire?

+4
1

Alamofire, , .

Alamofire

2 ActivityManager, Alamofire.Manager, Alamofire.Request.

AFNetworkActivityIndicatorManager, , AFNetworking.

+1

All Articles