2017-09-07 179 views
0

我的代碼必須更新網頁視圖和下載網站。但它不會因爲URLWithString返回null。我怎麼解決這個問題? stringByAddingPercentEscapesUsingEncoding已被棄用。URLWithString返回null

-(void) setWebViewMain:(NSString *) link { 
    // link = @"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working 

    NSURL *url = [NSURL URLWithString: link]; 

    NSLog(@"%@",url); // here url is (null) 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [self.webView loadRequest:requestObj]; 
    [self.webView reload]; 

} 
+0

歡迎來到SO。請改進您的[問題格式](https://stackoverflow.com/editing-help)並澄清您的問題 - – lifeisfoo

+1

顯然,您以「link」身份傳入的值不是有效的URL。您需要正確地轉義特殊字符。 – rmaddy

+0

這可能是一個重複的https://stackoverflow.com/questions/42529509/stringbyaddingpercentescapesusingencoding-deprecated – rmaddy

回答

-3

剛剛從你的方法刪除的最後一行,也將努力:

-(void) setWebViewMain:(NSString *) link { 
    // link = @"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working 

    NSURL *url = [NSURL URLWithString: link]; 

    NSLog(@"%@",url); // here url is (null) 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [self.webView loadRequest:requestObj]; 
    //[self.webView reload]; 
} 

你可以這樣調用:

[self setWebViewMain:@"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"]; 
+0

請閱讀該問題(以及您在答案中發佈的代碼中的註釋)。問題是'url'是'nil'。 – rmaddy

+0

是的,問題是url爲null – Vitaliy

0

嘗試爲

-(void) setWebViewMain:(NSString *) link { 
// link = @"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"; if this string uncomment code is working 

NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@",link]]; 

NSLog(@"%@",url); // here url is (null) 

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
[self.webView loadRequest:requestObj]; 
// [self.webView reload]; 

}

+0

我試過這個,但是這段代碼也不工作 – Vitaliy