c++ - NSURLConnection 和 NSURLRequest 的纯 C++ 等价物

标签 c++ objective-c nsurlconnection nsurl nsurlrequest

我正在为 Android 使用 native C++ 移植游戏。我需要知道我应该使用哪种方法来仅在 C++ 中实现与 NSURLConnectionNSURLRequest 相同的功能。还有如何为 C++ 实现以下所有委托(delegate)函数。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

请指教。

最佳答案

无法直接在 C++ 中实现 Objective-C 委托(delegate)方法。您真正希望做的最好的事情是使用 Objective-C++ 创建一个带有实例变量的 C++ 对象,该实例变量是一个 Objective-C 对象,其唯一目的是NSURLConnection 委托(delegate)并将这些方法调用转发给拥有的 C++ 对象。 C++ 对象应该拥有/保留 Objective-C 对象,并负责将其作为 NSURLConnection 对象的委托(delegate)插入。

上述模式的极端实现可能如下所示:

URLConnection.h

#ifndef __Cplusplus__URLConnection__
#define __Cplusplus__URLConnection__

#include <string>

class URLConnection
{
public:
    URLConnection(std::string url);
    ~URLConnection();

    void DidReceiveResponse(const void* response);
    void DidReceiveData(const void* data);
    void DidFailWithError(std::string error);
    void DidFinishLoading();

    void* mNSURLConnection;
    void* mDelegate;
};

#endif /* defined(__Cplusplus__URLConnection__) */

URLConnection.mm

#include "URLConnection.h"

@interface PrivateNSURLConnectionDelegate : NSObject <NSURLConnectionDelegate>
{
    URLConnection* mParent;
}
- (id)initWithParent: (URLConnection*) parent;
@end

@implementation PrivateNSURLConnectionDelegate

- (id)initWithParent: (URLConnection*) parent
{
    if (self = [super init])
    {
        mParent = parent;
    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    mParent->DidReceiveResponse(response);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    mParent->DidReceiveResponse(data.bytes);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    mParent->DidFailWithError(std::string([[error description]UTF8String]));
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    mParent->DidFinishLoading();
}

@end


URLConnection::URLConnection(std::string url)
{
    this->mDelegate = [[PrivateNSURLConnectionDelegate alloc] initWithParent: this];
    NSURLRequest* req = [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithUTF8String: url.c_str()]]];
    this->mNSURLConnection = [[NSURLConnection alloc] initWithRequest: req delegate: (id)this->mDelegate];
}

URLConnection::~URLConnection()
{
    [(NSObject*)this->mNSURLConnection release];
    [(NSObject*)this->mDelegate release];
}

void URLConnection::DidReceiveResponse(const void* response)
{
    // Do something...
}
void URLConnection::DidReceiveData(const void* data)
{
    // Do something...

}
void URLConnection::DidFailWithError(std::string error)
{
    // Do something...
}

void URLConnection::DidFinishLoading()
{
    // Do something...
}

归根结底,NSURLConnection 是一个 Objective-C 对象。不使用 Objective-C 就无法与之交互。

关于c++ - NSURLConnection 和 NSURLRequest 的纯 C++ 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13981937/

相关文章:

objective-c - UIPickerView viewForRow UILabel 忽略框架

ios - NSURLSessionUploadTask 后台持续上传

c# - 以 'typesafe' 方式将数组从 c++/cli 传递到 C#

c++ - 使用 GDB 调试核心转储时知道谁是继承者

c++ - 如何链接GLX?

objective-c - 加载新页面时为 Spinner 设置动画

ios - 横向模式下事件指示器未居中

ios - NSURLConnection 失去连接

iphone - 请求超时,找不到指定的服务器

c++ - 未初始化的局部变量是最快的随机数生成器吗?