NSURLConnection。我需要帮助保存收到的数据

发布于 2024-12-06 08:23:44 字数 265 浏览 0 评论 0 原文

我正在启动 NSURLConnection 并且需要保存从互联网接收的数据。 在 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 我需要使用原始 url 作为数据名称以不同名称保存数据... 如何使用异步请求在 connectionDidFinishLoading 中获取此信息(url)? 如果不可能,可以建议我一些其他方法来完成我的要求吗? 谢谢 保罗

I'm starting a NSURLConnection and I need to save data received from internet.
In the - (void)connectionDidFinishLoading:(NSURLConnection *)connection
I need to save data with different name using the original url as name of the data...
How Can get this info (url) in the connectionDidFinishLoading using async request?
If it is not possible can suggest me some other ways to do what I asked?
Thanks
Paolo

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

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

发布评论

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

评论(2

2024-12-13 08:23:44

** 答案仅在 iOS5 之前有效。自 iOS5 以来,Apple 引入了 -originalRequest 方法,该方法允许避免为此特殊目的进行任何进一步的子类化。一般来说,Apple 引入了 NSURLConnection 类如此多的改进,以至于不再需要子类化 NSURLConnection 除非需要不平凡的行为 **
您可以通过添加一个名为的额外属性来子类化 NSURLConnection

NSURL originalURL

and then start it. When the delegate finish method is executed you can retrieve this property and do the rest of the job. *

例如(我会展示相关部分,请不要复制粘贴):

MyURLConnection.h

@interface MyURLConnection:NSURLConnection { @property (nonatomic,retain) NSURL *originalURL; } @end MyURLConnection.m

@implementation MyURLConnection @synthesize originalURL; In your calling class:

MyURLConnection *myConnection = [[MyURLConnection alloc] initWithRequest:myRequest delegate:myDelegate]; myConnection.originalURL = [request URL]; [myConnection start]; and finally in the delegate: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { MyURLConnection *myConn = (MyURLConnection)connection; NSURL *myURL = myConn.originalUrl; // following code }

** Answer valid only pre-iOS5. Since iOS5 Apple introduced the -originalRequest method that allows to avoid any further subclassing for this special purpose. In general Apple introduced the the NSURLConnection class so many improvements that it makes no longer necessary to subclass NSURLConnection unless non-trivial behaviours are required **
You can subclass NSURLConnection by adding an extra property called

NSURL originalURL

and then start it. When the delegate finish method is executed you can retrieve this property and do the rest of the job. *

E.g. (I will show relevant parts, don't copy and paste please):

MyURLConnection.h

@interface MyURLConnection:NSURLConnection { @property (nonatomic,retain) NSURL *originalURL; } @end MyURLConnection.m

@implementation MyURLConnection @synthesize originalURL; In your calling class:

MyURLConnection *myConnection = [[MyURLConnection alloc] initWithRequest:myRequest delegate:myDelegate]; myConnection.originalURL = [request URL]; [myConnection start]; and finally in the delegate: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { MyURLConnection *myConn = (MyURLConnection)connection; NSURL *myURL = myConn.originalUrl; // following code }
万劫不复 2024-12-13 08:23:44

*现在作者不再支持 ASIHTTPRequest 库,因此最好开始采用其他一些库 *

我建议您使用 ASIHTTP 请求。我已经使用这个很久了。下面的代码示例用于从 url 异步下载数据。

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

更新:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      // Use when fetching text data
      NSString *responseString = [request responseString];

      // Use when fetching binary data
      NSData *responseData = [request responseData];

      //here you have access to NSURL variable url.
   }];
   [request setFailedBlock:^{
      NSError *error = [request error];
   }];
   [request startAsynchronous];
}

尝试在 ASIHTTP 中使用 GCD。在块内,您可以访问变量 url

*NOW ASIHTTPRequest Library is No Longer supported by the author so its good to start adopting some other libraries *

I would suggest you use ASIHTTP request. I have been using this for a long time. The below code sample is for downloading data from a url asynchronously.

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

UPDATE:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      // Use when fetching text data
      NSString *responseString = [request responseString];

      // Use when fetching binary data
      NSData *responseData = [request responseData];

      //here you have access to NSURL variable url.
   }];
   [request setFailedBlock:^{
      NSError *error = [request error];
   }];
   [request startAsynchronous];
}

Try using GCD in ASIHTTP. Here inside the block you'll have access to the variable url.

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