iPhone:使用 NSURL 请求异步下载数据,而不是 [NSData datawithContents ofURL]

发布于 2024-11-07 02:25:55 字数 1894 浏览 5 评论 0原文

您好,我是一名新手,我正在尝试从 googleapi 网页下载地理编码数据。我使用以下代码做到了这一点:代码:

        NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp];
        NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address];
        [address appendString:@" "];
        [address appendString:l.CityName];
        [address appendString:@" "];
        [address appendString:l.State];
        [address appendString:@" "];
        [address appendString:l.PostalCode];
        NSArray *addressArray = [address componentsSeparatedByString:@" "];
        [address release];
        NSString *a = [addressArray componentsJoinedByString:@"+"];
        [urlStr appendString:a];
        [urlStr appendString:@"&sensor=false"];
        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *data = [NSData dataWithContentsOfURL:url];

它工作得很好,但问题是它锁定了我的 GUI,因为它都是同步完成的。

有人建议我使用 NSURLrequest 异步执行此操作。于是我就去了苹果网站。从那里我看到了他们的例子。然而,我还是卡住了。如何将上面的同步请求代码转换为异步请求?到目前为止,这就是我所拥有的,但我不确定这是否有效......有点困惑......有人可以用这段代码为我指出正确的方向吗?

        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        //create NSURL request for asynchronous processing
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];

        NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if (theConnection) 
           {
            receivedData = [[NSMutableData data] retain];
           }
        else
           {
            // Inform the user that the connection failed.
           }

Hi Im a newbie and Im trying to download geocoding data from googleapi webpage. I did it using this: code:

        NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp];
        NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address];
        [address appendString:@" "];
        [address appendString:l.CityName];
        [address appendString:@" "];
        [address appendString:l.State];
        [address appendString:@" "];
        [address appendString:l.PostalCode];
        NSArray *addressArray = [address componentsSeparatedByString:@" "];
        [address release];
        NSString *a = [addressArray componentsJoinedByString:@"+"];
        [urlStr appendString:a];
        [urlStr appendString:@"&sensor=false"];
        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *data = [NSData dataWithContentsOfURL:url];

It worked fine but the glitch was that it locked up my GUI since it was all done synchronously.

Someone suggested I do it asynchronously using NSURLrequest. So I went to the apple website. from there I saw their example. However, Im still stuck. How do I convert the above code which is a synchronous request to an asynchronous one? So far this is what I have, but Im not sure if this works...... a bit confused ... could someone please point me in the righ direction with this code?

        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        //create NSURL request for asynchronous processing
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];

        NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if (theConnection) 
           {
            receivedData = [[NSMutableData data] retain];
           }
        else
           {
            // Inform the user that the connection failed.
           }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-11-14 02:25:55

使用 NSURLConnection

根据您构建代码的方式,您需要在成功完成 (connectionDidFinishLoading:) 或失败 (connection: didFailWithError:) 时采取行动或通知其他对象(委托、通知)。

Using NSURLConnection

Depending on how you structure your code, you'll want to act or notify some other object (delegate, notification) on successful completion (connectionDidFinishLoading:) or failure (connection: didFailWithError:).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文