最近作成されたYouTubeチュートリアルに従って、非同期でWebアドレスに接続してみてください。応答に対してnullを再調整しますが、データ長が0より大きいです。nullと長さの両方のチェックを行う他のコードを見ましたが、スローせず、NSLogしただけです。
@implementation ViewController
-(void)fetchAddress:(NSString *)address {
NSLog(@"Loading Address: %@", address);
[iOSRequest requestToPath:address onCompletion:^(NSString *result, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[self stopFetching:@"Failed to Fetch"];
NSLog(@"%@", error);
} else {
[self stopFetching:result];
NSLog(@"Good fetch: %@", result);
}
});
}];
}
- (IBAction)fetch:(id)sender {
[self startFetching];
[self fetchAddress:self.addressField.text];
NSLog(@"Address: %@", self.addressField.text);
}
-(void)startFetching {
NSLog(@"Fetching...");
[self.addressField resignFirstResponder];
[self.loading startAnimating];
self.fetchButton.enabled = NO;
}
-(void)stopFetching:(NSString *)result {
NSLog(@"Done Fetching %@", result);
self.outputLabel.text = result;
[self.loading stopAnimating];
self.fetchButton.enabled = YES;
}
@implementation iOSRequest
+(void)requestToPath:(NSString *)
path onCompletion:(RequestCompletionHandler)complete {
NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]
cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:10];
NSLog(@"path: %@ %@", path, request);
[NSURLConnection sendAsynchronousRequest:request
queue:backgroundQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[NSString alloc] initWithData:
data encoding:NSUTF8StringEncoding];
if (complete) {
complete(result, error);
NSLog(@"result: %@ %lu %@", result, (unsigned long)[data length], error);
}
}];
}
リクエストはhttp://www.google.com>レスポンスはnullデータ長は13644
何が悪いのかわからない...何か提案はありますか?