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.
我是iOS开发的新手,为了学习而玩得很快。作为一个小挑战,我正在尝试获取设备当前连接的网络SSID。
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.
这是使用Objective-C在堆栈上覆盖良好的基础:例如,没有私有库的iPhone get SSID…但是Objective-C / Swift舞给我带来了一些概念上的挑战。
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.
具体来说,建议的解决方案(在上面的链接文章中讨论过)是调用函数CNCopyCurrentNetworkInfo()——但是根据苹果的文档,这个函数在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
.
我将SystemConfiguration.framework添加到项目的链接框架和库中(我认为是正确的)。我还使用import SystemConfiguration将它带入视图控制器。
Questions
问题
- Do I need to also use
import SystemConfiguration
, or is that already done due to including the framework with my project? - 我是否还需要使用import SystemConfiguration,还是已经完成了,因为我的项目包含了框架?
-
Is there another / better way to include the
SystemConfiguration
library?是否有其他更好的方法来包含SystemConfiguration库?
-
The big one: How, once the required library is imported, do you call the Objective-C function from the library?
最大的问题是:一旦导入所需的库,如何从库中调用Objective-C函数?
Thank you!
谢谢你!
2 个解决方案
#1
2
Apple's interoperability book (from Understanding the Swift Import Process):
苹果的互操作性书籍(来自理解Swift导入过程):
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.
任何可以作为模块访问的Objective-C框架(或C库)都可以直接导入Swift。这包括所有Objective-C系统框架——比如Foundation、UIKit和spritekit——以及系统提供的公共C库。
Regarding your specific questions:
关于你的具体问题:
-
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 asimport SystemConfiguration
in your case), even – or rather: especially – in a playground!相反的情况是:您不需要手动地包含苹果的框架。Xcode将自动向您提供有效的导入语句(例如,在您的情况下导入系统配置),甚至(或者更确切地说:尤其是)在游乐场中!
-
ditto...
同上……
-
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
).根据上面的导入语句,系统配置确实是导入的,因为您可以调用它的函数(例如SCCopyLastError() / -> __NSCFError)并访问它的常量(例如kCFErrorDomainSystemConfiguration / -> com.apple.SystemConfiguration)。不幸的是,CaptiveNetwork似乎没有与它一起导入(例如,cncopysupportedinterface() /——>使用了未定义的标识符)。
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).
但是,您应该能够在Objective C中使用这个框架,并从Swift调用您自己的包装器函数。您只需要记住在桥接头中列出的导入中包含它们(有关桥接头的更多信息,请参阅同一项目中的Swift和Objective-C)。
#2
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
进口SystemConfiguration.CaptiveNetwork
You can immediately call:
你可以马上叫:
CNCopySupportedInterfaces()
CNCopySupportedInterfaces()
and it will work. Confirmed in Xcode 6.3.2.
它会工作。在Xcode再确认。
#1
2
Apple's interoperability book (from Understanding the Swift Import Process):
苹果的互操作性书籍(来自理解Swift导入过程):
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.
任何可以作为模块访问的Objective-C框架(或C库)都可以直接导入Swift。这包括所有Objective-C系统框架——比如Foundation、UIKit和spritekit——以及系统提供的公共C库。
Regarding your specific questions:
关于你的具体问题:
-
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 asimport SystemConfiguration
in your case), even – or rather: especially – in a playground!相反的情况是:您不需要手动地包含苹果的框架。Xcode将自动向您提供有效的导入语句(例如,在您的情况下导入系统配置),甚至(或者更确切地说:尤其是)在游乐场中!
-
ditto...
同上……
-
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
).根据上面的导入语句,系统配置确实是导入的,因为您可以调用它的函数(例如SCCopyLastError() / -> __NSCFError)并访问它的常量(例如kCFErrorDomainSystemConfiguration / -> com.apple.SystemConfiguration)。不幸的是,CaptiveNetwork似乎没有与它一起导入(例如,cncopysupportedinterface() /——>使用了未定义的标识符)。
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).
但是,您应该能够在Objective C中使用这个框架,并从Swift调用您自己的包装器函数。您只需要记住在桥接头中列出的导入中包含它们(有关桥接头的更多信息,请参阅同一项目中的Swift和Objective-C)。
#2
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
进口SystemConfiguration.CaptiveNetwork
You can immediately call:
你可以马上叫:
CNCopySupportedInterfaces()
CNCopySupportedInterfaces()
and it will work. Confirmed in Xcode 6.3.2.
它会工作。在Xcode再确认。