2013-06-26 47 views
1

我是相對較新的Objective-C但掙扎與NSURLConnection委託。下面我有一個執行文件api.m當NSURLConnection委託自己如何返回didReceiveData和didReceiveResponse我的方法

在我的viewcontrollers的其他地方,我用方法getGroups調用這個API對象,這裏的目的是返回發出API請求時發現的組數。我可以在didReceiveData中看到數據,但是如何將這些數據返回到我的getGroups中,以便我可以在viewController中訪問它?

在我的視圖控制器我有類似:

NSInteger *numGroups = [apiRequest getGroups]; 

,並在我的api.m實現文件我有以下。再次一切正常我只是不知道如何返回來自didReceiveData的數據,所以我可以在getGroups方法中訪問它。

#import "API.h" 
#import "Constants.h" 
#import "JSONParser.h" 

@implementation API 

@synthesize user, url, receivedData 

-(NSInteger)getGroups { 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; 

    [request setValue:APIKEY forHTTPHeaderField:@"apikey"]; 
    [request setHTTPMethod:@"GET"]; 
    [request setURL:url]; 

    NSURLConnection *myConnection; 
    myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    //How do I access what was append'd in receivedData below 

    return 2; 

} 


/* ---------------------------------------------------------------------------------------- 
NSURLConnection Delegates 
------------------------------------------------------------------------------------------- */ 

// Check the response code that was returned 
- (NSInteger)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    return [httpResponse statusCode]; 

} 



// Take a peak at the data returned. 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"DATA: %@", [data description]); 

    //How to get this information back up into the getGroups method 
    [receivedData appendData: data]; 

} 



// Close the connection 
- (void)connectionDidFinishLoading:(NSURLConnection*)connection { 

    NSLog(@"Connection Closed."); 

} 

@end 

回答

1

你想要做的是在你的ViewController中調用API將API的委託設置爲self。然後,您需要在ViewController中添加這些委託方法,而不是將它們用於API之外。當NSURLConnection試圖調用其中一個委託方法時,可以在ViewController中訪問它。你也想確保你在你的ViewController的.h文件中添加委託協議。

作爲一個簡單的例子你VC.h文件將包含以下內容:

@interface ViewController : UIViewController <NSURLConnectionDataDelegate> 

然後在您的VC.m文件你有以下幾種方法:

/* ---------------------------------------------------------------------------------------- 
NSURLConnection Delegates 
------------------------------------------------------------------------------------------- */ 

// Check the response code that was returned 
- (NSInteger)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    return [httpResponse statusCode]; 

} 



// Take a peak at the data returned. 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"DATA: %@", [data description]); 

    //How to get this information back up into the getGroups method 
    [receivedData appendData: data]; 

} 



// Close the connection 
- (void)connectionDidFinishLoading:(NSURLConnection*)connection { 

    NSLog(@"Connection Closed."); 

} 

現在,當您NSURLConnection會嘗試調用didReceiveData,它將在您的ViewController中調用,而不是在API中調用。

作爲一個便箋,我衷心推薦接受@SK9的建議,並將其作爲一個異步調用來將其從主線程中抽象出來。

0

NSURLConnectionsendSynchronousRequest:returningResponse:error:,看到here,將返回的數據對象。確保你很樂意阻止當前的線程。我更喜歡這是一個異步請求,並有一個完成塊來處理返回。在我提到的頁面上有更多詳細信息,但是如果這是新的話,確實會在塊上進行閱讀。 Short Practical Guid to Blocks可能會有所幫助。

相關問題