<轉>RestKit在iOS項目中的使用,包含xcode配置說明

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.