Search code examples
iphoneiosxmlencodingnsmutabledata

Converting XML to NSString


Previously, I was able to get the contents of the URL with this encoding:

<?xml version = "1.0" encoding = "UTF-8"?>

And parse it into NSString with codes shown below:

NSMutableData *receivedData =  [[[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:authenticateURL]] autorelease];
    NSString *string = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];if (string) {   // if connection established, string will look for <error> in XML
        NSRange range = [string rangeOfString:@"<error>"];
        if (range.location == NSNotFound) {
            result = TRUE;
            NSLog(@"Authenticated!");  // if <error> not found, record is present
        }                           //return TRUE to load data
        else {
            result = FALSE;         // <error> found in XML, record is not found
            NSLog(@"Not Authenticated!");
        }
    }

However, the contents I need to fetch now is actually without a type of encoding, example:

<?xml version="1.0" ?><authenticate><auth>Y</auth></authenticate>

Therefore now, as I NSLog the string, it is empty. Have tried searching for other methods but all the examples I found, people were fine with using UTF8StringEncoding. Would be glad for any advice :)!Thanks!


Solution

  • Sorry all, actually the reason why I was unable to retrieve the XML from the URL is because of some SSL problem. The solution I used can be found here: How to use NSURLConnection to connect with SSL for an untrusted cert?

    Thank you everyone for helping too^^