1

我对objective-c 相对较新,但在涉及NSURLConnection 时与代表苦苦挣扎。下面我有一个实现文件 api.m

在我的视图控制器的其他地方,我使用 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
4

2 回答 2

1

您要做的是在调用 API 的 ViewController 中将 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 的建议,并将其设为异步调用以将其从主线程中抽象出来。

于 2013-06-26T13:34:44.717 回答
0

NSURLConnectionsendSynchronousRequest:returningResponse:error:,见这里,将返回一个数据对象。请确保您很乐意像这样阻止当前线程。我希望这是一个异步请求,并带有一个完成块来处理返回。我提到的页面上有更多详细信息,但如果这是新的,请优先阅读块。块的简短实用指南可能会有所帮助。

于 2013-06-26T05:08:17.317 回答