title: Error when trying to get IP address in swift on iPhone
body: <p>Im trying to get an iPhone IP address and i'm using this sample:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/25626117/how-to-get-ip-address-in-swift">How to get Ip address in swift</a></li>
  </ul>
  
  <p>I'm getting this error:fatal error: unexpectedly found nil while unwrapping an Optional value
  and the code stops hear:</p>
  
  <pre>
          0x285160 :  bl     0x2ba61c                  ; function signature specialization  of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
      ->  0x285164 :  trap 
  </pre>
  
  <p>i have a xxx-Bridging-Header.h  in my project
  but i still get this error</p>
  
  <pre><code>   #import &lt;Foundation/Foundation.h&gt;
     #include &lt;arpa/inet.h&gt; 
     #include &lt;sys/socket.h&gt;
     #include &lt;ifaddrs.h&gt;
     @interface NSObject ()
     @end
  </code></pre>
  
  <p>the swift class</p>
  
  <pre><code>import Foundation
  public class NetworkUtils:NSObject{
  
  public func getIFAddresses() -&gt; [String] {
      var addresses = [String]()
  
      // Get list of all interfaces on the local machine:
      var ifaddr : UnsafeMutablePointer&lt;ifaddrs&gt; = nil
      if getifaddrs(&amp;ifaddr) == 0 {
  
          // For each interface ...
          for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
              let flags = Int32(ptr.memory.ifa_flags)
              var addr = ptr.memory.ifa_addr.memory
  
              // Check for running IPv4, IPv6 interfaces. Skip the     loopback interface.
              if (flags &amp; (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                  if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
  
                      // Convert interface address to a human readable string:
                      var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                      if (getnameinfo(&amp;addr, socklen_t(addr.sa_len), &amp;hostname, socklen_t(hostname.count),
                          nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                              if let address = String.fromCString(hostname) {
                                  addresses.append(address)
                              }
                      }
                  }
              }
          }
          freeifaddrs(ifaddr)
      }
  
      return addresses
   }
  }
  </code></pre>
  
  <p>Calling func</p>
  
  <pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
     // var osMajorVer:Int
      let osMajorVer:Int = getMajorSystemVersion()
      switch osMajorVer
      {
      case 8:
          var types: UIUserNotificationType = UIUserNotificationType.Badge |
              UIUserNotificationType.Alert |
              UIUserNotificationType.Sound
  
          var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
  
          application.registerUserNotificationSettings( settings )
          application.registerForRemoteNotifications()
  
          break
      case 7:
          application.registerForRemoteNotificationTypes( UIRemoteNotificationType.Badge |
              UIRemoteNotificationType.Sound |
              UIRemoteNotificationType.Alert )
          break
      default:
          break
      }
      var IP:NetworkUtils!
      var ip = IP.getIFAddresses()
      var bounds: CGRect = UIScreen.mainScreen().bounds
      var witdh:Int  = Int(bounds.size.width)
      var height:Int  = Int(bounds.size.height)
  
      let os = "iPhone"
      var ver = String(osMajorVer)
  
      let userDefaults = NSUserDefaults.standardUserDefaults()
  
      userDefaults.setValue("iPhone", forKey: "os")
      userDefaults.setValue("iPhone", forKey:"osType")
      userDefaults.setValue(String(osMajorVer), forKey:"ver")
      userDefaults.setValue(height, forKey:"height")
      userDefaults.setValue(witdh, forKey:"witdh")
      userDefaults.setValue("1.1.1.1", forKey:"IP")
      userDefaults.synchronize() // don't forget this!!!!
       return true
  }
  </code></pre>