如何在iPhone中集成Webview?

时间:2012-09-08 09:37:31

标签: iphone browser safari webview

我正在使用iPhone应用程序,使用Webview在屏幕底部加载Addvertisement并且工作正常,我想当用户选择WebView时,它会自动进入浏览器并加载到iPhone设备中,如何集成这个?请帮帮我

先谢谢

我试过了:

- (void)viewDidLoad
{

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
[webView setDelegate:self];
NSString *urlAddress = @"http://www.dasfafa./myadds.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}

-(void)webViewDidStartLoad:(UIWebView *)webView 
{

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString *currentURL = self.AddvertiseWebView.request.URL.absoluteString;
    NSLog(@"currentURL:%@",currentURL);

}

1 个答案:

答案 0 :(得分:4)

此处的代码显示了在UIWebView中单击时如何在Safari中打开链接。方法shouldStartLoadWithRequest是在单击链接时调用的委托方法。您可以覆盖该方法并告诉它在Safari中打开。

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

    NSURL *requestURL = [ [ request URL ] retain ]; 
    // Check to see what protocol/scheme the requested URL is.
    if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] 
        || [ [ requestURL scheme ] isEqualToString: @"https" ] ) 
        && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
        return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; 
    }
    // Auto release 
    [ requestURL release ];
    // If request url is something other than http or https it will open 
    // in UIWebView. You could also check for the other following 
    // protocols: tel, mailto and sms
    return YES;
}

或试试这个

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

    return YES; }