iOS 因为用了YYReachability,App被拒了

  事先声明一下,不是黑YYKit。YYKit是一个非常优秀的第三方库,项目里很多地方都用到了。我是因为调用了下面的方法,被拒的

+ (BOOL)netWorkIsPing{
    return [YYReachability reachability].isReachable;
}

  下面是被拒的邮件。

iOS 因为用了YYReachability,App被拒了_第1张图片
ipv6.png

   一看是因为ipv6。关于ipv6,可以看这篇文章 iOS应用支持IPV6,就那点事儿。服务器不用适配,只需要前端适配。所以出了这事,我第一感觉AFNetworking不支持IPV6,后来发现不可能,因为我在其他项目里一直用来着,从来没出过问题。所以我就看了一下代码,发现有可能是YYReachability的锅。

  我在每次网络请求之前,都要判断一下网络环境,如果无网,就直接返回了,问题就出现在这里。

- (FFURLSessionDataTask *) fetchPostRequestWithPath: (NSString *)path Parameter: (id) parameters UseCache: (BOOL)useCache completionBlock: (NetworkCompletionHandler)completionBlock
{
    // 判断当前网络环境
   //  把这段代码删除之后,重新提交审核通过了
    if (![FFTool netWorkIsPing]) {
        if (completionBlock) {
            completionBlock(NetworkReturnCodeNoPing, nil);
            return nil;
        }
    }
    
    if (path.length == 0) {
        return nil;
    }
    
    if (useCache) {
       
        return nil;
        
    }else {
        NSString *strURL = [self getURLWithPath:path];
        NSURLSessionDataTask *dataTask  = [self.httpSessionManager POST:strURL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (completionBlock) {
                responseObject = [FFTool ResultDic:responseObject];
                // token expire
                if ([responseObject[@"errno"] intValue] == 600101 || [responseObject[@"errorCode"] intValue] == 600101) {
                    [FFProgressHUD showErrorWithStatus:FFResponseInfo];
                    [FFUserModel removeUserInfo];
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationNameLoginStatusChange object:@{strLoginStatusKey :@0}];
                    return ;
                }
                
                completionBlock(NetworkReturnCodeSuccess, responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
           FFLog(@"%@",error);
            if (completionBlock) {
                completionBlock(NetworkReturnCodeFailure,nil);
            }
        }];
        
        return (FFURLSessionDataTask *)dataTask;
    }
}

  我去查看了一下YYReachability的源码。

+ (instancetype)reachability {
    return self.new;
}

- (instancetype)init {
    /*
     See Apple's Reachability implementation and read me:
     The address 0.0.0.0, which reachability treats as a special token that 
     causes it to actually monitor the general routing status of the device, 
     both IPv4 and IPv6.
     https://developer.apple.com/library/ios/samplecode/Reachability/Listings/ReadMe_md.html#//apple_ref/doc/uid/DTS40007324-ReadMe_md-DontLinkElementID_11
     */
    struct sockaddr_in zero_addr;
    bzero(&zero_addr, sizeof(zero_addr));
    zero_addr.sin_len = sizeof(zero_addr);
    zero_addr.sin_family = AF_INET;
    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&zero_addr);
    return [self initWithRef:ref];
}

  我去看了苹果的Reachability的源码,确实有这么一段话,可以用0.0.0.0来检测设备的网络状况。但是在readme里面又有一段话

- reachabilityWithAddress and SCNetworkReachabilityCreateWithAddress:  To monitor an IPv6 address, simply pass in an IPv6 `sockaddr_in6 struct` instead of the IPv4 `sockaddr_in struct`.

说为了监测ipv6,要用sockaddr_in6 struct 替代sockaddr_in struct。所以上面的代码应该变为

- (instancetype)init {
    struct sockaddr_in6 zero_addr;
    bzero(&zero_addr, sizeof(zero_addr));
    zero_addr.sin6_len = sizeof(zero_addr);
    zero_addr.sin6_family = AF_INET6;
    SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&zero_addr);
    return [self initWithRef:ref];
}

  这个比较坑的就是苹果只是在ReadMe.md里面提了一句,并没有在代码里用sockaddr_in6 struct 替代sockaddr_in struct,导致很多人以为sockaddr_in struct就支持ipv6了。写这篇文章的目的是让兄弟姐妹们别因为同样的原因被拒,因为被拒一次还是很耽误事的。

你可能感兴趣的:(iOS 因为用了YYReachability,App被拒了)