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:
https://vula.uct.ac.za/portal/pda/9f563cb2-24f9-481f-ab2e-631e85c9f3aa/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3
to a shorter URL that displays only part of the webpage and is:
https://vula.uct.ac.za/portal/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3
-(NSString *)cropURL:(NSString *)inputURL{
NSString *outputURL ;
NSRange match;
match = [inputURL rangeOfString: @"tool-reset"]; //Gives the index and range of the string "tool-reset"
int toolLen = match.location+match.length ; //Gives the index after "tool-reset"
NSString*tempIdentifier = [inputURL substringFromIndex:toolLen] ; //Gets the characters after "tool-reset"
NSString *firstURL = @"https://vula.uct.ac.za/portal/tool-reset" ; //First part of URL
NSMutableArray *concatURL = [[NSMutableArray alloc] initWithCapacity:2] ; // Array to concat firstURL and tempIdentifier
[concatURL addObject:(NSString *)firstURL] ;
[concatURL addObject:(NSString *) tempIdentifier] ;
outputURL = [concatURL componentsJoinedByString:@""] ; //Concatenatd to outputURL
outputURL = [outputURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ; //Encoding
return outputURL ;
}
*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?
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];