1

I know that this has been brought up many a times and answered twice as many on this site, however I think I may have something a little different and need to know if it is possible.

I am trying to load a banner ad from a website into a UIWebView in the app. All of this works flawlessly. However no matter what code I have tried to implement I cannot get it so that when the ad is clicked in the app it will launch in safari.

Basically I am wanting to have my own Ad server. The ad is managed ad hosted on our site. The banner has the link embedded into it by the server.

Here is the code I am using.

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    NSString *string;
    string = @"http://www.samplesite.com/mobile_ads";
    NSURL *url = [NSURL URLWithString: string];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.adBox loadRequest:requestObj];
}

-(BOOL) adBox:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest    navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;
}

Any ideas of where I should go?

GoGauchos
  • 588
  • 5
  • 11
Raymond
  • 477
  • 2
  • 5
  • 15

1 Answers1

1

In your UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method, do something like the following (assuming your ads have part of their URL identifiable):

- (void)methodThatCreatesTheWebview {
  UIWebView *webview = [[UIWebView alloc] init];
  webview.delegate = self;
}


// Portion of the URL that is only present on ads and not other URLs.
static NSString * const kAd = @"adSubdominOrSubDirectory";

// pragma mark - UIWebViewDelegate Methods

- (BOOL)webView:(UIWebView *)webView
    shouldStartLoadWithRequest:(NSURLRequest *)request
                navigationType:(UIWebViewNavigationType)navigationType
{
  if ([request.URL.absoluteString rangeOfString:kAd] !=  NSNotFound) {
    // Launch Safari to open ads
    return [[UIApplication sharedApplication] openURL:request.URL];
  } else {
    // URL isn't an ad, so just load it in the webview.
    return YES;
  }
}
Ben S
  • 68,394
  • 30
  • 171
  • 212
  • When you create the webview, add the view controller that manages it as its delegate. This will cause it to receive callbacks when certain events happen (for example, when the URL is about to change). See the Concepts in Objective-C Programming for more information about the delegation pattern: https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11 – Ben S Sep 18 '13 at 19:47
  • I have always found the apple docs to be more confusing than helpful. I understand what you are saying, but I still have no clue where this belongs. – Raymond Sep 18 '13 at 19:54
  • Even after getting through most of your code and placements it does not work. – Raymond Sep 18 '13 at 20:47
  • openURL: is going to return YES on success. If you return YES from webView:shouldStartLoadWithRequest:navigationType:, it will also load that URL in your webview; probably not what you want. I think that line of code should be `return ![[UIApplication sharedApplication] openURL:request.URL];` – zpasternack Apr 11 '15 at 20:04