0

从其他问题看来,我所要做的就是在我的变量前面添加 __block,但是,它似乎对我不起作用。

在块内,使用 NSLog() 检查时,令牌被正确分配。如果我在返回令牌之前再次检查它;它变为 NULL。

- (NSString *)extractTokenFromURL:(NSURL *)tokenURL
{
__block NSString *token = nil;

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                      delegate:self
                                                 delegateQueue:nil];
[[session dataTaskWithURL:self.tokenURL
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (!error) {
        NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
        if (httpResp.statusCode == 200) {
            NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSRange divRange = [content rangeOfString:@"<div id='token' style='display:none;'>" options:NSCaseInsensitiveSearch];
            if (divRange.location != NSNotFound) {
                NSRange endDivRange;
                endDivRange.location = divRange.length + divRange.location;
                endDivRange.length   = [content length] - endDivRange.location;
                endDivRange = [content rangeOfString:@"</div>" options:NSCaseInsensitiveSearch range:endDivRange];

                if (endDivRange.location != NSNotFound) {
                    divRange.location += divRange.length;
                    divRange.length  = endDivRange.location - divRange.location;

                    dispatch_async(dispatch_get_main_queue(), ^{
                        token = [content substringWithRange:divRange];
                    });
                }
            }
        }
    }
}] resume];

return token;
}
4

1 回答 1

2

那是因为任务是异步运行的,所以方法会在token分配之前返回。

您应该做的是将完成块传递给此方法,并在获得令牌后调用它。这样,您就不会在执行请求时阻塞主线程。

你可以这样做:

- (void)extractTokenFromURL:(NSURL *)tokenURL completion:(void (^)(NSString *token, NSError* error))completion {
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    [[session dataTaskWithURL:self.tokenURL
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (!error) {
            NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
            if (httpResp.statusCode == 200) {
                NSString *token = // ...;
                if (token) { // we have a valid token
                    if (completion) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            completion(token, nil);
                        });
                    } 
                } else if (completion) {
                    // create an error indicating why the token is not valid
                    NSError *error = // ...;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completion(nil, error);
                    });
                }
            }
        } else if (completion) {
            // send the http error. you could also wrap it on your own error domain and code
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, error);
            });
        }
    }] resume];
}

您还可以有两个不同的块:一个代表成功,另一个代表失败。

于 2013-11-07T12:08:45.093 回答