为什么我的nsurl请求没有出现在控制台中?

时间:2013-03-12 15:55:13

标签: iphone ios uiwebview nsurlconnection

我正在尝试将重定向的网址显示在控制台中。我可能在我的代码中遗漏了一个小细节。任何帮助都会被贬低。

- (void)viewDidLoad {
 UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 119,     self.view.bounds.size.width, self.view.bounds.size.height - 165)];
webView.backgroundColor = [UIColor whiteColor];
//    webView.userInteractionEnabled = NO;
webView.opaque = NO;
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
[self.view addSubview:webView];
webView.delegate = self;
}


 - (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)redirectResponse
{
//Make sure tinyurl is doing the redirection
if([[[redirectResponse URL] host] compare:@"bitly.com"
                                  options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
    NSLog(@"Redirect Location: %@", [request URL]);
}

//call [connection cancel]; to cancel the redirect and stop receiving data
//return nil; to cancel redirect but continue receiving data
//return request; will continue the redirection as normal
    return request;
 }

2 个答案:

答案 0 :(得分:2)

您正在使用NSURLConnectionDelegate的委托方法UIWebView

您没有在UIWebView中加载任何请求,您希望NSLog如何打印请求? 如果您使用的是UIWebViewDelegate,那么检查案例的正确方法应该是:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType  {

  NSLog(@"Redirect Location: %@",[request.URL absoluteString]);
}

答案 1 :(得分:0)

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)redirectResponse

这不是UIWebView委托方法。因此,除非您没有显示所有代码,否则它永远不会被调用。

您需要创建一个NSURLConnection来充分利用该调用。

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];