从TinyURL获取实际网址

时间:2011-04-19 15:38:50

标签: objective-c web-services web-applications ios4 redirect

我希望用户从TinyURL或Tiny.cc服务或任何其他URL重定向器获取实际的URL。那么我是否可以从短重定向的URL中获取实际的长URL,而无需在后台运行browswer应用程序?

提前致谢。

标题

#import "UntitledViewController.h"


@implementation UntitledViewController

- (id)init 
{
    self = [super init];
    if (self) 
    {
        NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
        [self loadTinyURL:url];
    }
    return self;
}

- (void)loadTinyURL:(NSURL *)url 
{
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                                  delegate:self];
    if (!connection) 
        NSLog(@"could not connect with: %@", url);
}

- (NSURLRequest *)connection:(NSURLConnection *)connection 
             willSendRequest:(NSURLRequest *)request 
            redirectResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    int statusCode = [httpResponse statusCode];

    // http statuscodes between 300 & 400 is a redirect ...
    if (response && statusCode >= 300 && statusCode < 400) 
        NSLog(@"redirecting to : %@", [request URL]);

    return request;
}

@end

实施

//
//  UntitledViewController.h
//  Untitled
//
//  Created by tushar chutani on 11-04-19.
//  Copyright 2011 Fleetwood park secondary . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UntitledViewController : UIViewController {

}

- (void)loadTinyURL:(NSURL *)url;

@end

2 个答案:

答案 0 :(得分:3)

NSURLConnection有一个回电connection:willSendRequest:redirectResponse:。此时,您可以检查redirectResponse以查看您的目的地。

<强>更新

- (NSURLRequest *)connection:(NSURLConnection *)connection 
             willSendRequest:(NSURLRequest *)request 
            redirectResponse:(NSURLResponse *)redirectResponse
{
    //Make sure tinyurl is doing the redirection
    if([[[redirectResponse URL] host] compare:@"tinyurl.com" 
                                      options:NSCaseInsensitiveSearch] == NSOrderedSame)
    {
        NSLog(@"Redirect Location: %@", [request URL]);
    }

    //call [connection cancel]; to cancel the redirect and stop receiving data
    //return nil; to cancel redirect but continue receiving data
    //return request; will continue the redirection as normal

    return request;
}

答案 1 :(得分:1)

查看以下代码:

#import "TinyURLHandler.h"


@interface TinyURLHandler (Private)
- (void)loadTinyURL:(NSURL *)url;
@end


@implementation TinyURLHandler

- (id)init 
{
   self = [super init];
   if (self) 
   {
      NSURL *url = [NSURL URLWithString:@"http://tinyurl.com/a3cx"];
      [self loadTinyURL:url];
   }
   return self;
}

- (void)loadTinyURL:(NSURL *)url 
{
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                                 delegate:self];
   if (!connection) 
      NSLog(@"could not connect with: %@", url);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
   int statusCode = [httpResponse statusCode];
   NSLog(@"%d : %@", statusCode, [NSHTTPURLResponse 
                                  localizedStringForStatusCode:statusCode]);   
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [connection release];
   NSLog(@"finished");
}

- (NSURLRequest *)connection:(NSURLConnection *)connection 
             willSendRequest:(NSURLRequest *)request 
            redirectResponse:(NSURLResponse *)response
{
   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
   int statusCode = [httpResponse statusCode];

   // http statuscodes between 300 & 400 is a redirect ...
   if (response && statusCode >= 300 && statusCode < 400) 
      NSLog(@"redirecting to : %@", [request URL]); 

   return request;
}

@end

@ opensposter :我不是百分百肯定你为什么要看原始项目,我想这是因为你想弄清楚什么是提取最终网址的好方法?好吧,我建议您创建一个委托,并在获得最终URL后通知调用者。如果您需要一个示例,我可以添加更多源代码......