An example to demonstrate the use of AFNetworking with RubyMotion.
git clone https://github.com/ericdagenais/AFNetworking-RubyMotion-Example.git
cd AFNetworking-RubyMotion-Example
Install both the 'cocoapods' and 'motion-cocoapods' gems via bundler (see this article for more info). Gemfile installs specific versions to workaround an incompatibility with the latest Cocoapods gem.
# gem install bundler # install bundler if you don't have it already
bundle install
Run the example
rake
The Objective-C examples for AFNetworking often pass in nil
as callback blocks. In RubyMotion, passing in nil to these callbacks causes a crash if the callback ever needs to be called. To avoid crashing, pass in an empty proc with the correct number of parameters.
For example, passing in nil
to failure:
below works in Objective-C but will cause a crash in RubyMotion if the request fails:
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Public Timeline: %@", JSON);
} failure:nil];
[operation start];
Therefore, the above should be written as the following to avoid problems:
url = NSURL.URLWithString("http://api.twitter.com/1/statuses/public_timeline.json")
request = NSURLRequest.requestWithURL(url)
operation = AFJSONRequestOperation.JSONRequestOperationWithRequest(request, success:lambda {
|request, response, json|
NSLog("Public Timeline: %@", json)
}, failure:lambda {
|request, response, error, json| # correct number of parameters is important
})
operation.start
- XCode 4.x w/ iOS SDK 5.x
- RubyMotion
- CocoaPods
Interested in RestKit instead of AFNetworking? Checkout RestKitTest.