13

My (Unity) app complains that the my iPod does not have network connectivity while the device has full wi-fi connectivity (as evidenced by every other app, including Safari).

This isn't just one API point - all of them (Facebook, StoreKit, etc) claim that the network is not available.

This message is periodically sent to the console from my app:

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

which may (I think) be coming from CoreTelephony.

Any idea what is causing this? I get it in debug and adhoc builds. I do not have any Restrictions (from Settings) active on the device.

The device is iOS 6 and the base sdk is iOS 5.

Swati
  • 2,870
  • 7
  • 45
  • 87
Dave
  • 7,589
  • 12
  • 36
  • 42

3 Answers3

10

The "Could not successfully update network info during initialization." log is shown every time you initialize the CTTelephonyNetworkInfo in a device without SIM card, (iPod touch or iPad without 3G).

If it's very annoying, you can just initialize it once and do your network checks against that instance.

rockdaswift
  • 9,613
  • 5
  • 40
  • 46
  • 1
    Oh! you rocks, daswift. That's exactly why I was getting this anoying message on simulator. Thanks! – Martin Oct 03 '17 at 14:23
  • I tried set location in simulator and CTTelephonyNetworkInfo initialization in appDelegate.m but not working, could you post the code? @rockdaswift – Yao Li Nov 30 '17 at 00:33
  • @YaoLi you just need to do this once (if needed): let networkInfo = CTTelephonyNetworkInfo() Then pass the object around, every time you do that in the simulator or in a device without SIM card you will get that warning. – rockdaswift Nov 30 '17 at 09:34
  • @rockdaswift I don't use CTTelephonyNetworkInfo directly, I only use AFNetworking 3.1.0 to make API request and AFNetworking version 2.0+ does work. – Yao Li Dec 01 '17 at 06:09
  • It's weird, AFNetworking doesn't use CTTelephonyNetworkInfo, maybe it's in another dependency. – rockdaswift Dec 01 '17 at 11:24
1

Check the developer tools to check if you have accidentally turned on 100% loss internet. It should be in the debug menu.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
0

I tried set location in simulator and CTTelephonyNetworkInfo initialization in appDelegate.m but not working. I fixed it by define a new class 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"];
}
Yao Li
  • 2,071
  • 1
  • 25
  • 24