0

Please see code below:

+ (void)splashDataFromJSON:(NSData *)objectNotation error:(NSError **)error
{
    NSError *localError = nil;
    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

    if (localError != nil) {
        *error = localError;
    }

    NSMutableArray* btms = [[NSMutableArray alloc] init];
    NSMutableDictionary* btmManufacturerResolutionDictionary = [[BTMCache sharedManager] btmManufacturerResolutionDictionary];

    NSArray *results = [parsedObject valueForKey:@"results"];
    NSLog(@"Count %d", parsedObject.count);

    NSString* imageBaseUrl = [[parsedObject valueForKey:@"general"] valueForKey:@"image_base_url"];
    imageBaseUrl = [imageBaseUrl stringByAppendingString:@"hdpi/"];
    NSString* splashImageName = [[[parsedObject valueForKey:@"general"] valueForKey:@"splash"] valueForKey:@"img"];
    NSString* splashAdvertiserURL = [[[[parsedObject valueForKey:@"general"] valueForKey:@"splash"] valueForKey:@"url"] copy];

    NSMutableString* appendedString = [[NSMutableString alloc] init];
    for(int i =0 ;i<[splashAdvertiserURL length]; i++) {
        char character = [splashAdvertiserURL characterAtIndex:i];
        printf(&character);
        sleep(0.1);
        if (character != "!")
        {
            [appendedString appendFormat:@"%c", character];
        }
    }

    [[SplashData sharedManager] setSplashAdvertiserURL:appendedString];
    [[SplashData sharedManager] setSplashImageName:splashImageName];

    splashAdvertiserURL = [[SplashData sharedManager] splashAdvertiserURL];
}

The point of interest is in splashAdvertiserURL. When I receive this data and print it out using po, it comes out as "https://radar.com/ref/go/84/". This is fine and what was expected. When I look at the incoming data in JSONLint it looks like this:

"general": {
        "image_base_url": "https:\/\/radar.com\/img\/manufacturers\/",
        "splash": {
            "img": "image1.png",
            "url": "https:\/\/radar.com\/ref\/go\/84\/"
        }
    },

As you can see, further on I put the NSString into a singleton with an NSString property. Nothing abnormal here. I then proceed to retrieve it to see that all is ok. Further to this the program continues. In another class I wish to retrieve this information, and when I try and do that, it throws EXC_BAD_ACCESS. There appears to be garbage in there.

I then put in a loop in the code as you can see to print out the characters one at a time. Very curiously, when I print that out using po I get:

https:// r a d ar.com/ref/go/8 4!/"

Exactly in that format. If I then proceed to hardcode the string https://radar.com/ref/go/84/ - including escape characters and everything, then all works fine. No issues. If I handle a normal string incoming without escape characters it stores fine in the singleton as well, no issue. enter code here

I am pretty stumped here as to what is going on. Can someone assist?

Thank you

Zephyr
  • 11
  • 1
  • Shouldn't it be `character != '!'`? (single quote to compare `char`)? If you convert `objectNotation` into `NSString` (`alloc`/`initWithData:encoding:` with UTF8), does it have the `\\` ? – Larme Jan 11 '17 at 15:52
  • Apologies, that code was removed - and pretty much irrelevant. I was busy playing around with it. – Zephyr Jan 12 '17 at 07:24

1 Answers1

0

For URL you received as string you need to encode before use it to in your app. Have a look at below code:

NSString *sampleUrl = @"https:\/\/radar.com\/ref\/go\/84\/";
NSString *encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
 NSUTF8StringEncoding];
Dhaval Dobariya
  • 171
  • 1
  • 12
  • Thank you for the comment. I tried this before, and checked now again. Same issue. Is there perhaps another encoding I needs to use? – Zephyr Jan 11 '17 at 06:47
  • Please try human readable URL string like `NSString *sampleUrl = @"https://radar.com/ref/go/84/";` – Dhaval Dobariya Jan 11 '17 at 12:16