iPhone/objC
我真的被困在这里,我可以使用一些帮助,拜托。
让我先解释一些事情:
我有一个加载 url 的 UIWebView。一旦用户单击链接 - (BOOL)webView:shouldStartLoadWithRequest:navigationType: 获取消息(根据协议)。在这个方法中,我可以决定是否应该在 webView 中加载 url 或者用它做其他事情。我正在建立一个 NSURLConnection 以便我可以检查响应。
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
NSLog(@" Connection established");
receivedDataFromConnection = [[NSMutableData data] retain];
}
else {
NSLog(@"Connection failed");
}
if (downloadYESorNO == YES) {
NSLog(@"Do the download");
return NO;
}
else {
NSLog(@"will show in webView");
return YES;
}
}
一旦应用程序收到响应 - (void)connection:didReceiveResponse: 收到一条消息(根据协议),我就可以在那里分析响应。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSString *MIME = response.MIMEType;
NSString *appDirectory = [[NSBundle mainBundle] bundlePath];
NSString *pathMIMETYPESplist = [appDirectory stringByAppendingPathComponent:@"MIMETYPES.plist"];
NSArray *displayMIMETypes = [NSArray arrayWithContentsOfFile: pathMIMETYPESplist];
BOOL *asdf = [displayMIMETypes containsObject:MIME];
if (asdf == YES) {
downloadYESorNO =NO;
}
else {
downloadYESorNO = YES;
}
[receivedDataFromConnection setLength:0];
[connection release];
现在我想要实现的是让 - (BOOL)webView:shouldStartLoadWithRequest: 等到 -(void)connection:didReceiveResponse: 准备好。
我可以使用 sendSynchronousRequest: 因为它会在我检查响应之前下载完整的文件。
有人有想法吗?提前致谢。还是有更好的方法来检查响应?