从NSURLConnection捕获重定向的地址

时间:2011-01-08 23:13:05

标签: multithreading cocoa redirect nsurlconnection blocking

我正在开发一个遵循HTTP重定向的软件,该重定向由服务器根据参数进行动态计算。我不想在Mobile Safari中显示主服务器,而只想显示重定向的地址。 以下代码工作:

    request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    // Extract the redirected URL
target = [response URL];

问题是服务器需要几秒钟才能回答。 sendSynchronousRequest完全阻止应用程序这个时候很乱,我甚至无法显示“忙碌”的动画。

有没有人知道如何在没有safari外观的情况下异步检索重定向地址,同时重定向服务器URL或在sendSynchronousRequest期间显示某种“耐心”动画? sendSynchronousRequest在另一个帖子中传递会有什么缺点?

1 个答案:

答案 0 :(得分:3)

使用NSURLConnection提供的异步行为,并提供与实现-connection:willSendRequest:redirectResponse:的委托的连接

然后阅读Five Reasons Why Synchronous Networking Is Bad