nsurlnsurlerrordomain

NSURL Bad URL error when using stringByAddingPercentEscapesUsingEncoding


I concatenated two strings to get a URL, below is the method for concatenating the strings: The purpose of this is to go from a URL namely:

to a shorter URL that displays only part of the webpage and is:

*Note that the website requires the user to log in, this is done earlier and these methods are only called once login is successful.

I sent this concatenated URL to the connection method with a GET and I get the error below:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x1d51f7d0 {NSUnderlyingError=0x1e547980 "bad URL", NSLocalizedDescription=bad URL}

The unconcatenated URL doesn't have this problem though.

The URL in question is:

https://vula.uct.ac.za/portal/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3

the URL is correct and has been tested. (Again, note that the site requires you to log in)

I did use:

stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

before sending it to the the connection, but I still get the same error.

All my other links work, the only links that don't work are the ones that were concatenated. These concatenated strings do work in a normal web browser.

Any ideas what could be wrong?


Solution

  • One way to achieve this then:

    NSURL *input = [NSURL URLWithString:@"https://vula.uct.ac.za/portal/pda/9f563cb2-24f9-481f-ab2e-631e85c9f3aa/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3"];
    
    NSString *identifier = input.lastPathComponent;
    
    NSURL *base = [NSURL URLWithString:@"https://vula.uct.ac.za/portal/tool-reset/"];
    
    NSURL *output = [base URLByAppendingPathComponent:identifier];