Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSURLSession not working (3rd party i.e.:Alamofire neither) #4

Closed
SentoCrespo opened this issue Nov 23, 2015 · 7 comments
Closed

NSURLSession not working (3rd party i.e.:Alamofire neither) #4

SentoCrespo opened this issue Nov 23, 2015 · 7 comments

Comments

@SentoCrespo
Copy link
Contributor

It's not catching requests/responses that go through Alamofire ("Equivalent" to AFNetworking in Swift)

Most basic case:

let request = ...
Alamofire.Manager.sharedInstance.request(request).validate().responseJSON() { 
...
}

My guessing is that it's not working for any NSURLSession request, this is not working either:

let request = NSURLRequest(URL: url)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
@kasketis
Copy link
Owner

Thanks for the input my friend. 👍

I don't use Alamofire but I will check the issue soon.

The library works fine with NSURLSession
Please try this

        NFX.sharedInstance().start()
        let request = NSURLRequest(URL: NSURL(string: "https://www.google.gr")!)
        let session = NSURLSession.sharedSession()
        session.dataTaskWithRequest(request) { (data, response, error) -> Void in
            if error != nil {
                print("error: \(error)")
                
            } else {
                if ((data) != nil) {
                    print("data: \(data)")
                    
                } else {
                    print("no data")
                }
            }
        }.resume()

I keep this issue open for any further comments :)

@SentoCrespo
Copy link
Contributor Author

You're right, it works that way but not with the framework. I think it would be important to know why as this is the "default" framework for networking as it was AFNetworking but in Swift (in fact, the author is Matt as well)

Please try importing Alamofire and use it like this:

pod 'Alamofire' // In your podfile + pod install

And then wherever:

let request = NSURLRequest(URL: NSURL(string: "https://www.google.gr")!)
Alamofire.Manager.sharedInstance.request(request).validate().responseJSON() { response in
       NFX.sharedInstance().show()
}

@kasketis
Copy link
Owner

After a little research I found that Alamofire uses NSURLSession internally which does not respect the protocol classes registered using NSURLProtocol.registerClass(). Instead it uses a NSURLSessionConfiguration object which has a protocolClasses property.

I think that I must catch Alamofire's case as an exception. I will try some things and I will post a workaround soon.

@SentoCrespo
Copy link
Contributor Author

You're the man 👍

@SentoCrespo
Copy link
Contributor Author

@kasketis
Copy link
Owner

Yeap, but I think to subclass it like this

import Alamofire
class NFXManager: Alamofire.Manager
{
    static let sharedManager: NFXManager = {
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.protocolClasses?.insert(NFXProtocol.self, atIndex: 0)
        let manager = NFXManager(configuration: configuration)
        return manager
    }()
}

Then you can use NFXManager.sharedManager instead of Alamofire.Manager.

I think your opinion on this, as an Alamofire user is very valuable! So what are you think? :)

@SentoCrespo
Copy link
Contributor Author

Yep, it's working doing that subclassing. My only doubt is if it will work with some extensions (Reactive, mapping, ...) Alamofire can get pretty big but right now I intend to use it alone.

In theory, all should work as it's "the same thing" and we're just overriding the shared manager, but I leave this comment here just in case.

I don't like this solution as the best, as I have to create a class for this and inherit, but maybe it's the only way to go with Alamofire. I don't dislike it that much as well :P

You can close this issue mate.

Keep up the good stuff going!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants