2014-11-21 8 views
0

WebView가 표시됩니다. 내가WebView에서 기본 URL이 작동하지 않습니다.

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

뭔가를 할 때
는 실패합니다.
하지만 표준 브라우저에서 www.google.com (또는 심지어 google.com)을 입력해도 정상적으로 작동합니다. 나는 그것이 내가 구현 어떻게 그렇게 잘

작동 NSString *url = @"https://www.google.co.in/?gws_rd=ssl"을 설정할 때

는 또한 페이지를로드 한 후, 표준 브라우저의 URL 텍스트 필드 위의 코드에서 https:// www. google.co.in/?gws_rd=ssl

-www.google.com에서 링크를 변경 것으로 나타났습니다 내 WebView보기 그래서 위의 컨텍스트의 조건을 표준 브라우저처럼 작동 해야하는 경우

+0

무엇이 오류입니까? – user2071152

+0

사실 저는 Mac에서하고 있습니다. 그래서 didFailProvisionalLoadWithError를 – Kaunteya

+0

트리거 할 수 있습니다 http://stackoverflow.com/questions/5677810/how-to-connect-with-client-certificate-using-a-webview-in-cocoa – user2071152

답변

1

UIWebView 요청을 시작할 때 항상 http 또는 https가 필요합니다. webView 내부의 링크를 클릭하면 자체 처리됩니다. 그래서 여기 그것을 처리하는 방법입니다 :

- (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]; 
} 
+0

' ftp : // my-host.com'. – Kampai

+0

당신의 앱이 ftp를 처리한다면, 당신이'UIActionSheet' 또는'UIAlertView'를 통해 사용자에게 물어봐야한다고 생각합니다. 도메인은 http 및 ftp와 함께 작동하므로 개인적으로 어떻게 문제를 해결할 수 있는지 잘 모릅니다. – emotality

+0

@emotality : 질문에 대한 오해가 있습니다. 지금 읽어주세요. 나는 그 문제를 재구성했다. – Kaunteya

0

URL을 UIWebView에 열려면 예 : @ "http://www.google.com"URL을 추가해야합니다.

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

그러면 작동합니다.

관련 문제