Strange error with initWithContentsOfURL

I am currently working on reading some XML from an external API.

The following code works fine:

NSError *error = nil;

NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

However, I wanted to add some error handling ("Unable to reach server", "No internet connection", etc.). So, I put this block in place with a little help from the Reachability code example:

if ([error code] == kCFURLErrorNotConnectedToInternet) {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Internet connection",@"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
        NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
        [self handleError:noConnectionError];
    } else {
        // otherwise handle the error generically
        [self handleError:error];
    }

The handleError method simply displays a UIAlertView with error data. When I started the application without a connection, I expected to see my error message. However, all I have ever received is "The operation could not be completed (Cocoa error 256)," which, from what I can collect, is a common reading error.

, TouchXML, , , NSString initWithCOntentsOfURL.

- ?

,

+3
2

, kCFURLErrorNotConnectedToInternet. , . userInfo dict , , . , , , .

userInfo dict , URL-, , - . , . URL- .

0

: 1) NSData dataWithContentsOfURL 2) xmlString , NSData, , , , (data)

0

Source: https://habr.com/ru/post/1727395/


All Articles