1

I want to get the Local Host Ip address on iPhone in swift ?

I have reference the How to get Ip address in swift , It needs to #include <ifaddrs.h> and the link mention the getifaddrs() is defined in <ifaddrs.h>, which is not included by default. Therefore you have to create a bridging header and add #include <ifaddrs.h>

What is the create the bridging header mean here ? How to create the bridging header?

Is it mean , copy the ifaddrs.h file to my swift project ?

or does there has other way to get the Local Host Ip address on iPhone in swift?

Thanks in advance.

Community
  • 1
  • 1
Martin
  • 2,813
  • 11
  • 43
  • 66
  • i don't have a code for swift but do have a code for objective - c, i can suggest if you couldn't find a code for swift then make a class with Objective - c class and write a method that would return NSString of your local Host ip. – Syed Ali Salman Jan 22 '15 at 09:13
  • if you like my suggestion then i would give you code in answer. – Syed Ali Salman Jan 22 '15 at 09:13
  • @SyedAliSalman OK , I approve your suggestion ! – Martin Jan 22 '15 at 09:17
  • allright, let me wirte code. – Syed Ali Salman Jan 22 '15 at 09:17
  • So your question is not "how to get the local IP address", but "how to create a bridging header file". So your title is misleading. – Martin R Jan 22 '15 at 09:20
  • @MartinR Sorry , that is also my question. I have modify the content. – Martin Jan 22 '15 at 09:22
  • "How to get the local IP address in Swift" is (hopefully) answered in http://stackoverflow.com/questions/25626117/how-to-get-ip-address-in-swift that you referenced, and that code should still work. So your *only problem* is how to create a bridging header file where you can add the required `#include `. And that is answered in http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift/24005242#24005242. The easiest way is to add *any* Objective-C file to your project. Then Xcode will automatically ask if you want a bridging header. – Martin R Jan 22 '15 at 09:27

1 Answers1

0

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift.

Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

Alternatively, you can create a bridging header yourself by choosing File > New > File > (iOS or OS X) > Source > Header File.

You’ll need to edit the bridging header file to expose your Objective-C code to your Swift code

To import Objective-C code into Swift from the same target

  1. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift.

  2. Under Build Settings, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header.

You can find more info on bridging-header here

deimus
  • 9,565
  • 12
  • 63
  • 107