The rest2mobile plugin for Xcode generates Objective-C code for your iOS app to interface with REST services. You can find a video here describing how to install and use it.
Go to the http://developer.magnet.com for more info about the command-line tool and Android Studio plugin.
The rest2mobile plugin for Xcode has these prerequisites:
- Java JDK 1.6 or later
- Xcode 5 or 6
Find all releases here.
Current release:
The following picture is a screenshot of the plugin:
Step 1: Install plugin The installation instructions are summarized here or simply run this script:
curl https://github.com/magnetsystems/r2m-plugin-ios/releases/download/v1.1.4/r2m-Xcode-plugin.zip -O -L
mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins
unzip -o r2m-Xcode-plugin.zip -d ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins
Step 2: (Re)start XCode Once Xcode is up, you should see the new R2M menu (on older version this menu is named "Magnet"):
Choose "Add new API" from the R2M menu (or "Magnet" menu on older version). The following describes the wizard options:
Alternatively, instead of crafting your own REST invocations, you can load existing examples from the r2m-examples github repo by clicking on the browse icon next to the Class Name
box:
STEP 3: Generate code
After filling the form, click on "Generate" to generate the code. This generates code under the r2m
directory. A Kiwi unit test file is also generated under the YourProjectNameTests directory.
For example, let's say you decided to use the GoogleDistance
built-in example. Select it from the drop-down menu, and specify a prefix (say, GG
). Click on "Generate", you should now see the controller, node, and test classes:
(here the project name is r2mdemo)
- Controller classes are generated under r2mdemo/r2m/Controllers
- Test class is generated under r2mdemoTests/r2m/Controllers
- Model classes are generated under r2mdemo/r2m/Nodes
NOTE in the rest of this tutorial, we assume that your project name is 'r2mdemo'
Step 4: Apply Podfile
You should also see an error in the debug area window if you haven't installed the R2M SDK for iOS.
In this case, copy the Podfile snippet, and close Xcode. Then go to your Xcode project folder and create a Podfile and paste the Podfile snippet into it.
It should look like this, be sure to replace r2mdemoTests
with the actual test directory name corresponding to your project ({YourProjectName}Tests)
platform :ios, '7.0'
pod 'Rest2Mobile', '~> 1.1'
target :r2mdemoTests, :exclusive => true do
pod 'Kiwi'
end
Now install the pod dependencies by running following command from your project directory:
> pod install
IMPORTANT ! you must re-open your project using the .xcworkspace file (in our case r2mdemo.xcworkspace
)
STEP 5: Test
Use the generated unit test generated under YourProjectNameTests. In this example, it will be
r2mdemo/r2mdemoTests/r2m/Controllers/GoogleDistanceSpec.m
This test is a template class, you can adapt it with the following code.
//
// GGGoogleDistanceSpec.m
//
// File generated by Magnet rest2mobile 1.1 - Dec 3, 2014 3:54:22 PM
// @See Also: http://developer.magnet.com
//
#import <Kiwi/Kiwi.h>
#import "GGGoogleDistance.h"
#import "GGGoogleDistanceResult.h"
#import "GGGoogleDistance.h"
#import "GGGoogleDistanceResult.h"
#import "GGElement.h"
#import "GGRow.h"
#import "GGDistance.h"
#define DEFAULT_TEST_TIMEOUT 5.0
SPEC_BEGIN(GGGoogleDistanceSpec)
describe(@"GGGoogleDistance", ^{
GGGoogleDistance *sut = [[GGGoogleDistance alloc] init];
context(@"when calling googleDistance", ^{
it(@"should invoke", ^{
NSString *origins = @"435 Tasso Street, Palo Alto, CA";
NSString *destinations = @"1 Embarcadero Street, San Francisco, CA";
NSString *sensor = @"false";
NSString *mode = @"bicycling";
NSString *language = @"en";
NSString *units = @"metric";
__block NSString *_distance;
[sut googleDistance:origins
destinations:destinations
sensor:sensor
mode:mode
language:language
units:units
success:^(GGGoogleDistanceResult *response){
GGRow *row = response.rows.firstObject;
GGElement *element = row.elements.firstObject;
GGDistance *distance = element.distance;
_distance = distance.text;
}
failure:^(NSError *error){
}];
// Assert
[[expectFutureValue(_distance) shouldEventuallyBeforeTimingOutAfter(DEFAULT_TEST_TIMEOUT)] equal:@"some value"];
});
});
});
SPEC_END
In this tests, we import the header files corresponding to the generated model and controller classes. The test calls the google distance REST API through the GoogleDistanceController to calculate the distance between 2 addresses in kilometers. Finally, it verifies that the distance value.
Now run the test, by clicking on Run->Test (Command+U). You'll see the test failing with the error:
[FAILED] expected subject to equal (NSString) "some value", got "55.6km"
In a true Test Driven development fashion, we made the test fail first, now replace "some value" with "55.6km"
That is, replace the line:
[[expectFutureValue(_distance) shouldEventuallyBeforeTimingOutAfter(DEFAULT_TEST_TIMEOUT)] equal:@"some value"];
with
[[expectFutureValue(_distance) shouldEventuallyBeforeTimingOutAfter(DEFAULT_TEST_TIMEOUT)] equal:@"55.6km"];
And re-run the test (Run->Test or Command+U). The test is now successful.
Congratulations! You created the Google Distance controller using Rest2Mobile! You can now create your own controller for any REST API that you want.
For v1.1.2+, run rm -r ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/R2M.xcplugin/
For earlier versions, run rm -r ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Magnet.xcplugin/
Licensed under the [Apache License, Version 2.0] license (the "License"); you may not use this software except in compliance with the License.
Copyright © 2014 Magnet Systems, Inc. All rights reserved.