Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Request sent with NSURLSession doesn't get recordet #5

Open
chrs1885 opened this issue May 3, 2016 · 7 comments
Open

Request sent with NSURLSession doesn't get recordet #5

chrs1885 opened this issue May 3, 2016 · 7 comments

Comments

@chrs1885
Copy link

chrs1885 commented May 3, 2016

Hi,

I'm currently trying out your demo project. While this is working without any problem, requests sent with NSURLSession don't get recorded. To get a repro, simply replace

@IBAction func goButtonTapped(sender: AnyObject) {
    if let urlString = urlTextField.text, url = NSURL(string: urlString){
        browseWebView.loadRequest(NSURLRequest(URL: url))
    }
}

with

@IBAction func goButtonTapped(sender: AnyObject) {
    let request = NSMutableURLRequest(URL: NSURL(string: "http://www.google.com/")!)
    httpGet(request) {
        (resultString, error) -> Void in
        print(resultString)
    }
}

func httpGet(request: NSMutableURLRequest!, callback: (String,
    String?) -> Void) {
    let configuration =
        NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: configuration,
                               delegate: nil,
                               delegateQueue:NSOperationQueue.mainQueue())
    let task = session.dataTaskWithRequest(request){
        (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
        callback(response.debugDescription, error?.localizedDescription)
    }
    task.resume()
}

in the ViewController class of the demo project.

Any idea? Thanks for your help.

@chrs1885
Copy link
Author

chrs1885 commented May 4, 2016

BTW, here's my setup code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    
    if let sharedRecorder = SWHttpTrafficRecorder.sharedRecorder() {
        sharedRecorder.recordingFormat = SWHTTPTrafficRecordingFormat.BodyOnly
        sharedRecorder.progressDelegate = self
        let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) as [String]
        try! sharedRecorder.startRecordingAtPath(paths[0], forSessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
    }
    return true
}

I'm currently using 1.0.1 of your library.

Thank you!

@Ashton-W
Copy link
Contributor

Ashton-W commented May 6, 2016

NSURLSessionConfiguration.defaultSessionConfiguration() creates a new session configuration every time you call it, it is not an accessor for a shared configuration.

@Ashton-W
Copy link
Contributor

Ashton-W commented May 6, 2016

We should consider carefully swizzling NSURLSession to enable recording of all NSURLSessions.
eg: https://github.com/AliSoftware/OHHTTPStubs/blob/master/OHHTTPStubs/Sources/NSURLSession/OHHTTPStubs%2BNSURLSessionConfiguration.m

@JinlianWang
Copy link
Contributor

@chrs1885 @Ashton-W Can you save the session configuration instance of the http calls and pass it to recorder? I can also swizzle NSURLSession as suggested, though it does not give precise control of which http calls to be recorded.

@chrs1885
Copy link
Author

chrs1885 commented May 9, 2016

Hi,

thank you for the info. you are right about the default session configuration. didn't check the docs and the naming was a bit missleading. unfortunately, I can't access the session configuration, since I'm trying to record the traffic produced by several 3rd party libraries (they don't expose the config instances). swizzling NSURLSession sounds like a valid option for my usecase and is still better than forking 3 repos just to expose the config.

@JinlianWang
Copy link
Contributor

@chrs1885 You convinced me. One way to fix this is to change the implementation for 'SWHttpTrafficRecorder.sharedRecorder().startRecording()' to swizzle NSURLSession to enable recording of all NSURLSessions as suggested by @Ashton-W previously. And still keep the other interface with session configuration as one of the parameters so that users can control to record one particular session when needed. With this said, I am on some other projects now and may not get time to implement this recently. Is it something you are willing to take on and submit a PR?

@Amindv1
Copy link

Amindv1 commented Dec 9, 2016

It would be really useful if you could implement this. I'm not too sure on what you mean by 'swizzle'. I'm having the same issue as OP where I don't have access to the session configuration.

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

No branches or pull requests

4 participants