为什么这个iOS应用程序无法访问网络?

时间:2013-06-07 02:54:43

标签: ios networking unity3d

我的(Unity)应用程序抱怨我的iPod没有网络连接,而设备具有完整的Wi-Fi连接(所有其他应用程序,包括Safari)都证明了这一点。

这不仅仅是一个API点 - 所有这些(Facebook,StoreKit等)声称网络不可用。

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

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

可能(我认为)来自 CoreTelephony

知道造成这种情况的原因是什么?我在调试和adhoc构建中得到它。我没有在设备上激活任何限制(来自设置)。

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

3 个答案:

答案 0 :(得分:9)

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

如果它非常烦人,您只需初始化一次并对该实例进行网络检查。

答案 1 :(得分:1)

检查开发者工具,检查您是否意外开启了100%互联网丢失。它应该在调试菜单中。

答案 2 :(得分: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"];
}