1

I am trying to get the redirected url to show up in console. I may be missing a minor detail somewhere in my code.Any help will be appreicated.

- (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;
 }
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Moo Moo
  • 85
  • 7

2 Answers2

2

You are using delegate method of NSURLConnectionDelegate it is not right method for UIWebView

You are not loading any request in the UIWebView either how do you expect NSLog to print out the request? If you are using UIWebViewDelegate, then the right method to check in your case should be:

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

  NSLog(@"Redirect Location: %@",[request.URL absoluteString]);
}
Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
0
- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)redirectResponse

That isn't a UIWebView Delegate Method. So unless you're not showing all your code its never getting called.

You need to create an NSURLConnection to take advantage of that call like so.

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98