UIWebView无法在iPhone应用程序中打开链接

时间:2013-08-24 06:08:03

标签: iphone ios uiwebview

我有UIWebView我想在其中加载链接,但它没有显示任何东西这里是我使用它的代码不显示

fileName的值为http://www.google.com

            NSURL *url = [NSURL fileURLWithPath:fileName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];

    [webView loadRequest:request];  

4 个答案:

答案 0 :(得分:1)

尝试这样做,将fileURLWithPath替换为URLWithString

    NSString *fileName= @"http://www.google.com";

    NSURL *url = [NSURL URLWithString:fileName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview setScalesPageToFit:YES];

    [webview loadRequest:request];

答案 1 :(得分:0)

如果有包含您网址的路径,则会使用

**fileURLWithPath**

但是在你的情况下,有一个直接的URL,所以你应该使用如下

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",@"http://www.google.com"]];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];

[webView loadRequest:request]; 

答案 2 :(得分:0)

//Create a URL String.
NSString *urlString = [NSString stringWithFormat:@"http://www.google.com"];
//Create a Legeal URL object.
NSURL *MyUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:MyUrl];
// set Fit page to WebView
[webView setScalesPageToFit:YES];
//Load the request in the UIWebView.    
[webView loadRequest:requestObj];

注意:如果您使用webView添加,请不要忘记将self.View添加到XIB ,然后确保您连接是正确的;

答案 3 :(得分:0)

试试这个...... !!!

 UIWebView * aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    NSString *fileName= @"http://click-labs.com";

    NSURL *url = [NSURL URLWithString:fileName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [aWebView setScalesPageToFit:YES];

    [aWebView loadRequest:request];
    [ self.view addSubview:aWebView];