11

I am making player which going to support Google DFP adserver. The Ad-server has a VAST link which is XML type and contains media-file, track links, clickLink etc...

The clickLinks includes a redirecting url to the original url which I could not properly decode.

The link looks like: https://pubads.g.doubleclick.net/aclk?sa=L&ai=B2JSpRFcKVq_SAsifbr-gqPAE9-CGpQcAAAAQASCPsd4lOABY37Owp4gCYJn-noa4IboBCzYyNHgzNTJfeG1syAEF2gEFaHR0cDqpArGOjXR685U-wAIC4AIA6gIULzEzMTMwMjQwNy9Vem1hbl9Ea3n4AvTRHpADhAeYA4QHqAMB0ASQTuAEAZAGAaAGI9gHAQ&num=0&cid=5Ggrgwo88gHDUD7JBb6uTLxZ&sig=AOD64_0xhsqN4jSnVOZ-eKo9KCVTet61iQ&client=ca-pub-3069068742246799&adurl=http://dkykartal.com/%3Futm_source%3DUzmanTV%26utm_medium%3DVideo_PreRoll%26utm_content%3DDKY_Kartal%26utm_campaign%3DDKY_Kartal_IBillBoard

I tried to decode by using stringByAddingPercentEscapesUsingEncoding and then encode again but NSURL seems to be broken.

Original VastURL is:

https://pubads.g.doubleclick.net/gampad/ads?sz=624x352&iu=/131302407/Uzman_Dky&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1&url=[referrer_url]&description_url=[description_url]&correlator=1443516172

How can properly cast this to an NSURL?

sunny
  • 3,853
  • 5
  • 32
  • 62
Onder OZCAN
  • 1,556
  • 1
  • 16
  • 36

3 Answers3

5

Read your question again, carefully. You say you want to decode the URL and call a method that's called

stringByAddingPercentEscapesUsingEncoding
        ^^^^^^

Try using stringByRemovingPercentEncoding. This should decode your URL.

To the other question, how to "cast" it to NSURL: Casting means you are forcing the debugger to treat a variable like a specific type. Meaning you cannot cast an object and turn it into an instance of another type.

You have to create an instance of NSURL using a string:

NSURL *URL = [NSURL URLWithString:[URLString stringByRemovingPercentEncoding]];

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • so NSURL will accept decoded URL or do i have to encode it again? – Onder OZCAN Oct 09 '15 at 12:07
  • URL encoding is only needed in browsers. In a "real electronic world" you would never need no escaping. `URLWithString` converts a string into URL. Thats it. Don't think of "lets do as much as can for the computer, he is too stupid anyway". Remember; you are developing for an Apple platform. Use it as you would do in real life. Don't think of abstractions like encoding. Apple will do it all for you! – Julian F. Weinert Oct 09 '15 at 12:42
  • Julian thank you for your help. I successfully Decoded URL but there's something not going well for decoding operation. I just wrote down on gist which my original Vast Link ( coming from xml ) , Decoded URL (Not working redirection occured). the method that i've changed : https://gist.github.com/onderozcan/6493f337a9c1dea8058d – Onder OZCAN Oct 09 '15 at 14:20
  • Can we discuss this in the chat? http://chat.stackoverflow.com/rooms/91877/discussing-properly-decoding-string-url-for-casting-in-nsurl – Julian F. Weinert Oct 09 '15 at 17:41
  • sure i joined, and wrote something in. Pls check it @Julian – Onder OZCAN Oct 11 '15 at 17:35
0

You can use componentsSeparatedByString.

Example:

NSString* url = @"https://pubads.g.doubleclick.net/gampad/ads?sz=624x352&iu=/131302407/Uzman_Dky&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1&url=[referrer_url]&description_url=[description_url]&correlator=1443516172";

NSArray* components = [url componentsSeparatedByString:@"&"];
for (NSString* str in components)
{
    NSArray* keyval = [str componentsSeparatedByString:@"="];
    // keyval[0] is your key
    // keyval[1] is your value
    NSLog(@"key: %@ | value: %@", keyval[0], keyval[1]);
}

Example output:

 key: https://pubads.g.doubleclick.net/gampad/ads?sz | value: 624x352
 key: iu | value: /131302407/Uzman_Dky
 key: impl | value: s
 key: gdfp_req | value: 1
 key: env | value: vp
 key: output | value: xml_vast2
 key: unviewed_position_start | value: 1
 key: url | value: [referrer_url]
 key: description_url | value: [description_url]
 key: correlator | value: 1443516172

The URLs you got, can be decoded by stringByRemovingPercentEscapesUsingEncoding, to remove all escape sequences.

Leandros
  • 16,805
  • 9
  • 69
  • 108
  • So what my steps going to be ? 1. Seperating URI by components and adding in to Array 2. Decode each component with stringByAddingPercentEscapesUsingEncoding 3. concat all components and create new NSString 4. Cast NSString in to NSURL urlWithString: Are these true steps? Regards – Onder OZCAN Oct 02 '15 at 13:25
  • I just tried to create method but output of URL not working correctly. Here is the gist link that you can easily read my code. Thank you again: https://gist.github.com/onderozcan/0a11c3b1c55ba2e258b1 – Onder OZCAN Oct 02 '15 at 14:13
  • What is not working correctly? What are your issues? – Leandros Oct 05 '15 at 07:15
  • check the link that i provided gist.github.com – Onder OZCAN Oct 05 '15 at 07:24
  • 1
    Yes I've seen that, what are your issues? – Leandros Oct 05 '15 at 07:29
  • The encoded NSURL output is totaly wrong could you check the output URL and original URL strings? – Onder OZCAN Oct 05 '15 at 11:49
  • have u tried to encode and cast NSURL , it does not. – Onder OZCAN Oct 08 '15 at 08:58
0

The problem was, url relies on CDATA thats why it could not decode/encode properly. I used - (void) parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{ delegation method and encoded the string like this:

NSString *string = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

Than I assigned to NSURL without any problem.

sunny
  • 3,853
  • 5
  • 32
  • 62
Onder OZCAN
  • 1,556
  • 1
  • 16
  • 36