1

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

我正在尝试将网站上的横幅广告加载到应用程序中的 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;
}

关于我应该去哪里的任何想法?

4

1 回答 1

1

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

- (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;
  }
}
于 2013-09-18T19:33:40.467 回答