This is a class to parse JSONP responses with AFNetworking. It uses Javascript contexts provided by the JavaScriptCore framework.
#import "AFNetworking.h"
#import "CDLJSONPResponseSerializer.h"
NSString *urlString = @"http://derp.example/";
NSString *callback = @"foo";
NSDictionary *parameters = @{@"callback": callback};
NSURLRequest *request =
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET"
URLString:urlString
parameters:parameters];
AFHTTPRequestOperation *op =
[[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer =
[CDLJSONPResponseSerializer serializerWithCallback:callback];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
-
Xcode 5.0
-
Target of iOS 7.0 or OS X 10.9
-
AFNetworking 2.0
-
ARC enabled
-
JavaScriptCore framework
Installation with CocoaPods
Add this line to your Podfile:
pod "CDLJSONPResponseSerializer", "~> 0.9"
In the Tests/
directory, run pod install
to set up the workspace. Then you
can open the workspace and run tests in the Tests
project.
To run the tests from the command line, install xctool with Homebrew:
$ brew update
$ brew install xctool --HEAD
Then, run rake test
.
Because JSONP responses are executed in Javascript contexts, a malicious response could cause very large memory or CPU usage. Your app could quit or lock up. This is also true of plain JSON, but JSONP can do it with only a small amount of data, for example:
while(1); // The response will use 100% CPU and never complete.
Created by Chris Lundie.
See the LICENSE file.