NSURL соединение на Watch OS 6 - PullRequest
0 голосов
/ 25 января 2020

Я очень плохо знаком с кодированием, и мои знания в Obj- C очень ограничены. Я пытаюсь портировать некоторый код Objective- C на watchOS для создания кроссплатформенного фреймворка и с ошибками при компиляции. Как я могу преобразовать код ниже, чтобы работать на watchOS 6?

- (void) sendRequest: (NSURLRequest*) request completion:(void (^(NSHTTPURLResponse*,NSData*,NSError*))completion;
{
    if (self) {
        self->_request = request;
        self->_completion = completion;
    }

    if ( _staticHandler != nil) {
        SBStaticHandlerResponse* mockResponse = _staticHandler(request);

        if ( mockResponse != nil) {

            if (completion) {
                NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:nil headerFields:mockResponse.Headers];
            completion(response,mockResponse.Data,nil);
            }
            return;
        }
    }

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    //--> In line above I got error: 
    //--> 'initWithRequest:delegate:' is unavailable: not available on watchOS

    if (!theConnection && completion) {
        NSString* msg = [NSString stringWithFormat:@"Initiate request failed for %@",[request description]];
        completion(nil,nil,[SBNotificationHubHelper errorWithMsg:msg code:-1]);
    }
}

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
{
    if ( _staticHandler != nil) {
        SBStaticHandlerResponse* mockResponse  = _staticHandler(request);

        if ( mockResponse != nil) {
            (*response) = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:nil headerFields:mockResponse.Headers];

            return mockResponse.Data;
        }
    }

    return [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error];
    //--> In line above I got error: 
    //--> 'sendSynchronousRequest:returningResponse:error:' is unavailable: not available on watchOS
}

После поиска я обнаружил, что NSURLConnection не рекомендуется также для watchOS. Решения, которые я нашел, не работают. Где я делаю ошибку?

Я заменил вместо:

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Я использовал:

NSURLSession *theConnection = [[NSURLSession alloc] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
//--> I got warning:
//--> Incompatible pointer types initializing 'NSURLSession *' with an expression of type 'NSURLSessionDataTask *'

и в «return» во второй функции:

Я заменил вместо:

return [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error];

Я использовал:

return [NSURLSession sendSynchronousRequest:request returningResponse:response error:error];
//--> I got error:
//--> No known class method for selector 'sendSynchronousRequest:returningResponse:error:'

Заранее спасибо:)

...