0

我正在为 Android 移植一个使用原生 C++ 的游戏。NSURLConnection我需要知道我应该使用哪种方法来实现与NSURLRequest仅在 C++ 中相同的功能。还如何获得为 C++ 实现的所有以下委托函数。

-(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

请指教。

4

2 回答 2

1

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

上述模式的一个极其幼稚的实现可能如下所示:

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.

于 2012-12-21T00:18:08.027 回答
0

LibCURL 可能是用于处理网络/互联网连接的跨平台 C/C++ 实现的最著名的库。如果将它与线程之类的异步技术结合起来,就可以开始模拟 NSURLConnection。

See here [https://curl.haxx.se/libcurl/c/multithread.html] for an example. You'll have to create a class that manages the CURL reference and an appropriate std::function delegate, that you will call when the connection finishes/errors/aborts etc. When using it, you can pass C++14 lambda functions to act as stand-ins for Objective C delegates.

于 2018-02-24T14:00:10.753 回答