2012-04-07 63 views
0

我在applicationDidFinishLaunching的應用程序委託中初始化RKClient的sharedClient,它工作的很好。我在大多數應用程序中使用這個客戶端的目標URL,但是在1個類(播放器)中,我需要從gravatar.com加載用戶的頭像。所以,我有Player類定義它自己的RKClient並符合RKRequestDelegate協議。然後它通過這個新的RKClient實例發出請求,並將請求的委託設置爲self。問題是我從來沒有收到迴應;即RestKit:多個RKClients /更改baseURL

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response 

永遠不會被調用。這裏是整個代碼示例:

// Player.m 

#import "Player.h" 

@implementation Player 

# pragma mark - Accessor Synthesizers 

@synthesize identifier = _identifier; 
@synthesize name = _name; 
@synthesize story = _story; 
@synthesize emailHash = _emailHash; 
@synthesize pointPercentage = _pointPercentage; 
@synthesize hitPercentage = _hitPercentage; 
@synthesize lastCups = _lastCups; 
@synthesize shotCount = _shotCount; 
@synthesize hitCount = _hitCount; 
@synthesize wins = _wins; 
@synthesize losses = _losses; 
@synthesize gravatar = _gravatar; 

# pragma mark - Instance Methods 

- (void)getGravatar { 
    RKClient *gClient = [RKClient clientWithBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 
    NSString *path = [self gravatarLink]; 
    NSLog(@"Getting Gravatar With Link: http://gravatar.com%@", path); 
    [gClient get:path delegate:self]; 
} 

- (NSString *)description { 
    return [NSString stringWithFormat:@"{Season: [id=%i, name=%@, story=%@ ]}", _identifier, _name, _story]; 
} 

# pragma mark - RKRequest Delegate Methods 

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { 
    NSLog(@"Gravatar Back: %@", response.bodyAsString); 
    self.gravatar = response.body; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GravatarLoaded" object:self]; 
} 

# pragma mark - Private Methods 

- (NSString *)gravatarLink { 
    NSString *path; 
    path = [NSString stringWithFormat:@"/avatar/%@?d=monsterid&r=x", _emailHash]; 
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale]==2) 
     return [path stringByAppendingString:@"&s=200"]; 
    else 
     return [path stringByAppendingString:@"&s=100"]; 
} 

@end 

另外,我還試圖改變sharedClient的基本URL,只是使用了的gravatar請求sharedClient。但每當我試圖改變RKClient sharedClient的基本URL特性,通過以下兩種方式:

[[RKClient sharedClient] setBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 

[RKClient sharedClient].baseURL: [NSURL URLWithString:@"http://gravatar.com"]; 

我得到一個運行時錯誤:

2012-04-07 15:37:23.565 MLP[34403:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL URLByAppendingResourcePath:queryParameters:]: unrecognized selector sent to instance 0x8bcdd50' 
*** First throw call stack: 
(0x1b7c022 0x1f58cd6 0x1b7dcbd 0x1ae2ed0 0x1ae2cb2 0x23eb5 0x24032 0x6ddb 0xdda2 0xc465c5 0xc467fa 0x14db85d 0x1b50936 0x1b503d7 0x1ab3790 0x1ab2d84 0x1ab2c9b 0x20407d8 0x204088a 0xbb5626 0x27cd 0x2735) 
terminate called throwing an exception(lldb) 

回答

0

clientWithBaseURL:方法預計NSString,而不是NSURL。嘗試使用

[[RKClient sharedClient] setBaseURL:@"http://gravatar.com"]; 
0

你只需要到Targets->構建設置 - >其他鏈接器標記

正如RestKit教程中指定我們改變添加了一個名爲「-ObjC-all_load」標誌現在編輯那隻顯示「-ObjC」

這必須爲你起鍋。

1

我用:

[[RKClient sharedClient] setBaseURL:[RKURL URLWithString:@"http://gravatar.com"]]; 

爲我工作。