Problem related to NSURLConnection - ios

today, I encountered a problem with NSURLConnection. I want to download the contents of the URL http://api.wunderground.com/api/fs3a45dsa345/geolookup/q/34.532900,-122.345.json. If I simply paste the URL into Safari, I get the correct response. However, if I do the same thing with NSURLConnection, I get a "not found" response. Here's the code I'm using:
NSURL *requestURL = [[NSURL alloc] initWithString:#"same url as above"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:requestURL];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self
startImmediately:YES];
What's the problem here?

Make sure you're escaping any special characters in the URL string by sending it a stringByAddingPercentEscapesUsingEncoding: message, for example:
NSString *s = [#"some url string" stringByAddingPercentEscapesUsingEncoding:NSUTFStringEncoding];
NSURL *requestURL = [NSURL URLWithString:s];
EDIT
It turns out the web service request is failing because the User-Agent header doesn't get set by default. To set it, use an instance of NSMutableURLRequest rather than NSURLRequest to create the request, as shown below:
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myURL];
[myRequest setValue:#"My App" forHTTPHeaderField:#"User-Agent"];

Where is the delegate code? For async NSURLConnection there needs to be a delegate method to receive the returned data. Other options include sendSynchronousRequest: or if it must be async wrapping sendSynchronousRequest in a GCD block.

Related

WKWebview POST with Authorization Header return 500 Internal server error

WKWebview Load request send 500 when the method set to Post. backend said that it is not even receiving the call. and it is sending a proper error when method is GET.
NSString *fullURL = _URL;
NSString *encodedStringUrl = [fullURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encodedStringUrl];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSString * token = [NSString stringWithFormat:#"bearer %#", [UserDefaults getAccessToken]];
NSDictionary * header = #{#"Content-Type": #"application/x-www-form-urlencoded",
#"Authorization": token};
[requestObj setHTTPMethod:#"POST"];
[requestObj setAllHTTPHeaderFields:header];
[_webview loadRequest:requestObj];
is there any other way to load the request into WKWebView with Authorization header?
If you ever encounter this error with WKWebView POST requests. Please check the server configurations for URL Module Redirect/ SSL redirect/ Cross Domain (CORS) issues. there is nothing to do from Mobile end. I had to make this post because Web and android was working fine and only the iOS was giving this error.

How To Get Key Of goDaddy

Actually I want to created application of shortening URL , i have used GoDady by creating account at http://app.x.co/ But My URL doesnot get shorten.
This is my key
#define kGoDaddyAccountKey #"b201137c009311e6984efa163ee12fa9"
This is actually The method that do work for Shortening URL
- (IBAction)shortenURL:(id)sender
{
NSString *urlToShorten = self.webView.request.URL.absoluteString;
NSString *urlString = [NSString stringWithFormat:#"http://api.x.co/Squeeze.svc/text/%#?url=%#",kGoDaddyAccountKey,
[urlToShorten stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
shortURLData = [NSMutableData new];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
shortenURLConnection = [NSURLConnection connectionWithRequest:request
delegate:self];
}
But I get error like This.
Quite Interesting, Please Help.

Set video metadata after uploading to vimeo

I've successfully uploaded video to vimeo by using API V3. Now, i want to set few metadata likes name, description, privacy.view etc to uploaded video.
When i tried with vimeo playground, it get success: https://developer.vimeo.com/api/endpoints/videos#/{video_id}
But in code, i tried following two way but didn't get success.
NSString *strURL = [NSString stringWithFormat:#"%#videos/%#?access_token=%#&name=%#&description=%#&privacy.view=%#", VIMEO_API_CALL_URL, strVideoID, VIMEO_ACCESS_TOKEN_TEMP, strName, strDescription, strPrivacyView];
NSURL *URL = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
[request setHTTPMethod:#"PATCH"];
and following also,
NSString *strURL = [NSString stringWithFormat:#"%#videos/%#?access_token=%#", VIMEO_API_CALL_URL, strVideoID, VIMEO_ACCESS_TOKEN_TEMP];
NSURL *URL = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
[request setHTTPMethod:#"PATCH"];
[request setValue:#"iOS_QuickTime" forHTTPHeaderField:#"name"];
[request setValue:#"This video is uploaded via iOS applcation and to perform last step by adding all required metadata." forHTTPHeaderField:#"description"];
[request setValue:#"nobody" forHTTPHeaderField:#"privacy.view"];
Here, VIMEO_API_CALL_URL is "https://api.vimeo.com/";
I'm wandering for solution. Till if any one can help me here.
Your access token should be included in a proper authentication header. Something like this:
Authorization: Bearer xxxxxxxxxtokenxxxxxxx
The values you are trying to set (title, description, privacy) should be query params, not header key / values. So your first approach is correct.
You could also consider using the Vimeo iOS SDK (I'm one of the authors) or just browse the codebase to help you get from point A to point B:
https://github.com/vimeo/VIMNetworking
Feel free to file issues on that repo too.

iOS Trying to call web service and I'm getting a NSURLErrorDomain error

I'm trying to connect to a web service that if I hit it through Safari I get back some json, but when I hit it through code I get a NSURLErrorDomain error with the following description.
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be "url" which could..."
Here is the code:
NSURL *url = [NSURL URLWithString:#"webservice_url"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
Can anyone explain how to get around this? Also, it is a https web service.
The website doesn't have a HTTPS certificate. Try looking into this:
https://stackoverflow.com/a/15011030/4716039
In your code, the URLWithString value needs to be an actual URL. It is the same thing as going to Safari and typing in "webservice_url" as the entire URL instead of giving it the entire address. Instead you'll want to do something like the following:
NSURL *url = [NSURL URLWithString:#"https://www.urlgoeshere.com/getJSON"];
You can use like this -
NSURL *baseURL = [NSURL URLWithString:#"http://example.com/v1/"];
[NSURL URLWithString:#"foo" relativeToURL:baseURL];
// http://example.com/v1/foo

ios - making a remote call works on emulator but can not get it to work from live devices

I have a bit of a confusing situation. I have a form after which I create a url and make an asynchronous server call. That data does not need to be secure.
Here is what I have:
NSString *urlString = #"http://my.url.com/script_name.php?subject=hardcoded_string&body=";
NSString *inputString = textArea.text;
NSString *url_to_send = [NSString stringWithFormat:#"%#%#", urlString , inputString];
NSURL *url = [NSURL URLWithString:url_to_send];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url ];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
...
Is there something that I am doing incorrectly here in how I create the url?
Thanks!
it seems ok... take a look at asihttprequest libs, on google... that's a powerful wrapper for http connections... you should now set up the queue and add the request to the queue. finally you have to start the queue.

Resources