I am trying to set up an Http GET request to a certain URL from my Objective C code. No matter what I do, or which URL I use, I get the following error:
Connection Failed: Error Domain=NSURLErrorDomain
Code=-1000 "bad URL" UserInfo=0x755c440
{NSUnderlyingError=0x7561950 "bad URL", NSLocalizedDescription=bad URL}
I am new to iOS development, so I am not sure if I am doing this right. I have followed the apple documentation and looked for answers in various forums and even then I get this error.
Here is my code:
NSString *url = [NSString stringWithFormat: @"http://example.com/api/%@/%@", str1, str2];
NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSLog(@"URL - %@", encodedUrl); // Checking the url
NSMutableURLRequest *theRequest= [[NSMutableURLRequest alloc] init];
[theRequest setHTTPMethod:@"GET"];
[NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
Any suggestion is most welcome.
You’re not initializing your
theRequest.This line does nothing if you’re not assigning its return value to a variable.
This is the code you should use: