3

Background

I'm new to iOS development and playing with Swift for the sake of learning. As a small challenge, I'm trying to get the SSID of network to which a device is currently connected.

This is well-covered ground on Stack using Objective-C: iPhone get SSID without private library, for example ... but the Objective-C / Swift dance is causing me some conceptual challenges.

Specifically, the suggested solution (discussed in the above-linked post) is to call the function CNCopyCurrentNetworkInfo() -- but, according to Apple's documentation, this function is not available in Swift.

So far

I've included (I think correctly) SystemConfiguration.framework by adding it to the Linked Frameworks and Libraries of the project. I've also brought it into the View Controller using import SystemConfiguration.

Questions

  1. Do I need to also use import SystemConfiguration, or is that already done due to including the framework with my project?
  2. Is there another / better way to include the SystemConfiguration library?

  3. The big one: How, once the required library is imported, do you call the Objective-C function from the library?

Thank you!

Community
  • 1
  • 1
TriggerDan
  • 91
  • 5

2 Answers2

6

I know this is not directly answering your question, but it's an answer to your problem. You can do everything in swift in your case.

After importing the library,

import SystemConfiguration.CaptiveNetwork

You can immediately call:

CNCopySupportedInterfaces()

and it will work. Confirmed in Xcode 6.3.2.

mash
  • 4,204
  • 4
  • 32
  • 34
  • The only catch is that autocomplete still does not show anything when I type "CN" and hit Ctrl+Space. Don't be deceived by that - full word "CNCopySupportedInterfaces" will work! – Lukasz Czerwinski Sep 07 '15 at 00:38
  • Do you know who owns the object? According to documentation which covers only Objective C it's users responsibility to call CFRelease, however this function is not available in Swift. – Lukasz Czerwinski Sep 07 '15 at 00:45
2

Apple's interoperability book (from Understanding the Swift Import Process):

Any Objective-C framework (or C library) that’s accessible as a module can be imported directly into Swift. This includes all of the Objective-C system frameworks—such as Foundation, UIKit, and SpriteKit—as well as common C libraries supplied with the system.

Regarding your specific questions:

  1. The opposite is the case: you do not need to manually include Apple's frameworks. Xcode will automatically make them available to you given a valid import statement (such as import SystemConfiguration in your case), even – or rather: especially – in a playground!

  2. ditto...

  3. Given the above import statement, SystemConfiguration is indeed imported since you can call its functions (e.g. SCCopyLastError() // --> __NSCFError) and access its constants (e.g. kCFErrorDomainSystemConfiguration // --> com.apple.SystemConfiguration). Unfortunately, CaptiveNetwork does not seem to be imported with it (e.g. CNCopySupportedInterfaces() // --> Use of unresolved identifier).

You should be able, however, to use this framework on the Objective C side and simply call your own wrapper functions from Swift. You just need to remember to include them among the imports listed in your bridging header (see Swift and Objective-C in the Same Project for more about bridging headers).

Milos
  • 2,728
  • 22
  • 24