我的应用使用标准的sharedApplication.OpenURL直接打开iTunes Affiliate链接。但是,Apple建议使用以下代码。
是否有支持Xamarin的C#版本?
// Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle
(void)openReferralURL:(NSURL *)referralURL
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:referralURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[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
{
if (response) {
NSMutableURLRequest *r = [[request mutableCopy] autorelease]; // original request
[r setURL: [request URL]];
self.iTunesURL = [r URL];
if ([self.iTunesURL.host hasSuffix:@"itunes.apple.com"]) {
[[UIApplication sharedApplication] openURL:self.iTunesURL];
}
return r;
} else {
return request;
}
}
答案 0 :(得分:3)
找不到现有的API,所以我写了:
享受!
NSUrlRequest theRequest;
NSUrlConnection conn;
void Process (NSUrl url)
{
theRequest = new NSUrlRequest (url, NSUrlRequestCachePolicy.UseProtocolCachePolicy, 30);
var del = new LinkDelegate ();
conn = new NSUrlConnection (theRequest, del);
conn.Start ();
}
}
class LinkDelegate: NSUrlConnectionDelegate
{
public override NSUrlRequest WillSendRequest (NSUrlConnection connection, NSUrlRequest request, NSUrlResponse response)
{
if (response!=null) {
var url = request.Url;
if (url.Host.ToLower ().Contains ("itunes.apple.com"))
UIApplication.SharedApplication.OpenUrl (url);
return NSUrlRequest.FromUrl (url);
} else {
return request;
}
}
}