UIWebView在Safari中启动

时间:2013-09-18 19:24:34

标签: ios uiwebview safari

我知道这已经提出了很多次并在本网站上回答了两倍,但我想我可能会有一些不同的东西,需要知道是否可能。

我正在尝试将横幅广告从网站加载到应用中的UIWebView。所有这一切都完美无瑕。然而,无论我试图实现什么代码,我都无法理解,以便在应用程序中点击广告时它将在safari中启动。

基本上我想拥有自己的广告服务器。广告是在我们网站上托管的广告。横幅具有服务器嵌入其中的链接。

这是我正在使用的代码。

- (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;
}

我应该去哪里?

1 个答案:

答案 0 :(得分:1)

UIWebViewDelegatewebView:shouldStartLoadWithRequest:navigationType:方法中,执行以下操作(假设您的广告中有部分网址可识别):

- (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;
  }
}