AWS API SDK provides invalid URL - ios

I created an API using AWS and generated the SDK for iOS use. However, when I try to call the API, I get this error.
Error occurred: Error
Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo={NSUnderlyingError=0x1546a36b0{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=(null)https://(api-path),...}
The URL is hard-coded in the SDK and does not have any sort of (null) in the string. I have no clue where this is coming from.

Replacing the following code in the AWS-generated init(configuration: AWSServiceConfiguration) worked for me:
Original:
if let endpoint = configuration.endpoint {
self.configuration.baseURL = endpoint.URL
}
Replace by:
self.configuration.baseURL = URL(string: URLString)
I don't understand why it says the endpoint is nil even though it has successfully been created immediately beforehand. Anyway, it might help you solve the issue.

Check your NSURL object to see if it is nil when you init it from NSString.
Then check your url in NSString to see if it contains any white spaces, which make your url invalid.
To solve that you have to add this to your url:
NSString *yourUrl; // Your url in NSString type.
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encoded];
To those who downvoted my answer:
If you downvoted because it wasn't written in swift, I just tried my best to help.
Just because I don't have experience with swift programming, doesn't mean I have to stop helping others to solve their problems.

Related

Properly escaping characters for AFNetworking 2.0 URL string

I have a api which needs to use a url string in order to work (wouldn't work with the normal NSDictionary request). The string I'm trying to use is
http://10.1.10.25:8181/config?param={"obj":["hours"]}
However, the following code which I used to escape the characters does not work. It returns a bad url error. What is the proper way to escape characters here?
NSURL *url = [NSURL URLWithString:#"http://10.1.10.25:8181/config?param={\"obj\":[\"hours\"]}"];
// Error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x8ca3780 {NSUnderlyingError=0x8ca6980 "bad URL", NSLocalizedDescription=bad URL}
Below is a picture of a working example using a online REST service.
In this case you just need to encode your param like this:
http://10.1.10.25:8181/config?param=%7B%22obj%22%3A%5B%22hours%22%5D%7D

Instagram Getting user details

Am trying to integrate instagram in my app,while getting user details after getting access token,its showing error . Am appending data in NSData and trying to print it in connectionDidFinishLoading(NSURLConnection *)connection ( NSURLConnection Delegate) . While converting NSData to id ,its showing null .Also if i am converting NSdata to NSString am getting Response as
{"meta":{"error_type":"OAuthAccessTokenException","code":400,"error_message":"The access_token provided is invalid."}}{"meta":{"code":200},"data":{"username":"XXXX","bio":"","website":"","profile_picture":"http:sampleiamge.jpg","full_name":"samplename","counts":{"media":1,"followed_by":0,"follows":0},"id":"sampleID"}}
If am printing the json as id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
getting error as
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0xa967bc0
{NSDebugDescription=Garbage at end.}
Tried all jsonreading option ,but no result
Any hep will be appreciable.
Thanks
You could also have a look at the InstagramKit which does this fairly straight forward. https://github.com/shyambhat/InstagramKit
There's a demo app so you can see how the authentication is done and request a user's details in just a few lines of code. You don't really need to mess around with the access token data format.
It seems like you are not authenticate.
There is a project in GitHub that implements a simple UIViewController that performs the Instagram auth process step by step:
https://github.com/Buza/Instagram-Auth-iOS
You can see all process from request token to error management.
Hope it helps!
The response occurs when the access_token is invalid, try printing (NSLog) out the URL your are making the request and make sure the access_token is correctly passed.

SocketRocket - Invalid Sec-WebSocket-Accept

When trying to connect to a custom socket server using SocketRocket I am getting the error:
Error Domain=SRWebSocketErrorDomain Code=2133 "Invalid Sec-WebSocket-Accept response" UserInfo=0x8f6af00 {NSLocalizedDescription=Invalid Sec-WebSocket-Accept response}
I have linked this back to the _checkHandshake method in _HTTPHeadersDidFinish. The _checkHandshake method is doing the following:
NSString *acceptHeader = CFBridgingRelease(CFHTTPMessageCopyHeaderFieldValue(httpMessage, CFSTR("Sec-WebSocket-Accept")));
I have found this question and have the latest version of socketrocket but doesn't seem to be working?
https://github.com/square/SocketRocket/issues/24
Please let me know how I get around this error (SocketRocket - Invalid Sec-WebSocket-Accept)
I confirm that (using a SockJS server) by changing this:
NSURL *url = [NSURL URLWithString:#"http://localhost:9090"];
to this:
NSURL *url = [NSURL URLWithString:#"http://localhost:9090/websocket"];
the error was gone.

initWithContentsOfURL returns nil only in iOS 5.x [duplicate]

I have been stuck with this for a while and don't seem to get around this.
I am trying to read the contents of an URL as a string from an URL, But i get a weird
Error -> Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"
My code :
fetchedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"www.example.com/iphone"] encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#",fetchedString);
// if there is something wrong with the URL
if (error) {
NSLog(#"Error -> %#", error);
return ;
}
What am I doing wrong? I tried using getting as NSData as well, but I get null back.
Yes, the URL is missing the scheme: "http://".
"Error -> Error Domain=NSCocoaErrorDomain Code=256"
For the error code check the Apple documentation:
NSError codes in the Cocoa error domain.
NSFileReadUnknownError = 256,
NSFileReadUnknownError
"Read error, reason unknown"
Not that the error definition is very helpful. :-)
Also do not check if error is nil to determine if there is an error, check the return value for nil. error is not guaranteed to be nil on successful execution.
I had a similar problem accessing files located on my device. I followed NSURL isFileURL always returns NO
and used [NSURL fileURLWithPath] instead of [NSURL URLWithString] - this worked!
I got this error (Error Domain=NSCocoaErrorDomain Code=256) as soon as our ssl certificate expired. that may not help you but could help someone else.
Sandboxing
If you're using sandboxing in your app, you might want to check that com.apple.security.network.client is set to YES. It's in the the General tab of your Target in Xcode 5 under
Network: Outgoing Connections (Client)
Also be aware that if you see a code 257 when trying to reach a file:/// url, that's also probably because of sandboxing, but this time rather the File Access part. Because I didn't want to open it to anything else than `com.apple.security.files.user-selected.read-write'
User selected files
I preferred to use A Dead Simple Fileserver and use http://localhost:3000 when in Debug mode.
More reasons that might be causing this specific error:
SSL is misconfigured on the server
The server redirects (301) the http URL to https (see #1)
App transport security also uses this code for blocked requests.
I got the same error. The above marked answer is perfect. But in my case, I had the "http://" in the url but had to add the port number in the url request since there is a service running on a specific port that is actually responding to your request.
#"http://example.com:8084/yyy.zzz"
I got the same error, the above solution didn't work for me, in my case i was calling dataWithContentsOfURL from within a UNNotificationServiceExtension so i had to update the info.plist file of the UNNotificationServiceExtension with the app transport security entries.

iOS - NSURLConnection - Proper encoding of characters in request

In my iOS application, i'm posting the request to server using NSURLConnection. The following is the code that I'm using:
NSString* str = #"http://www.myserver.com/post.php?action=rating&grade=A+&name=vasu";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
If you see the grade, its equal to "A+"
But its posting only "A" as grade to server. Even though when I'm encoding the string appropriately, why is it not able to post the request properly?
Is it something that has to be handled on server?
The + sign is a reserved character for URL encoding (for 'space'). Although the escaped value for + should be %2B after stringByAddingPercentEscapesUsingEncoding, well... it isn't. What you can do though is to add a simple category into NSString to bypass this problem. You can find a relevant blog post here and some information on URL encoding here

Resources