I have a text file in My Documents folder in iOS that contains a link that looks like this inside: @"https://www.mysite.com"
I can read the file successfully with:
//READ TEXT FILE:
NSArray *paths5 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory5 = [paths5 objectAtIndex:0];
NSString *filePath5 = [documentsDirectory5 stringByAppendingPathComponent:@"mytext.txt"];
NSString *myContent = [NSString stringWithContentsOfFile:filePath5 encoding:NSUTF8StringEncoding error:NULL];
NSString *myContent is the actual link: @"https://www.mysite.com
Now I load the content via NSURLRequest like so:
NSURLRequest *request2 = [NSURLRequest requestWithURL:
[NSURL URLWithString:myContent]];
[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
The Problem/error I get:
Connection failed: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xfe75cc0 {NSUnderlyingError=0xfe86ac0 "bad URL", NSLocalizedDescription=bad URL}
BUT, If [NSURL URLWithString:myContent]]; is replaced literally with the url string such as [NSURL URLWithString:@"https://www.mysite.com"]], then the connection works.
What's happening here? Why can't I just load the url in the text file into my URLWithString parameter?
filePath5 = [filePath5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
– Sylvain Guillopé