I just installed the first iOS 11 beta to an iPhone 7 and am interested in trying the NFC. There's nothing about it in settings. I am wondering if there's any sample code out there showing how to read a tag. Can anyone show how to use the Core NFC SDK, in a code snippet?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
I added Core NFC to a project using the resources in these answers. One additional thing that wasn't noted though was that even if you add the capability manually via the entitlements, Xcode doesn't seem to look at the file unless you have a capability turned on. This is probably due to Xcode 9 Beta 1 not having Core NFC as a capability switch for lots of people's sample projects. So just be sure to turn at least one other capability on if you're still seeing issues! I was seeing an unexpected termination error immediately return until I did this.
I would leave this as a comment as it belongs, but don't have enough reputation yet to do so. Figured this was important enough to note.
Just to enrich previous answers, it's important to bear in mind these considerations specifically of the
NFCNDEFReaderSession
class:com.apple.developer.nfc.readersession.formats
" entitlement in your process. In addition your application'sInfo.plist
must contain a non-empty usage description string.-beginSession
is called to inform the start of the session; the UI sheet is automatically dismissed when the session is invalidated either by the user or by calling-invalidateSession
.-beginSession
is called;-readerSession:didInvalidateWithError:
will returnNFCReaderSessionInvalidationErrorSessionTimeout
error when the time limit is reached.-readerSession:didInvalidateWithError:
will returnNFCReaderSessionInvalidationErrorSystemIsBusy
when a new reader session is initiated by-beginSession
when there is an active reader session.-readerSession:didInvalidateWithError:
will returnNFCReaderSessionInvalidationErrorUserCanceled
when user clicks on the done button on the UI.-readerSession:didInvalidateWithError:
will returnNFCReaderSessionInvalidationErrorSessionTerminatedUnexpectedly
when the client application enters the background state.-readerSession:didInvalidateWithError:
will returnNFCReaderErrorUnsupportedFeature
whenYou need to make sure the usage description is in place and also add the capability to the app inside of the Apple Developer Center. I have a tutorial based on my experience (Swift 4-based). It's available here: Core NFC Tutorial
Updated for second Xcode beta.
Add the NFC capability to your app from the Developer Center. Identifiers -> App IDs -> enable "NFC Tag Reading".
If your project does not have an entitlement file, let Xcode create one for you by just activating and then subsequently deactivating any capability from within Xcode -> Project Targets -> Capabilities. You will find a new [AppName].entitlements file in your project navigator. Right-click on that file and select "Open as -> Source Code". Enter the following entry manually between
<dict></dict>
:As soon as Xcode 9 allows enabling NFC Tag Reading from the Capabilities selection this step becomes obsolete because all you have to do is to enable it there. The current (first) Beta version does not support this.
You also need to enter a usage description for the privacy warning iOS will show the user. (At the moment (beta 1) this warning will be displayed when the device is ready to scan and shows a native system dialogue which will include this message. However, this seems to be unploished.) Open your target's Info.plist and start typing "Privacy" and you can scroll down to "Privacy - NFC Usage Description" to select it by hitting return. Enter a meaningful explanation to your user in the right column.
Now you should be able to import CoreNFC, in Swift:
Then head over to Apple's documentation.
Important: If the compiler returns an error withBeta 2 doesn't have this issue anymore. However, actual hardware in form of iPhone 7/p is still required for actual testing.No such module 'CoreNFC'
check if you have selected an actual iOS 11 device to build for, not a simulator. It also has to be the iPhone 7 or 7 plus. This might change in a future version, but testing NFC will only ever be fully working with actual hardware. (cp. Core Bluetooth where you can run on Simulator but not test the actual capabilities.)For fix this trouble you should add
com.apple.developer.nfc.readersession.formats
key into your entitlements file. The key should be associated with array of enabled nfs types. As example, you can add this into you entitlements.<key>com.apple.developer.nfc.readersession.formats</key> <array> <string>NDEF</string> </array>
It worked for me.
my two cents:
1) under xcode 9.0 (beta 4 9M189t ) if You have already added capabilities, no need to manually add:
it is done automatically
2) no crash if not using iPhone 7 OR you are in simulator:
You wil be called in:
it will show: "Feature not supported"
3) dont miss:
so:
4) if user cancels, you will get:
"Session is invalidated by user cancellation"
in didInvalidateWithError callback.