如何在nsurlconnection完成之前暂停程序?

时间:2020-12-14 16:10:01

I know that people have asked this but I have not found satisfactory answers. I have one method that I send all my URLRequests through. I return the response of the request as a string when the method completes. I have recently added ssl to my program. This means that I can no longer use a synchronous request because I need to take advantage of the didReceiveAuthenticationChallenge function as my credentials are currently self-signing. The program needs the response from the URL in order to continue so there is not harm in waiting for the response. However, I cannot seem to find a way to just hold the code up and continue once completed. I can alert the original function that called to request function but I would like the program to pick up right after that call. And it has unique code below such calls so I cannot specialize the connectionDidFinishLoading: function because each method who calls this is different.

我知道有人问这个,但我没有找到满意的答案。我有一个方法可以通过我发送所有的URLRequests。当方法完成时,我将请求的响应作为字符串返回。我最近在我的程序中添加了ssl。这意味着我不能再使用同步请求,因为我需要利用didReceiveAuthenticationChallenge功能,因为我的凭据当前是自签名的。程序需要来自URL的响应才能继续,以便等待响应没有损害。但是,我似乎无法找到一种方法来保持代码并继续完成一次。我可以提醒调用请求函数的原始函数但我希望程序在该调用之后立即获取。它在这些调用下面有唯一的代码,所以我不能专门研究connectionDidFinishLoading:函数,因为调用它的每个方法都不同。

How can I pause the program so I can return the nsdata from the connection to the methods that called it?

如何暂停程序以便将nsdata从连接返回到调用它的方法?

Here is some pseudo-code to show you what I mean:

这是一些伪代码,向您展示我的意思:

- (void) login:(NSString *)username :(NSString *)password {
     NSString *str = [NSString stringWithFormat:%@"%@:::%@",username,password];
     NSURL *url = [NSURL urlWithString:@"https://blahblahblah"];
     NSString *result = [self connectToUrl:str:url];
     if ([result isEqualToString:@"valid"]) {
           //this would be more complex in here
           NSLog(@"hooray");
     } else {
           NSLog(@"bummer");
    }
}


- (NSString *)connectToUrl:(NSURL *)url :(NSString *)str {

     NSData *FileData = [str dataUsingEncoding: NSUTF8StringEncoding];



    NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];

    //set up the rest of the request...

     ...

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
   [connection start];

    //WOULD LIKE TO PAUSE HERE UNTIL COMPLETE!  THEN CONTINUE

    // received data is assigned in didReceiveData: method
    return [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];  

}

But alas, I cannot do this because I cannot make the final line wait until the connection is complete... Please help me!

但是,我不能这样做,因为我不能让最后一行等到连接完成后......请帮助我!

Very appreciative!

R

1 个解决方案

#1


3  

iOS and OS X and much of the Cocoa/Cocoa touch frameworks are built on an event model. You don't pause your app. That's not the proper approach. You need to start the connection and then move on. When the connection completes, you act on that event.

iOS和OS X以及大多数Cocoa / Cocoa触摸框架都是基于事件模型构建的。你不要暂停你的应用程序。那不是正确的做法。您需要启动连接然后继续。连接完成后,您将对该事件执行操作。

In other words, your login method can't sit and wait for the result. It should start the connection and return.

换句话说,您的登录方法无法坐下来等待结果。它应该开始连接并返回。

When you get the result of the connection you call some method to process the login result.

当您获得连接的结果时,您调用一些方法来处理登录结果。

Making use of blocks can make things like this easier but there are other ways. You just need to stop thinking about such things in a linear fashion. Dealing with asynchronous processing requires a different approach.

利用块可以使这样的事情变得更容易,但还有其他方法。你只需要以线性方式停止思考这些事情。处理异步处理需要不同的方法。

#1


3  

iOS and OS X and much of the Cocoa/Cocoa touch frameworks are built on an event model. You don't pause your app. That's not the proper approach. You need to start the connection and then move on. When the connection completes, you act on that event.

iOS和OS X以及大多数Cocoa / Cocoa触摸框架都是基于事件模型构建的。你不要暂停你的应用程序。那不是正确的做法。您需要启动连接然后继续。连接完成后,您将对该事件执行操作。

In other words, your login method can't sit and wait for the result. It should start the connection and return.

换句话说,您的登录方法无法坐下来等待结果。它应该开始连接并返回。

When you get the result of the connection you call some method to process the login result.

当您获得连接的结果时,您调用一些方法来处理登录结果。

Making use of blocks can make things like this easier but there are other ways. You just need to stop thinking about such things in a linear fashion. Dealing with asynchronous processing requires a different approach.

利用块可以使这样的事情变得更容易,但还有其他方法。你只需要以线性方式停止思考这些事情。处理异步处理需要不同的方法。