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?

3
  • Hi, is it the first or the second URL that's broken? Commented Oct 8, 2015 at 20:06
  • I think second url seems to be broken Commented Oct 8, 2015 at 20:34
  • stackoverflow.com/questions/12652396/… Commented Oct 26, 2015 at 14:10

3 Answers 3

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]];

Sign up to request clarification or add additional context in comments.

5 Comments

so NSURL will accept decoded URL or do i have to encode it again?
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 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 : gist.github.com/onderozcan/6493f337a9c1dea8058d
Can we discuss this in the chat? chat.stackoverflow.com/rooms/91877/…
sure i joined, and wrote something in. Pls check it @Julian
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.

7 Comments

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
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: gist.github.com/onderozcan/0a11c3b1c55ba2e258b1
What is not working correctly? What are your issues?
check the link that i provided gist.github.com
Yes I've seen that, what are your issues?
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.