读取HCE模拟NFC标签时遇Missing required entitlement错误求助
问题:读取HCE标签时调用sendCommand报错"Missing required entitlement"
错误信息:
-[NFCTagReaderSession _connectTag:error:]:744 Error Domain=NFCError Code=2 "Missing required entitlement" UserInfo={NSLocalizedDescription=Missing required entitlement} ERROR: Tag is not connected
我的配置文件
Entitlements文件
<dict> <key>aps-environment</key> <string>development</string> <key>com.apple.developer.nfc.readersession.formats</key> <array> <string>TAG</string> <string>NDEF</string> </array> </dict>
Info.plist配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key> <array> <string>F00102030405</string> </array>
我的代码
func startNFCSession() { let pollingOptions: NFCTagReaderSession.PollingOption = [.iso14443] nfcSession = NFCTagReaderSession(pollingOption: pollingOptions, delegate: self, queue: nil) nfcSession?.alertMessage = NSLocalizedString("Hold the device close to scan the ticket.", comment: "") nfcSession?.begin() } func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) { if tags.count > 1 { session.alertMessage = NSLocalizedString("Multiple tags detected. Try again.", comment: "") session.restartPolling() return } guard let firstTag = tags.first else { session.invalidate(errorMessage: NSLocalizedString("No tags found", comment: "")) return } guard case let .iso7816(iso7816Tag) = firstTag else { session.invalidate(errorMessage: NSLocalizedString("Incompatible tag", comment: "")) return } session.connect(to: firstTag) { (error: Error?) in if let error = error { session.invalidate(errorMessage: String(format: NSLocalizedString("Connection error: %@", comment: ""), error.localizedDescription)) return } let apduCommand = NFCISO7816APDU( instructionClass: 0x00, instructionCode: 0xA4, p1Parameter: 0x04, p2Parameter: 0x00, data: Data([0xF0, 0x01, 0x02, 0x03, 0x04, 0x05]), expectedResponseLength: -1 ) iso7816Tag.sendCommand(apdu: apduCommand) { (responseData, sw1, sw2, error) in if let error = error { print("ERROR: \(error.localizedDescription)") session.invalidate(errorMessage: String(format: NSLocalizedString("Error sending APDU: %@", comment: ""), error.localizedDescription)) } else { self.handleResponse(session: session, responseData: responseData, sw1: sw1, sw2: sw2) } } } }
问题原因及解决方案
问题原因
你的entitlements文件缺少ISO7816协议对应的权限声明。虽然info.plist中配置了com.apple.developer.nfc.readersession.iso7816.select-identifiers,但读取ISO7816类型的HCE标签必须在entitlements中同时声明该权限项,否则系统会判定缺少必要权限。
解决方案
修改entitlements文件,添加com.apple.developer.nfc.readersession.iso7816.select-identifiers键,确保其值与info.plist中的配置完全一致:
<dict> <key>aps-environment</key> <string>development</string> <key>com.apple.developer.nfc.readersession.formats</key> <array> <string>TAG</string> <string>NDEF</string> </array> <!-- 添加ISO7816权限项 --> <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key> <array> <string>F00102030405</string> </array> </dict>
额外检查项
- 确认Apple开发者账号已开启NFC Tag Reading权限,Xcode项目配置中关联的开发者证书包含该权限。
- 清理Xcode缓存,重新构建并安装应用——权限变更需要重新签名才能生效。
- 验证HCE模拟标签使用的AID与配置的
F00102030405完全一致,大小写匹配。
内容的提问来源于stack exchange,提问作者Emilio Cubo Ruiz
相关产品推荐
相关产品推荐

