.NET MAUI iOS端NFC标签扫描无法检测标签的问题求助
.NET MAUI iOS端NFC标签扫描无法检测问题排查求助
开发一款.NET MAUI应用,NFC标签扫描是核心需求之一。Android端从检测NFC标签、获取到处理Payload均已正常实现,但iOS端始终无法检测到NFC标签。
iOS NFC读取服务实现
实现的简易iOS NFC服务代码如下:
using CoreFoundation; using CoreNFC; using Foundation; using MSS.App.Services.Interfaces; using System; using UIKit; namespace OurAppNamespace.Platforms.iOS.Services { public class IosNfcService : NFCTagReaderSessionDelegate, INfcService { private readonly ILogService? _logService; private NFCTagReaderSession? _nfcSession; public IosNfcService(ILogService? logService) { _logService = logService; } public async void Enable() { _logService?.LogInfo($"{this}.Enable"); try { var isNfcAvailable = UIDevice.CurrentDevice.CheckSystemVersion(13, 0); _logService?.LogInfo($"{this}.Enable > {nameof(isNfcAvailable)} = {isNfcAvailable}"); if (isNfcAvailable) { _nfcSession = new NFCTagReaderSession(NFCPollingOption.Iso14443 | NFCPollingOption.Iso15693, this, DispatchQueue.CurrentQueue) { AlertMessage = "Test123" }; _nfcSession.BeginSession(); _logService?.LogInfo($"{this}.Enable > Session started."); } } catch (Exception exception) { _logService?.LogError($"{this}.Enable > Starting Reader Session failed.", exception); } } public override void DidBecomeActive(NFCTagReaderSession session) { _logService?.LogInfo($"{this}.DidBecomeActive"); } public override void DidInvalidate(NFCTagReaderSession session, NSError error) { _logService?.LogInfo($"{this}.DidInvalidate"); } public override void DidDetectTags(NFCTagReaderSession session, INFCTag[] tags) { _logService?.LogInfo($"{this}.DidDetectTags"); } public void Disable() { _logService?.LogInfo($"{this}.Disable"); _nfcSession?.InvalidateSession(); } } }
按照预期,手机靠近NFC标签时应触发DidDetectTags方法,但实际并未触发。
Info.plist配置
在Platforms/iOS下的Info.plist中添加了NFC权限配置:
<key>NFCReaderUsageDescription</key> <string>NFC tag to read NDEF messages into the application</string> <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key> <array> <string>com.apple.developer.nfc.readersession.iso7816.select-identifiers</string> <string>D2760000850100</string> </array>
Entitlements.plist配置
作为首个iOS目标应用,已按照官方指南配置Entitlements.plist:
- 在Platforms/iOS下添加空的Entitlements.plist文件
- 在项目设置的iOS>Bundle Signing中配置自定义权限文件
配置后项目文件中自动添加了对应的PropertyGroup:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'"> <CodesignEntitlements>Platforms\iOS\Entitlements.plist</CodesignEntitlements> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'"> <CodesignEntitlements>Platforms\iOS\Entitlements.plist</CodesignEntitlements> </PropertyGroup>
测试结果
已清理bin和obj文件夹,删除设备上的旧版本并重启设备后重新构建应用:
- 应用可正常打开,能看到
Enable方法的日志,但DidDetectTags始终未触发 - 若在Entitlements.plist中勾选“Near Field Communication Tag Reading”,应用无法部署,Xamarin日志报错:
Xamarin.Messaging.IDB.Local.DeployAppMessageHandler Error: 0 : An error occurred while trying to deploy the app '[APP_NAME]'. Details: Could not install the application '[PATH]\[APP_NAME]\out\[APP].ipa' on the device [TEST_DEVICE]. Details: ApplicationVerificationFailed|0xE8008015 - Failed to verify code signature of /var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.59k3xO/extracted/Payload/[APP_NAME] : 0xe8008015 (A valid provisioning profile for this executable was not found.) Xamarin.iOS.Windows.WindowsiOSException: Could not install the application '[LOCAL_PATH]\[APP_NAME]\out\[APP].ipa' on the device [TEST_DEVICE]. Details: ApplicationVerificationFailed|0xE8008015 - Failed to verify code signature of /var/installd/Library/Caches/com.apple.mobile.installd.staging/temp.59k3xO/extracted/Payload/[APP_NAME] : 0xe8008015 (A valid provisioning profile for this executable was not found.) at Xamarin.iOS.Windows.Installer.ApplicationSession.InstallApp(String appPath, String appBundleId) in D:\a\_work\1\s\src\Tools\Xamarin.iOS.Windows.Client\Installer\ApplicationSession.cs:line 276 at Xamarin.iOS.Windows.Installer.ApplicationSession.Deploy(String appRootFolder, String appBundleId, String appName) in D:\a\_work\1\s\src\Tools\Xamarin.iOS.Windows.Client\Installer\ApplicationSession.cs:line 95 at Xamarin.iOS.Windows.HotRestartClient.Deploy(AppleDevice nativeDevice, String appBundleId, String appBundleName, Boolean& incremental) in D:\a\_work\1\s\src\Tools\Xamarin.iOS.Windows.Client\HotRestartClient.cs:line 250 at Xamarin.Messaging.IDB.Local.DeployAppMessageHandler.<ExecuteAsync>d__5.MoveNext() in D:\a\_work\1\s\src\Messaging\Xamarin.Messaging.IDB.Local\Handlers\DeployAppMessageHandler.cs:line 43: 04/04/2024 16:20:09Z DateTime=2024-04-04T16:20:09.7343180Z: 04/04/2024 16:20:09Z
取消勾选后应用可正常运行,但仍无法检测NFC标签,特此求助。
内容的提问来源于stack exchange,提问作者BeRo
相关产品推荐
相关产品推荐

