2012-02-28 25 views
0
UIViewController *webViewController = [[[UIViewController alloc] init] autorelease]; 

    UIWebView *uiWebView = [[[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)] autorelease]; 
    NSString *urlst=[[NSString alloc]initWithString:[catWebArray objectAtIndex:indexPath.row]]; 
    [uiWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlst]]]; 
    uiWebView.scalesPageToFit=YES; 
    [webViewController.view addSubview: uiWebView]; 

    [self.navigationController pushViewController:webViewController animated:YES]; 

其中數組包含ADRESS像www.google.com,www.yahoo.com。 由於這些是由用戶添加http://可能會添加或不添加。所以如何我可以加載它在uiwebview?如何處理未知的方案,對webview什麼都不做?

回答

1

你並沒有說明具體的應用程序用戶界面的樣子,或用戶如何在實際的URL進入,所以我只是將假定這是一個文本框,看起來像什麼移動Safari瀏覽器使用。

您可以編寫一個方法,在撥打requestWithURL電話之前撥打電話,如果您未在用戶輸入的URL中看到預先設置的方案,則可以返回一個添加了http://的方法。

E.G.

- (NSString *) fullyFormedURL: (NSString *) originalURL 
{ 
    NSRange rangeOfScheme = [originalURL rangeOfString: @"://"]; 
    if(rangeOfScheme.location == NSNotFound) 
    { 
     NSString * newURL = [NSString stringWithFormat: @"http://%@", originalURL]; 
    } 
    return(originalURL); 
} 

(此代碼片段上面是不完美的,但意思是什麼讓你開始...的rangeOfString呼叫沒有考慮到的東西脂肪手指的用戶可能潛在地做,就像把一個方案在字符串的末尾...例如"www.google.comhttp://"

+0

感謝您的回覆。通過webservice收到的數據。 – user928622 2012-02-28 10:59:51