NFCTagReaderSession缺少必需的权利

时间:2019-10-10 12:45:02

标签: ios swift iphone

我正在尝试使用ISO7816通过corenfc启动通信,但是即使我尝试了几次发布在Internet上的请求,也总是遇到相同的错误。

代码是

@IBAction func scanPressed(_ sender: Any) {
        nfcSession = NFCTagReaderSession.init(pollingOption: .iso14443, delegate: self)
        nfcSession?.alertMessage = "Hold your IPhone near the ISO7816 tag to begin transaction.";
        nfcSession?.begin()
    }

我的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">
<plist version="1.0">
<dict>
    <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
    <array>
        <string>D2760000850101</string>
    </array>
    <key>com.apple.developer.nfc.readersession.formats</key>
    <array>
      <string>NDEF</string>
      <string>TAG</string>
    </array>  
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NFCReaderUsageDescription</key>
    <string>Message in a Card</string>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
        <string>nfc</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

我收到以下错误

  

该会话无效:错误域= NFC错误代码= 2“缺少所需的权利” UserInfo = {NSLocalizedDescription =缺少所需的权利}

如何解决?

1 个答案:

答案 0 :(得分:0)

您的权利应位于名为NFCTagReaders.entitlements的文件中,并且应包含以下内容:

<?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">
<plist version="1.0">
<dict>
    <key>com.apple.developer.nfc.readersession.formats</key>
    <array>
        <string>NDEF</string>
        <string>TAG</string>
    </array>
</dict>

我不确定为什么在您的情况下它包含在主Info.plist文件中。 可能与您的问题有关,因为您的配置看起来正确。

Info.plist和NFCTagReaders.entitlements文件:

screenshot Info.plist and NFCTagReaders.entitlements files

请同时检查项目配置中是否激活了NFC功能。

您还应该在开始会话之前通过添加以下行来检查NFC会话是否可用:

guard NFCTagReaderSession.readingAvailable else {
        print("NFC Session not available")
        return
    }