本文转载至 http://www.cnblogs.com/visen-0/archive/2012/05/03/2480693.html
最近在iPhone工程中添加RestKit并编译,但是由于之前找了很多不靠谱的说明文档,导致编译了一天也没有通过编译,总报出莫名其妙的错误。终于在最后的关头找了一篇英文的较为权威的文档才发现自己的问题出在一个很细节的地方。结论就是:不靠谱的文档害死人。
下面就总结一下怎么在xcode项目中使用Restkit。
1. 下载RestKit源码,到官网去下,下载后解压源码,不做过多解释;
2. 在xcode中建立一个iOS项目,并在项目的文件夹中复制一份RestKit源码
3. 将RestKit中的RestKit.xcodeproj文件拖动到xcode的资源管理器中
4. 选择最顶层的工程,然后选择中间栏PROJECT中的那个项目,进行设置
(1)找到Build Setting中的Header Search Path,然后设置其值为"$(SOURCE_ROOT)/RestKit/Build"
(2)找到Build Setting中的Library Search Path,然后设置其值为"$(SOURCE_ROOT)/RestKit/Build/$(BUILD_STYLE)-$(PLATFORM_NAME)"
5. 选择中间栏TARGET中的一项,然后点击Build Phase选项卡,在Target Dependence中添加RestKit
6. 在Link Binary with Libraries中添加如下包名称:
libRestKitCoreData.a
libRestKitJSONParserYAJL.a
libRestKitNetwork.a
libRestKitObjectMapping.a
libRestKitSupport.a
CFNetwork.framework
CoreData.framework
MobileCoreServices.framework
SystemConfiguration.framework
7. 头文件中引入
#import <RestKit/RestKit.h>
#import <RestKit/CoreData/CoreData.h>
点击编译,如果没问题的话就编译成功了。
现在我们进行一个简单的测试:
在applicationDidFinishLaunching函数中添加如下代码:
- (void)applicationDidFinishLaunching:(UIApplication*)application withOptions:(NSDictionary*)options {
RKClient* client = [RKClient clientWithBaseURL:@"http://restkit.org"];
}
测试如下:
#import <RestKit/RestKit.h> // Here we declare that we implement the RKRequestDelegate protocol
// Check out RestKit/Network/RKRequest.h for additional delegate methods
// that are available.
@interface RKRequestExamples : NSObject <RKRequestDelegate> {
} @end @implementation RKRequestExamples - (void)sendRequests {
// Perform a simple HTTP GET and call me back with the results
[[RKClient sharedClient] get:@"/foo.xml" delegate:self]; // Send a POST to a remote resource. The dictionary will be transparently
// converted into a URL encoded representation and sent along as the request body
NSDictionary* params = [NSDictionary dictionaryWithObject:@"RestKit" forKey:@"Sender"];
[[RKClient sharedClient] post:@"/other.json" params:params delegate:self]; // DELETE a remote resource from the server
[[RKClient client] delete:@"/missing_resource.txt" delegate:self];
} - (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(@"Retrieved XML: %@", [response bodyAsString]);
} } else if ([request isPOST]) { // Handling POST /other.json
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
} } else if ([request isDELETE]) { // Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
}
}
} @end
附上英文原文网址:http://liebke.github.com/restkit-github-client-example