2017-07-17 100 views
0

我的應用程序因爲應用程序在IPV6網絡中崩潰而被我的應用程序商店拒絕。這是我第二次發生。我已經更新了我的代碼以支持IPV6網絡,並且在該代碼中成功移動了兩個構建版本。但是現在他們又提出了同樣的問題,他們說這個問題只發生在Wi-Fi網絡上。這裏是我的嘗試代碼:iOS Swift 3應用程序被App Store拒絕因爲AppV6網絡中的應用程序崩潰

internal class Reachability 
    { 
     class func isConnectedToNetwork() -> Bool 
     { 
      var zeroAddress = sockaddr_in6() 
      zeroAddress.sin6_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) 
      zeroAddress.sin6_family = sa_family_t(AF_INET6) 

      let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) { 
       $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in 
        SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.google.com") 
       } 
      } 

      var flags = SCNetworkReachabilityFlags() 
      if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) { 
       return false 
      } 
      let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 
      let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 
      return (isReachable && !needsConnection) 
     } 
    } 

幫助我找出解決這個問題的正確方案。

+0

後,實際上導致崩潰的代碼。指出導致崩潰的確切行幷包含完整的錯誤消息。 – rmaddy

回答

0
internal class ReachabilityCheck 
{ 
    class func isConnectedToNetwork() -> Bool 
    { 
     var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) 
     zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) 
     zeroAddress.sin_family = sa_family_t(AF_INET) 

     let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) 
     { 
      $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in 
       SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) 
      } 
     } 

     var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) 
     if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false 
     { 
      return false 
     } 
     // Working for Cellular and WIFI 
     let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 
     let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 
     let ret = (isReachable && !needsConnection) 

     return ret 
    } 
} 
0

你可以嘗試這個請它對我工作良好與3G,4G和WiFi網絡也工作。舊的不支持移動網絡連接。

class func isConnectedToNetwork() -> Bool 
    { 
     var zeroAddress = sockaddr_in() 
      zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress)) 
     // zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) 
     zeroAddress.sin_family = sa_family_t(AF_INET) 

     let defaultRouteReachability = withUnsafePointer(&zeroAddress) { 
      SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0)) 
     } 

     var flags = SCNetworkReachabilityFlags() 
     if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) { 
      return false 
     } 
     let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 
     let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 
     return (isReachable && !needsConnection) 
    } 
+0

以上代碼是否支持IPV6網絡? –

相關問題