Now I am trying to replace the direct URL stored in "buyUrlString" with URL from LinkShare, working off the Apple Tech Q&A QA1629. I believe there is an error in the Apple code. I changed "iTunesLink" in their last line of code to "iTunesURL". Other than that, my current attempt has no errors or warnings, but doesn't do anything either
Code:
- (IBAction) buyButton:(id)sender {
[self openReferralURL];
}
// Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle
- (void)openReferralURL:(NSURL *)referralURL {
NSURL *buyUrl = [[NSURL alloc] initWithString:buyUrlString];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:buyUrl] delegate:self startImmediately:YES];
[conn release];
}
// Save the most recent URL in case multiple redirects occur
// "iTunesURL" is an NSURL property in your class declaration
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
self.iTunesURL = [response URL];
return request;
}
// No more redirects; use the last URL saved
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[UIApplication sharedApplication] openURL:self.iTunesURL];
// Original code from Apple example uses iTunesLink. Think it is meant to be iTunesURL
// [[UIApplication sharedApplication] openURL:self.iTunesLink];
}
Wow. This is really driving me nuts. I found an example at Epsilon Team that is very similar to the Apple code. If I use a static URL (changing "itunes" to "phobos"), it will open the App Store . If I try to load the same URL from a dictionary, it opens in Safari. When I look at the URL that is being retrieved with NSLog, they look the same to me.
Does NOT work: This opens in Safari, where buyUrlString is pulled from a dictionary
I have narrowed this down somewhat, but am still quite puzzled. It seems that whenever a URL is pulled from the dictionary, it is adding extra characters. Any place in the URL where "%25" appears, the code is adding an extra "25". so:
Code:
http%253A%252F%252Fphobos.apple.com%252F
becomes
Code:
http%25253A%25252F%25252Fphobos.apple.com%25252F
Not sure why this is happening, or what should be done to fix it. Have been searching for any encode that might fit this pattern, but am not finding anything. Any insight would be greatly appreciated.
Looks like you caught Apple's mistake. As I noted here, the code provided by Apple has some mistakes, which I took quite some time to catch too.
As to answer your questions:
1) can't help much as I don't know how your dictionary was declared
2) nope, you will still get creditted
fyi, the iPhone and iPod touch looks at the URL and decides whether to open it via Safari or the AppStore application, for this instance, phobos will always open in the AppStore application. when you get your link from the iTunes Link Maker, it will most likely be in "itunes.apple.com" format - just change it...
hope this helps...
p/s: have you tried simulating a failed redirection? my app crashes when it fails to redirect
As to answer your questions:
1) can't help much as I don't know how your dictionary was declared
Thanks for your response. I created the dictionary within XCode by doing:
File > New File > Other > Property List
I set up the keys in XCode, then pasted my values in from various sources. If I open the plist with a text editor, it shows
just a note on the apple example code,
i believe it has a bug.
in this line: "self.iTunesURL = [response URL];",
i believe it retains a reference to a non-durable URL,
and indeed, if the code in the article is called repeatedly (eg the user taps a button repeatedly), it crashes when we attempt to use self.iTunesURL.
in my limited testing, this is fixed by changing the line to:
self.iTunesURL = [[response URL] copy];
i'm still filling in gaps in my Objective C understanding,
so my proposed fix may represent a [small] memory leak,
so if you're going to be hitting the code a lot, probably want a different fix.