2011-04-20 45 views
1

任何人都可以提供给我一个吗?连接:willSendRequest:redirectResponse示例项目

.H

#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); 
else { 
    NSLog(@"this works"); 
} 

} 


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

回答

0

可以使用NSURLConnection的samplecode基于异步或同步从苹果website.you可以知道为什么我们通过stackoverflow link

使用该委托方法
+0

嗨,感谢您的链接,我试过这样做,但它不工作,出于某种原因,我将通过问题更新与我的代码.h和.m请帮我在这 – 2011-04-20 03:45:44

+0

你是否继承了NSURLConnection的委托方法,你的问题是什么,你只给出了代码..然后你在方法中加入了断点来检查它是否调用 – 2011-04-20 03:53:25