13

我的 (Unity) 应用程序抱怨我的 iPod 没有网络连接,而该设备具有完整的 Wi-Fi 连接(正如其他所有应用程序所证明的那样,包括 Safari)。

这不仅仅是一个 API 点——它们(Facebook、StoreKit 等)都声称网络不可用。

此消息会定期从我的应用程序发送到控制台:

<Error>: Could not successfully update network info during initialization.

这可能(我认为)来自CoreTelephony

知道是什么原因造成的吗?我在调试和临时构建中得到它。我在设备上没有任何限制(来自设置)。

该设备是 iOS 6,基本 sdk 是 iOS 5。

4

3 回答 3

10

“初始化期间无法成功更新网络信息。” 每次在没有 SIM 卡的设备(没有 3G 的 iPod touch 或 iPad)中初始化 CTTelephonyNetworkInfo 时都会显示日志。

如果这很烦人,您可以只初始化一次,然后针对该实例进行网络检查。

于 2017-06-30T10:49:07.123 回答
1

检查开发人员工具以检查您是否不小心打开了 100% 丢失互联网。它应该在调试菜单中。

于 2014-04-11T11:13:44.300 回答
0

我尝试在模拟器中设置位置并在 appDelegate.m 中初始化 CTTelephonyNetworkInfo,但没有工作。我通过定义一个新类 APIAgent.m 来修复它:

+ (APIAgent *)manager {
    static APIAgent *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[APIAgent alloc] initWithBaseURL:[NSURL URLWithString:API_URL]];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        NSMutableIndexSet* codes = [NSMutableIndexSet indexSetWithIndexesInRange: NSMakeRange(200, 100)];
        [codes addIndex: 400];
        [codes addIndex: 401];
        [codes addIndex: 404];
        [codes addIndex: 409];
        [codes addIndex: 500];
        _manager.responseSerializer.acceptableStatusCodes = codes;
    });

    return _manager;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

    [self setHeaders];

    return self;
}

- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress *uploadProgress))uploadProgress
                     complete:(void (^)(NSURLSessionDataTask *task, id responseObject, NSError *error))complete {
    return [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (complete) {
            complete(task, responseObject, nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (complete) {
            complete(task, nil, error);
        }
    }];
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters complete:(void (^)(NSURLSessionDataTask *task, id responseObject, NSError *error))complete {
    return [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (complete) {
            complete(task, responseObject, nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (complete) {
            complete(task, nil, error);
        }
    }];
}


- (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress *uploadProgress))uploadProgress
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    self.securityPolicy.allowInvalidCertificates = YES; // self signed certificate
    self.securityPolicy.validatesDomainName = NO;
    return [super GET:URLString parameters:parameters progress:nil success:success failure:failure];
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                      progress:(void (^)(NSProgress *uploadProgress))uploadProgress
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
    NSMutableDictionary *params = [parameters mutableCopy];

    self.securityPolicy.allowInvalidCertificates = YES; // self signed certificate
    self.securityPolicy.validatesDomainName = NO;
    return [super POST:URLString parameters:params progress:nil success:success failure:failure];
}

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block
                      progress:(void (^)(NSProgress *uploadProgress))uploadProgress
                       success:(void (^)(NSURLSessionDataTask *, id))success failure:(void (^)(NSURLSessionDataTask *, NSError *))failure {
    NSMutableDictionary *params = [parameters mutableCopy];

    if (parameters == nil) {
        params = [[NSMutableDictionary alloc] init];
    } else {
        if (parameters[@"postId"] != nil) {
            params[@"postId"] = parameters[@"postId"];
        }
    }

    self.securityPolicy.allowInvalidCertificates = YES; // http://stackoverflow.com/questions/27808249/problems-with-ssl-pinning-and-afnetworking-2-5-0-nsurlerrordomain-error-1012
    self.securityPolicy.validatesDomainName = NO;
    return [super POST:URLString parameters:params constructingBodyWithBlock:block progress:nil success:success failure:failure];

}

- (void)setHeaders {
    NSString *time = [NSString stringWithFormat:@"%f", [[[NSDate alloc] init] timeIntervalSince1970]];
    AFHTTPRequestSerializer * serializer = self.requestSerializer;
    [serializer setValue:time forHTTPHeaderField:@"X-API-TIME"];
    [serializer setValue:[UIDevice currentDevice].identifierForVendor.UUIDString forHTTPHeaderField:@"X-UUID"];

    [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    NSString *preferredLang = [NSLocale preferredLanguages].firstObject;

    [serializer setValue:preferredLang forHTTPHeaderField:@"X-USER-LANGUAGE"];
}
于 2017-12-04T19:21:23.197 回答