22

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.

Kenster
  • 23,465
  • 21
  • 80
  • 106
Neelesh
  • 3,673
  • 8
  • 47
  • 78

7 Answers7

18

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks for your message. But I found something more interesting I have the following code. NSURL *serverUrl = [NSURL URLWithString:@"http://google.com"]; NSError *error = nil; NSString *stringFromStream = [NSString stringWithContentsOfURL:serverUrl encoding:NSUTF8StringEncoding error:&error]; NSLog(@"string is %@ error is %@", stringFromStream, error); I got following error string is (null) error is Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x6a36c60 {NSURL=http://google.com, NSStringEncoding=4} – user454083 May 29 '12 at 09:59
14

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!

user3000868
  • 455
  • 4
  • 13
3

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.

James
  • 41
  • 1
3

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.

StuFF mc
  • 4,137
  • 2
  • 33
  • 32
2

More reasons that might be causing this specific error:

  1. SSL is misconfigured on the server
  2. The server redirects (301) the http URL to https (see #1)
  3. App transport security also uses this code for blocked requests.
Community
  • 1
  • 1
nschum
  • 15,322
  • 5
  • 58
  • 56
0

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"

Aragorn
  • 34
  • 5
0

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.