UIWebView 在 Safari 中启动 [英] UIWebView Launch in Safari

查看:40
本文介绍了UIWebView 在 Safari 中启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题在这个网站上被提到过很多次,回答过的次数是这个网站的两倍,但是我想我可能有一些不同的东西,需要知道是否可能.

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.

我正在尝试将来自网站的横幅广告加载到应用程序的 UIWebView 中.所有这些都完美无缺.但是,无论我尝试实现什么代码,我都无法获得它,因此当在应用中点击广告时,它会在 safari 中启动.

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?

推荐答案

在您的 UIWebViewDelegatewebView:shouldStartLoadWithRequest:navigationType: 方法,做点什么如下所示(假设您的广告的部分网址是可识别的):

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

这篇关于UIWebView 在 Safari 中启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆