I am trying to implement an asynchornus request . I did my research and this is the best I got but the code is full of errors that I do not find the solution for
NSURL *url2 = [NSURL URLWithString:@"www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url2];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
[delegate receivedData:data];//i get the error here use of undeclared identifier 'delegate' ,when I put self insead I receive the error : no visible @interface for "my class name" declares the selector "received data"
else if ([data length] == 0 && error == nil)
[delegate emptyReply];//same error here
else if (error != nil && error.code == ERROR_CODE_TIMEOUT) //the error here is "use of undelcared identifier ERROR_CODE_TIMEOUT
[delegate timedOut];//error here is save as the first one
else if (error != nil)
[delegate downloadError:error];//error here is save as the first one
}];
I have added NSURLConnectionDelegate
in my .h file
can somebody tell me what is the error ?
Thanks
I don't have much experience with this method, but this example works. I sent this to my server to test getting product info for an in-app purchase. You could put more else-ifs into it to, like you posted, to test for different possible outcomes, but this should get you started. I'm not sure why that stub you posted has references to a delegate -- the point (or one of the points) of block methods is to be able to do these kinds of things without a delegate.
- (void)makeConnection {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:kServerPath]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:5];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error) {
NSLog(@"%@, %@ %@",theResponse.suggestedFilename,theResponse.MIMEType,theResponse.textEncodingName);
if (theData != nil && error == nil) {
NSArray *productArray = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",productArray);
}else{
NSLog(@"%@",error.localizedDescription);
}
}];
}