iOS 11 CoreNFC 如何检查设备是否具有NFC功能?
iOS 11 CoreNFC How To Check if device has NFC Capability?
如何检测iPhone是否有能力使用核心NFC框架提供的NFC芯片?
我现在知道它只适用于 iPhone 7 和 7 plus,但我不想硬编码硬件字符串标识符,因为我不知道将来会出现什么设备。
您可以使用 readingAvailable
class 属性:
if NFCNDEFReaderSession.readingAvailable {
// Set up the NFC session
} else {
// Provide fallback option
}
iOS12 的更新:
1) 如果您希望 运行 您的应用仅适用于 iPhone 7 和更新的型号,您可以在 Info.plist
:
中添加 NFC 要求
<key>UIRequiredDeviceCapabilities</key>
<array>
// ... your restrictions
<string>nfc</string>
</array>
根据此要求,只有具有 NFC 的设备才能从 App Store 下载我们的应用程序。
2) 对于早于 iPhone 7 的 iPhone 和 iPad 支持,您还必须检查 Core NFC
是否可用,因为它 不包含 对于这些设备。这就是为什么你应该 link Core NFC
框架使用 Weak Linking:
然后在代码中检查 Core NFC
可用性:
var isNFCAvailable: Bool {
if NSClassFromString("NFCNDEFReaderSession") == nil { return false }
return NFCNDEFReaderSession.readingAvailable
}
如果isNFCAvailable
returns true
那么您可以使用Core NFC
提供的所有API而不用担心您的应用崩溃。
同时检查 iOS < 11.0 的设备,即。 iPhone 5
import CoreNFC
.
.
// Check if NFC supported
if #available(iOS 11.0, *) {
if NFCNDEFReaderSession.readingAvailable {
// available
}
else {
// not
}
} else {
//iOS don't support
}
可以从Apple查看设备列表https://developer.apple.com/support/required-device-capabilities/(很长,使用:cmd-f + "nfc")
如何检测iPhone是否有能力使用核心NFC框架提供的NFC芯片?
我现在知道它只适用于 iPhone 7 和 7 plus,但我不想硬编码硬件字符串标识符,因为我不知道将来会出现什么设备。
您可以使用 readingAvailable
class 属性:
if NFCNDEFReaderSession.readingAvailable {
// Set up the NFC session
} else {
// Provide fallback option
}
iOS12 的更新:
1) 如果您希望 运行 您的应用仅适用于 iPhone 7 和更新的型号,您可以在 Info.plist
:
<key>UIRequiredDeviceCapabilities</key>
<array>
// ... your restrictions
<string>nfc</string>
</array>
根据此要求,只有具有 NFC 的设备才能从 App Store 下载我们的应用程序。
2) 对于早于 iPhone 7 的 iPhone 和 iPad 支持,您还必须检查 Core NFC
是否可用,因为它 不包含 对于这些设备。这就是为什么你应该 link Core NFC
框架使用 Weak Linking:
然后在代码中检查 Core NFC
可用性:
var isNFCAvailable: Bool {
if NSClassFromString("NFCNDEFReaderSession") == nil { return false }
return NFCNDEFReaderSession.readingAvailable
}
如果isNFCAvailable
returns true
那么您可以使用Core NFC
提供的所有API而不用担心您的应用崩溃。
同时检查 iOS < 11.0 的设备,即。 iPhone 5
import CoreNFC
.
.
// Check if NFC supported
if #available(iOS 11.0, *) {
if NFCNDEFReaderSession.readingAvailable {
// available
}
else {
// not
}
} else {
//iOS don't support
}
可以从Apple查看设备列表https://developer.apple.com/support/required-device-capabilities/(很长,使用:cmd-f + "nfc")