在方法内部使用Asynchronous方法具有返回类型

时间:2016-07-01 08:37:24

标签: ios web

我正在使用NSURLProtocol来处理来自Webview的请求。我试图将请求从主机转换为IP地址,然后将其发送出去。我将这些逻辑放在

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
        Here is an asynchronous method which gives a NSString by using a block. 
        I need to convert this NSString back to NSURLRequest, 
        so the webview can load the content. 
}

2 个答案:

答案 0 :(得分:0)

您可以使用block参数在块中返回值,然后在完成处理程序块中加载此请求。

+ (void)canonicalRequestForRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *request))completionHandler {
//    Here is an asynchronous method which gives a NSString by using a block.
//    I need to convert this NSString back to NSURLRequest,
//    so the webview can load the content.


    // call completionHandler inside of asynchronous block
    completionHandler(returnRequest);
}

答案 1 :(得分:0)

这是答案,它提供了一个包装器方法来创建异步方法,使其行为类似于同步方法并返回值。它基本上使用while循环来阻止返回。

how to wrap an asynchronous method that takes a block and turn it synchronous in objective c