diff --git a/.travis.yml b/.travis.yml index 98e7129..4cd0583 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: objective-c +osx_image: xcode7 xcode_workspace: Example/VOKMockUrlProtocol.xcworkspace xcode_scheme: VOKMockUrlProtocol xcode_sdk: iphonesimulator diff --git a/README.md b/README.md index 09c3bdf..79834fd 100644 --- a/README.md +++ b/README.md @@ -82,3 +82,7 @@ In order to switch back and forth between mock and live, you can also take out t [currentProtocolClasses removeObject:mockURLProtocol]; self.sessionConfiguration.protocolClasses = currentProtocolClasses; ``` + +### Using with Frameworks/Swift + +When `VOKMockURLProtocol` is built as a framework (usually for use with Swift), make sure to call the `setTestBundle:` class method and pass in your test bundle. Since the default behavior is to fall back to the bundle for the current class, that would look in the Framework's bundle rather than the test bundle, and nothing would work. diff --git a/VOKMockUrlProtocol.h b/VOKMockUrlProtocol.h index ac387ba..3bfba73 100644 --- a/VOKMockUrlProtocol.h +++ b/VOKMockUrlProtocol.h @@ -12,4 +12,12 @@ */ @interface VOKMockUrlProtocol : NSURLProtocol +/** + * Sets a custom test bundle to use to look for the mock data files. Primarily useful + * when VOKMockUrlProtocol is being used as a framework (for example, with Swift). + * + * @param bundle The test bundle to use, or nil to reset to the bundle VOKMockUrlProtocol is in. + */ ++ (void)setTestBundle:(NSBundle *)bundle; + @end diff --git a/VOKMockUrlProtocol.m b/VOKMockUrlProtocol.m index b43324d..185f81e 100644 --- a/VOKMockUrlProtocol.m +++ b/VOKMockUrlProtocol.m @@ -49,6 +49,12 @@ + (instancetype)containerWithResponse:(NSHTTPURLResponse *)response data:(NSData @implementation VOKMockUrlProtocol +static NSBundle *testBundle = nil; ++ (void)setTestBundle:(NSBundle *)bundle +{ + testBundle = bundle; +} + + (BOOL)canInitWithRequest:(NSURLRequest *)request { return YES; @@ -312,9 +318,13 @@ - (VOKMockUrlProtocolResponseAndDataContainer *)responseAndData // First, look for a complete-HTTP-response file. for (NSString *resourceName in resourceNames) { - filePath = [[NSBundle bundleForClass:[self class]] pathForResource:resourceName - ofType:@"http" - inDirectory:MockDataDirectory]; + if (!testBundle) { + testBundle = [NSBundle bundleForClass:[self class]]; + } + + filePath = [testBundle pathForResource:resourceName + ofType:@"http" + inDirectory:MockDataDirectory]; NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]; diff --git a/VOKMockUrlProtocol.podspec b/VOKMockUrlProtocol.podspec index ecf7e9c..3c95d22 100644 --- a/VOKMockUrlProtocol.podspec +++ b/VOKMockUrlProtocol.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "VOKMockUrlProtocol" - s.version = "2.0.4" + s.version = "2.1.0" s.summary = "A url protocol that parses and returns fake responses with mock data." s.homepage = "https://github.com/vokal/VOKMockUrlProtocol" s.license = { :type => "MIT", :file => "LICENSE"}