最近在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