IPV6 Swift可达性

我是新来的迅速和xcode&已经让我的应用程序由于IPV6拒绝:

性能 – 2.1

我们在以下情况下,您的应用在运行iOS 9.3.5的iPad和iPhone上连接到IPv6networking时崩溃:

具体来说,当我们触摸应用程序右下angular的更多button时,它会崩溃。

我花了很多时间在ipv6上阅读,并得出结论说,它可能是我正在使用的可达性类。

我有几个问题,我将如何修改代码以包含ipv6:

public class Reachability { 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(sizeofValue(zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) let defaultRouteReachability = withUnsafePointer(&zeroAddress) { SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0)) } var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0) if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false { return false } let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0 let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0 return isReachable && !needsConnection } } 

而且因为我使用http请求,我的服务器也必须configuration为ipv6吗?

任何帮助将非常感激!

崩溃日志:

 Incident Identifier: C86A011D-2913-4FFE-992A-0E359781CECD CrashReporter Key: ae6778b695dfd22e7ce7236f09740cb14847b427 Hardware Model: xxx Process: Fibre [3135] Path: /private/var/containers/Bundle/Application/8F9AF4E2-D64C-4C0C-8BF0-2C8FE63580EA/*** Identifier: *** Version: 1 (4.1.5) Code Type: ARM-64 (Native) Parent Process: launchd [1] Date/Time: 2016-09-08 17:51:55.55 -0700 Launch Time: 2016-09-08 17:51:52.52 -0700 OS Version: iOS 9.3.5 (13G36) Report Version: 105 Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x0000000100032b54 Triggered by Thread: 5 Filtered syslog: None found Global Trace Buffer (reverse chronological seconds): 6148914686.196010 CFNetwork 0x0000000181b0a0a4 NSURLConnection finished with error - code -1003 6148914686.196214 CFNetwork 0x0000000181ad5d50 HTTP load failed (error code: -1003 [12:8]) 6148914686.196214 CFNetwork 0x0000000181a596a8 _CFNetworkIsConnectedToInternet returning 1, flagsValid: 1, flags: 0x2 6148914686.196815 CFNetwork 0x0000000181abc184 TCP Conn 0x136fe5290 Failed : error 0:-65554 [-65554] 6148914686.196815 CFNetwork 0x0000000181abbe7c TCP Conn 0x136fe5290 complete. fd: -1, err: -65554 6148914686.196982 CFNetwork 0x0000000181abd3a8 TCP Conn 0x136fe5290 event 3. err: -65554 6148914686.198890 CFNetwork 0x0000000181abd480 TCP Conn 0x136fe5290 started 6148914686.200546 CFNetwork 0x0000000181b17e5c Creating default cookie storage with default identifier 6148914686.200546 CFNetwork 0x0000000181b17e28 Faulting in CFHTTPCookieStorage singleton 6148914686.200546 CFNetwork 0x0000000181b69e50 Faulting in NSHTTPCookieStorage singleton 6148914688.516532 AppleJPEG 0x0000000182e9626c [0x13808ee00] Created session Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0: 0 libsystem_kernel.dylib 0x0000000180fb4fd8 mach_msg_trap + 8 1 libsystem_kernel.dylib 0x0000000180fb4e54 mach_msg + 72 2 CoreFoundation 0x00000001813ecc60 __CFRunLoopServiceMachPort + 196 3 CoreFoundation 0x00000001813ea964 __CFRunLoopRun + 1032 4 CoreFoundation 0x0000000181314c50 CFRunLoopRunSpecific + 384 5 GraphicsServices 0x0000000182bfc088 GSEventRunModal + 180 6 UIKit 0x00000001865fa088 UIApplicationMain + 204 7 Fibre 0x0000000100020b94 0x100004000 + 117652 8 libdyld.dylib 0x0000000180eb28b8 start + 4 Thread 1 name: Dispatch queue: com.apple.NSURLSession-work Thread 1: 0 libsystem_kernel.dylib 0x0000000180fd14d8 kevent_qos + 8 1 libdispatch.dylib 0x0000000180e944a8 _dispatch_kq_update + 152 2 libdispatch.dylib 0x0000000180e94404 _dispatch_mgr_wakeup + 48 3 libdispatch.dylib 0x0000000180e91a40 _dispatch_queue_wakeup_with_qos_slow + 40 4 libdispatch.dylib 0x0000000180e8d694 _dispatch_queue_drain + 1332 5 libdispatch.dylib 0x0000000180e84f80 _dispatch_queue_invoke + 464 6 libdispatch.dylib 0x0000000180e8f390 _dispatch_root_queue_drain + 728 7 libdispatch.dylib 0x0000000180e8f0b0 _dispatch_worker_thread3 + 112 8 libsystem_pthread.dylib 0x0000000181099470 _pthread_wqthread + 1092 9 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 2 name: Dispatch queue: com.apple.libdispatch-manager Thread 2: 0 libdispatch.dylib 0x0000000180e96b84 _dispatch_source_kevent_unregister + 316 1 libdispatch.dylib 0x0000000180e8390c _dispatch_source_invoke + 708 2 libdispatch.dylib 0x0000000180e8390c _dispatch_source_invoke + 708 3 libdispatch.dylib 0x0000000180e8d694 _dispatch_queue_drain + 1332 4 libdispatch.dylib 0x0000000180e8dcd8 _dispatch_mgr_queue_drain + 44 5 libdispatch.dylib 0x0000000180e94734 _dispatch_mgr_invoke + 68 6 libdispatch.dylib 0x0000000180e83648 _dispatch_source_invoke + 0 Thread 3: 0 libsystem_kernel.dylib 0x0000000180fd0b48 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x0000000181099530 _pthread_wqthread + 1284 2 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 4: 0 libsystem_kernel.dylib 0x0000000180fd0b48 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x0000000181099530 _pthread_wqthread + 1284 2 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 5 name: Dispatch queue: NSOperationQueue 0x136f35cc0 :: NSOperation 0x136fe3590 (QOS: LEGACY) Thread 5 Crashed: 0 Fibre 0x0000000100032b54 0x100004000 + 191316 1 CFNetwork 0x0000000181a965ac __67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 180 2 Foundation 0x0000000181df0540 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16 3 Foundation 0x0000000181d42870 -[NSBlockOperation main] + 96 4 Foundation 0x0000000181d32e48 -[__NSOperationInternal _start:] + 604 5 Foundation 0x0000000181df2934 __NSOQSchedule_f + 224 6 libdispatch.dylib 0x0000000180e8147c _dispatch_client_callout + 16 7 libdispatch.dylib 0x0000000180e8d4c0 _dispatch_queue_drain + 864 8 libdispatch.dylib 0x0000000180e84f80 _dispatch_queue_invoke + 464 9 libdispatch.dylib 0x0000000180e8f390 _dispatch_root_queue_drain + 728 10 libdispatch.dylib 0x0000000180e8f0b0 _dispatch_worker_thread3 + 112 11 libsystem_pthread.dylib 0x0000000181099470 _pthread_wqthread + 1092 12 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 6: 0 libsystem_kernel.dylib 0x0000000180fd0b48 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x0000000181099530 _pthread_wqthread + 1284 2 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 7: 0 libsystem_kernel.dylib 0x0000000180fd0b48 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x0000000181099530 _pthread_wqthread + 1284 2 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 8: 0 libsystem_kernel.dylib 0x0000000180fd0b48 __workq_kernreturn + 8 1 libsystem_pthread.dylib 0x0000000181099530 _pthread_wqthread + 1284 2 libsystem_pthread.dylib 0x0000000181099020 start_wqthread + 4 Thread 9 name: com.apple.NSURLConnectionLoader Thread 9: 0 libsystem_kernel.dylib 0x0000000180fb4fd8 mach_msg_trap + 8 1 libsystem_kernel.dylib 0x0000000180fb4e54 mach_msg + 72 2 CoreFoundation 0x00000001813ecc60 __CFRunLoopServiceMachPort + 196 3 CoreFoundation 0x00000001813ea964 __CFRunLoopRun + 1032 4 CoreFoundation 0x0000000181314c50 CFRunLoopRunSpecific + 384 5 CFNetwork 0x0000000181a95bcc +[NSURLConnection(Loader) _resourceLoadLoop:] + 412 6 Foundation 0x0000000181e0be4c __NSThread__start__ + 1000 7 libsystem_pthread.dylib 0x000000018109bb28 _pthread_body + 156 8 libsystem_pthread.dylib 0x000000018109ba8c _pthread_body + 0 9 libsystem_pthread.dylib 0x0000000181099028 thread_start + 4 Thread 5 crashed with ARM Thread State (64-bit): x0: 0x0000000000000000 x1: 0x0000000000000000 x2: 0x0000000136db3890 x3: 0x0000000136f6dda0 x4: 0x0000000100039250 x5: 0x0000000000000001 x6: 0x0000000000000000 x7: 0x0000000000000a30 x8: 0x0000000136fd58f8 x9: 0x0000000000000004 x10: 0x0000000000000000 x11: 0x0000000000000000 x12: 0x0000000137091190 x13: 0x000005a1001e9247 x14: 0x00000000ffffffff x15: 0x00000000ffffffff x16: 0x00000001003ec47c x17: 0x0000000186e3db68 x18: 0x0000000000000000 x19: 0x0000000000000000 x20: 0x0000000136db3890 x21: 0x0000000000000000 x22: 0x0000000136fe5b50 x23: 0x0000000136f6dda0 x24: 0x8e00a063ef134017 x25: 0x00000000000000d8 x26: 0x0000000000020001 x27: 0x00000000240008ff x28: 0x0000000000010001 fp: 0x000000016e246a00 lr: 0x0000000181a965ac sp: 0x000000016e246980 pc: 0x0000000100032b54 cpsr: 0x20000000 Binary Images: 0x100004000 - 0x100193fff Fibre arm64 <a3baaae1e0f93363a87211e3e15a394e> /var/containers/Bundle/Application/8F9AF4E2-D64C-4C0C-8BF0-2C8FE63580EA/Fibre.app/Fibre 0x10021c000 - 0x10041bfff libswiftCore.dylib arm64 <39ceee8206bb32809dab8ecba2e05d8e> /var/containers/Bundle/Application/8F9AF4E2-D64C-4C0C-8BF0-2C8FE63580EA/Fibre.app/Frameworks/libswiftCore.dylib 

正如在拒绝通知中所看到的那样,您的问题源自于仅在IPv6和其他现代networking拓扑上不再有效的假设。 有两个步骤前进

  1. 在连接之前不要使用可达性进行“预检”。 从苹果指导到支持现代networking :

可达性API(请参阅SCNetworkReachability参考 )用于识别连接问题之后的诊断目的。 许多应用程序错误地使用这些API来主动检查Internet连接,方法是调用SCNetworkReachabilityCreateWithAddress方法并将0.0.0.0的IPv4地址传递给networking,这表明networking上有路由器。 但是,路由器的存在并不能保证存在Internet连接。 通常,避免预检networking可达性。 只要尝试build立连接并妥善处理失败。 如果您必须检查networking可用性,请避免调用SCNetworkReachabilityCreateWithAddress方法。 调用SCNetworkReachabilityCreateWithName方法并将其传递给主机名。

某些应用程序还会将SCNetworkReachabilityCreateWithAddress方法传递给IPv4地址169.254.0.0 (一个自分配的链接本地地址),以检查活动的Wi-Fi连接。 要检查Wi-Fi或蜂窝连接,请查找networking可达性标记kSCNetworkReachabilityFlagsIsWWAN

  1. 在提交到App Store之前,先testing一下纯IPv6networking 。 您可以在您的开发Mac上设置一个NAT64网关,以创build供testing设备使用的纯IPv6networking。

在上述链接中支持IPv6和现代networking,以及在WWDC16会议714“现代Internet的networking”中,有更多的好build议。

Xcode 8.3•Swift 3.1

为了解决在评论中提到的4G问题,我使用了@AshleyMills可达性实现作为参考,并重写了Swift 3.1的Reachability:

来源链接

检查与斯威夫特互联网连接