Skip to content

Commit

Permalink
Merge branch 'pr/20'
Browse files Browse the repository at this point in the history
  • Loading branch information
kasketis committed Nov 25, 2015
2 parents 16c77f2 + 9bf8085 commit 8f22dc7
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 39 deletions.
Binary file modified .DS_Store
Binary file not shown.
19 changes: 0 additions & 19 deletions Alamofire.md

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ Tip: You can use the url of the host (for example "https://www.github.com") to i

#### Other

- Alamofire users check [this](https://github.com/kasketis/netfox/blob/master/Alamofire.md)
- Alamofire users check [this](https://github.com/kasketis/netfox/blob/master/Workarounds.md#alamofire-workaround)
- If you can't log request body check [this](https://github.com/kasketis/netfox/blob/master/Workarounds.md#no-http-body-for-requests)
- Due to the large size of request/response bodies, the library provides disk storage for low memory overhead

#### Licence
Expand Down
34 changes: 34 additions & 0 deletions Workarounds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#### Alamofire workaround

Due to the way Alamofire uses NSURLSession, you’ll need to do a little more than the standard installation to monitor all requests. The simplest way to do this is to create a subclass of Manager to handle your requests as this

<pre>
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
}()
}
</pre>

Then just use NFXManager.sharedManager instead of Alamofire.request()

#### No HTTP body for requests

Unfortunately, due to a limitation in NSURLProtocol, netfox is unable to log the HTTP body of some requests (check [this](http://openradar.appspot.com/15993891) radar for more details)

[jasoncabot](https://github.com/jasoncabot) posted a nice workaround on this:

You can do the following to monitor these requests
<pre>
if let bodyData = mutableURLRequest.HTTPBody {
NSURLProtocol.setProperty(bodyData, forKey: "NFXBodyData", inRequest: mutableURLRequest)
}
</pre>

Tip for Alamofire users: Subclass URLRequestConvertible and make all the work there
4 changes: 2 additions & 2 deletions netfox.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "netfox"
s.version = "0.1.6"
s.version = "0.1.8"
s.summary = "A lightweight, one line setup, iOS network debugging library!"

s.description = <<-DESC
Expand All @@ -11,7 +11,7 @@ DESC
s.screenshots = "https://raw.githubusercontent.com/kasketis/netfox/master/assets/overview.gif"
s.license = 'MIT'
s.author = "Christos Kasketis"
s.source = { :git => "https://github.com/kasketis/netfox.git", :tag => '0.1.6' }
s.source = { :git => "https://github.com/kasketis/netfox.git", :tag => '0.1.8' }

s.platform = :ios, '8.0'
s.requires_arc = true
Expand Down
11 changes: 8 additions & 3 deletions netfox/NFX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import UIKit

let nfxVersion = "0.1.6"
let nfxVersion = "0.1.8"

@objc
public class NFX: NSObject
Expand Down Expand Up @@ -112,20 +112,25 @@ public class NFX: NSObject
navigationController!.navigationBar.barTintColor = UIColor.init(netHex: 0xccc5b9)
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.init(netHex: 0xec5e28)]

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(navigationController!, animated: true, completion: { () -> Void in
presentingViewController?.presentViewController(navigationController!, animated: true, completion: { () -> Void in
self.presented = true
})


}

private var presentingViewController: UIViewController? {
let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
return rootViewController?.presentedViewController ?? rootViewController
}

private func hideNFX()
{
if !self.presented {
return
}

UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, completion: { () -> Void in
presentingViewController?.dismissViewControllerAnimated(true, completion: { () -> Void in
self.presented = false
})
}
Expand Down
4 changes: 3 additions & 1 deletion netfox/NFXHTTPModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class NFXHTTPModel: NSObject
var requestTimeout: String?
var requestHeaders: Dictionary<String, String>?
var requestBodyLength: Int?
var requestType: String?

var responseStatus: Int?
var responseType: String?
Expand All @@ -55,6 +56,7 @@ class NFXHTTPModel: NSObject
self.requestCachePolicy = request.getNFXCachePolicy()
self.requestTimeout = request.getNFXTimeout()
self.requestHeaders = request.getNFXHeaders()
self.requestType = requestHeaders?["Content-Type"]
saveRequestBodyData(request.getNFXBody())
}

Expand Down Expand Up @@ -108,7 +110,7 @@ class NFXHTTPModel: NSObject
guard let data = readRawData(getRequestBodyFilepath()) else {
return ""
}
return prettyOutput(data)
return prettyOutput(data, contentType: requestType)
}

func getResponseBody() -> NSString
Expand Down
6 changes: 1 addition & 5 deletions netfox/NFXHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ extension NSURLRequest

func getNFXBody() -> NSData
{
if (HTTPBody != nil) {
return HTTPBody!
} else {
return NSData()
}
return HTTPBody ?? NSURLProtocol.propertyForKey("NFXBodyData", inRequest: self) as? NSData ?? NSData()
}
}

Expand Down
20 changes: 12 additions & 8 deletions netfox/NFXProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ public class NFXProtocol: NSURLProtocol

override public class func canInitWithRequest(request: NSURLRequest) -> Bool
{
if (!(request.URL!.absoluteString.hasPrefix("http")) && !(request.URL!.absoluteString.hasPrefix("https"))) {
if let url = request.URL {
if (!(url.absoluteString.hasPrefix("http")) && !(url.absoluteString.hasPrefix("https"))) {
return false
}

for ignoredURL in NFX.sharedInstance().ignoredURLs {
if url.absoluteString.hasPrefix(ignoredURL) {
return false
}
}
} else {
return false
}

if NSURLProtocol.propertyForKey("NFXInternal", inRequest: request) != nil {
return false
}

for url in NFX.sharedInstance().ignoredURLs {
if request.URL!.absoluteString.hasPrefix(url) {
return false
}
}

return true
}

Expand Down

0 comments on commit 8f22dc7

Please sign in to comment.