objective-c - 在 Cocoa 中获取短 URL 的长 URL 的最简单方法?

标签 objective-c cocoa short-url

在 Cocoa 中检索短 URL 的原始 URL 的最简单方法是什么?有什么可以在几行内完成的事情吗?

最佳答案

更新:我刚刚看到您的评论并意识到它遵循重定向。

请参阅委托(delegate)方法:connection:willSendRequest:redirectResponse:,它告诉您它正在根据之前的响应重定向到此新请求。

您可以从此处的新请求或重定向响应的 Location header 获取扩展的 URL。

Discussion If the delegate wishes to cancel the redirect, it should call the connection object’s cancel method. Alternatively, the delegate method can return nil to cancel the redirect, and the connection will continue to process. This has special relevance in the case where redirectResponse is not nil. In this case, any data that is loaded for the connection will be sent to the delegate, and the delegate will receive a connectionDidFinishLoading or connection:didFailLoadingWithError: message, as appropriate.

原始答案如下...

NSURLConnection 与委托(delegate)一起使用。在委托(delegate)的 connection:didReceiveResponse: 方法中,获取 allHeaderFields 并读取“Location” header 的值。

类似于:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Expanded URL = %@", [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Location"]);
}

我会创建一个小 URLExpander 类来亲自执行此操作,其签名如下:

+(void)asyncExpandURL:(NSURL *)aURL didExpandTarget:(id)target selector:(SEL)selector;

然后只需在消息中传回两个参数,一个用于短 URL,一个用于长 URL。

关于objective-c - 在 Cocoa 中获取短 URL 的长 URL 的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4480903/

相关文章:

iphone - Facebook 安装的参数不返回数据

cocoa - 电子签名 Mac OS X

objective-c - 如何在基于文档的应用程序中的应用程序启动时执行代码

web-services - 短网址服务如何运作?

mongodb - 在分布式数据存储中生成短 URL

ios - CoreData 在 NSFetchRequest 找到新对象之前需要一些时间

objective-c - Objective-C - 如何在数字中添加数字?

cocoa - Model View Controller 架构 - 示例代码

php - 我的站点 htaccess 需要短 URL

objective-c - 如何填充内存以便稍后释放它?