0

I am learing WebView.
when I do something like

NSString *url = @"www.google.com";
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

it fails.
But when I type www.google.com (or even google.com) in standard browser, it works fine.

I also noticed that after loading the page, the url text field in standard browser changes the link from www.google.com to https:// www. google.co.in/?gws_rd=ssl

In above code when I set NSString *url = @"https://www.google.co.in/?gws_rd=ssl" it works fine

So how do I implement my WebView view so that if should work like a standard browser it terms of above context

Kaunteya
  • 3,107
  • 1
  • 35
  • 66
  • Actually I am doing on Mac. So it triggers didFailProvisionalLoadWithError – Kaunteya Nov 21 '14 at 11:52
  • can you check the link http://stackoverflow.com/questions/5677810/how-to-connect-with-client-certificate-using-a-webview-in-cocoa it may help – user2071152 Nov 21 '14 at 12:02

2 Answers2

1

UIWebView always needs http or https when starting request, if you click on links inside the webView it will handle it itself. So here is how to handle it:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITextField *addressBar = [[UITextField alloc] init];
    [addressBar setDelegate:self];

    [self loadRequestFromString:@"www.google.com"];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self loadRequestFromString:textField.text];
    [textField resignFirstResponder];
    return YES;
}

- (void)loadRequestFromString:(NSString *)urlString
{
    NSURL *webpageUrl;
    if ([urlString hasPrefix:@"http://"] || [urlString hasPrefix:@"https://"]) {
        webpageUrl = [NSURL URLWithString:urlString];
    } else if ([urlString containsString:@" "] || ![urlString containsString:@"."]) {
        webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    } else {
        webpageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", urlString]];
    }

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:webpageUrl];
    [_webView loadRequest:urlRequest];
}
emotality
  • 12,795
  • 4
  • 39
  • 60
  • What about `ftp://my-host.com`. – Kampai Nov 21 '14 at 12:38
  • I think if your app handles ftp, I think you should ask the user through an `UIActionSheet` or `UIAlertView`? Because a domain will work with http and ftp, so personally I don't know how to troubleshoot that. – emotality Nov 21 '14 at 12:44
  • @emotality: You have misunderstood the question. Please read now. I have restructured the question. – Kaunteya Nov 21 '14 at 14:57
  • No I haven't, I updated the answer to make it more clear. `UIWebView` will always do this, that's what it does because not all websites start with http or whatever, you get other prefixes as well. So they make it more flexible for the developer. This is how to do it for websites using http etc. – emotality Nov 21 '14 at 17:10
0

to open any URL in UIWebView you should add url with protocol eg @"http://www.google.com".

[NSURL URLWithString:@"http://www.google.com"];

then it will work.

Kampai
  • 22,848
  • 21
  • 95
  • 95