0

My code must update web view and download web site. But it doesn't because URLWithString returns null. How can I solve this problem? stringByAddingPercentEscapesUsingEncoding is already deprecated.

-(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];

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vitaliy
  • 1
  • 1
  • Welcome to SO. Please improve you [question formatting](https://stackoverflow.com/editing-help) and clarify your question- – lifeisfoo Sep 07 '17 at 20:21
  • 1
    Clearly the value you pass in as `link` isn't a valid URL. You need to properly escape special characters. – rmaddy Sep 07 '17 at 20:29
  • This is probably a duplicate of https://stackoverflow.com/questions/42529509/stringbyaddingpercentescapesusingencoding-deprecated – rmaddy Sep 07 '17 at 20:30
  • Could you check: that `link` is really a NSString object? Could you check that it has correct no extra "{", or "("? – Larme Sep 08 '17 at 08:31
  • https://github.com/Vitalikspb/rss.git. link 100% NSString there is no "{", "(" – Vitaliy Sep 08 '17 at 16:15

2 Answers2

0

try as

  -(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];

}

Vikas Rajput
  • 1,754
  • 1
  • 14
  • 26
-3

Just remove the last line from your method and it will work:

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

You can call it like this:

[self setWebViewMain:@"http://www.rbc.ru/society/05/09/2017/59ae2dfe9a794753f05e3e06"];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amjad
  • 1
  • 1
  • Please read the question (and the comment in the code you posted in your answer). The problem is that `url` is `nil`. – rmaddy Sep 07 '17 at 22:22