Swift CoreNFC as quick as possible

Swift CoreNFC as quick as possible

https://medium.com/@myrickchow32/swift-corenfc-as-quick-as-possible-79544796cd0a

 

NFC stands for “Near Field Communication”. Two electronic devices can transmit data through NFC when they are distanced fewer than 4 cm apart. NFC can be used for electronic payment. It was invented many years ago but it has just been introduced by Apple to iPhone series since the launch of iPhone7 in 2016. To test an app with NFC feature, a real device must be used instead of an iOS simulator.

Tosetup a NFC project, the “NFC Tag Reading” must be enabled in the “App Services” session during registering an App ID in the Apple Developer Console.

A screenshot of the App Services session during registering an App ID in Apple Developer Console

Secondly, the “Near Field Communication Tag Reading” has to be enabled at the “Capabilities” tab of the app target session.

“Near Field Communication Tag Reading” in the “Capabilities” tab

Thirdly, “Privacy — NFC Scan Usage Description” must be added to the Info.plist. Otherwise, app is terminated by libsystem_kernel.dylib __pthread_kill with no error message at Xcode console.

Info.plist screenshot

IfNFC in your app is an optional feature and you probably wants those device without NFC chip to run your app, the CoreNFC.framework MUST be set to “Optional” in iOS 12.

Else, app will be crash at start with the following message showing “CoreNFC.framework” cannot be loaded.

dyld: Library not loaded: /System/Library/Frameworks/CoreNFC.framework/CoreNFC
Referenced from: /var/containers/Bundle/Application/DF6D870C-4928-40F6-A242-BAB9C07B3737/BaseProject.app/BaseProject
Reason: image not found

Let’s start coding now!

 

Implementing NFCNDEFReaderSessionDelegate

 

How to interpret the NFC information from [NFCNDEFMessage] Object?

The NFC information is stored inside the payload of record within the NFCNDEFMessage object.

Each NFCNDEFMessage object contains a list of “record” and each record contains:
 1. “payload” in form of NSData and contains tag message
 2. “typeNameFormat”
 3. “type”
 4. “identifier”

Example:

  • NFC tag original message: “abc”
  • Original message in ASCII code: 
     — 0: 97 [a]
     — 1 : 98 [b]
     — 2 : 99 [c]
  • Scanned payload in record of NFCNDEFMessage
    — 0 : 2 [START OF TEXT] in ASCII table.
     — 1 : 101 [e]
     — 2 : 110 [n]
     — 3 : 97 [a]
     — 4 : 98 [b]
     — 5 : 99 [c]

Why there is “en” in front of the original message “abc”?

“All language codes MUST be done according to RFC 3066 [RFC3066]. The language code MAY NOT be omitted. The language code length is encoded in the six least significant bits of the status byte. Thus it is easy to find by masking the status byte with the value 0x3F.”
 — NFC Data Exchange Format (NDEF) Technical Specification, NFC Forum

Thus, the payload of record has to be advanced by 3 before converting to a String for displaying!

record.payload.advanced(by:3)

Handling result at the didDetectNDEFs

Case 1 (init with parameter — invalidateAfterFirstRead: true):

If NFC reader session is initialised with invalidateAfterFirstRead equals true, NFC tag reading dialog will be automatically dismissed after tag is successfully detected and the blue-tick animation is completed (last for around 3 seconds).

 

Due to this 3-second time gap, UIAlertController is presented but covered by the NFC tag reading dialog completely, and user cannot interact with anyUIAlertAction. This results in a bad user experience. See below gif:

Case 2 (init with parameter — invalidateAfterFirstRead: false):

NFC reading tag dialog can be dismissed manually if NFCNDEFReaderSession is initialised with invalidateAfterFirstRead equals false.

 

However, the NFC session can actually be dismissed after the blue-tick animation is completed. Thus, calling nfcSession.invalidate() right before showing UIAlertController does not guarantee the NFC tag reading dialog is actually dismissed. There is an around 0.5 second time gap for this. Thus, the UIAlertController is still shown behind the NFC tag reading dialog for a noticeable short period of time. User experience is not perfect.

Case 3 (init with parameter — invalidateAfterFirstRead: false + 1s delay):

Finally, the solution to provide the best user experience is to give an around 1 second delay to the DispatchQueue to ensure that the NFC tag reading dialog is completely dismissed before showing an UIAlertController.

 

Minor reminders:

  1. The NFCNDEFReaderSession has a default 60 seconds time out for reading NFC tag. Below is the error message for time out.
Session is invalidated due to maximum session timeout

2. Alert message in the NFC tag reading dialog is optimised for 3 lines of wordings. The 4th line is truncated at the end.

3. Titles of NFC tag reading dialog and cancel button are not customisable and the locale is fixed to the device locale.

Conclusion:

  1. Ensure “NFC Tag Reading” is enabled for your app at the Apple Developer Console.
  2. “Near Field Communication Tag Reading” has to be checked in app capabilities list.
  3. Make sure your real device has NFC chip or setting CoreNFC.framework to be an optional framework.
  4. “Privacy — NFC Scan Usage Description” must be filled in to request the permission of using user’s NFC chip.
  5. Create the NFCNDEFReaderSession with invalidateAfterFirstRead: false , and give one second delay to show any UIAlertController or navigate to any page.
  6. didDetectNDEFs callback is run in background thread and never update UI directly.

Have a nice day and enjoy programming ^_^ !

博主设置当前文章不允许评论。

it' possible !

10-13

如今呆得实在无聊,除了上网还是上网! 在网上漫游过程中,我发现了一个问题, 为什么各个网站都能为你赚到很多钱呢?就连读一封 E-mail 点几下广告栏都能挣到钱? 难以置信,天下哪免费的午餐?可呆着实在是无聊反正闲着也是闲着,找了一个不要身份,不要会员费,只要有个地址,电号码就能挣到钱的网站,申请成了会员……(反正不交钱,挣不到钱也不会吃亏^o^)等了一个月,两个月…… 什么汇款单?连个影子都没有!不过一想,无所谓反正也没吃亏… ^^ 就当是鬼迷心窍一回吧! rn可一直到了3个月…… 突然……汇款单到了?不看不知道,一看吓一跳!9百多美金?折合人7665元币不就是7千多了吗? 半信半疑,到了银行,交了几十元的手续费,换回了7千多…… 真像做梦一样……买了些一直想买的东东…… 兴奋之余,又开始,我的宣传!宣传越多,挣的越多吗 ^o^ 果然,不到一个月又飞来了一张单子! 1千3百多美金…… 真是难以置信!又过了一个月上涨了2千4百多美金…… 如今,不用上班也有钱花了,真是乐哉乐哉! 你不信?那没办法只可惜这白花花的银子喽…… 不过,古人云:“宁可信其有,不可信其无”反正也不吃亏,就疯一回试试看嘛…… 相信我,没错的! rn****加入方法*** 很简单哦******http://www.mintmail.com/?m=1249041 *********进入网站(不点击,要用复制、粘贴到地址栏) 打开网页,点击右方黄色的click here 活动图标, 或点击蓝色 FREE Sign-up page 字样也可以!然后,跟着提示,一步一步输入信息就ok了值得高兴的是它能认别中文信息,而能100%加入。下面是我为了方便大家的加入详细说明了加入过程中的细节:rn   - First name*: 名字(例:张三) rn   - Last Name*: 姓 (例:张 ) rn   - Company Name: 可不填 rn   - Street Address*: 家庭住址:(一定要详细填写,不然收不到汇款单喽!) rn  例:武汉市**地方rn   - City*: 城市名 (例:wuhan 英文填写 ) rn   - State*: 可不填 rn   - Zip*: 邮编 ( 填 000-000 ) rn   - Country*: 国家( 选 china ) rn   - Phone*: 电话号码 ( 国际代码 86 + 去掉区位号前0 的电话号码) rn  例:020-64243365 → 86-20-5663447) rn   - Fax: 可不填 rn   - E-mail*: 电子信箱(所有的交流都通过信箱传递所以务必填写正确) rn   - Confirm E-mail*: 再次输入信箱地址 ***** rn- Year of birth*: 出生年例:1970rn   - Gender*: 性别 Male(男), F-emaie (女) rn   - Password*: 密码 (6位以上) rn   - Confirm Password: 确认密码 (须与上相同)****** rn   - how do you want to receive commissions that you earn? rn  以什么形式接收礼? rn*gift certificates(double$$) 奖品 *cash 现金rn如要奖品能收到双价格的东西, rn  但都是一些英文版书籍、磁带、光盘 等对于中国人来说,还是选择现金比较合算些rn;,请选择 cash rn   - do you want to be notified when your referrals sing up? rn  加入会员成功时通知你吗?选 yes rn   - MintMail.com 请选择自己的爱好或兴趣 (最多可选) rn   - Submit 点击它 屏幕上就会出现 thank you 的字样连同时你的ID(用户名数字)和密码也会出现在屏幕上记住它,加入完毕就一切ok了!恭喜发财 ^o^ rn  然后,五分钟之内你会收到一封欢迎信!. rn   *****宣传方法***** rn   http://www.mintmail.com/?m=1249041 rn  先介绍给你的亲朋友们,然后,到各个网站留言板中,把上面的内容全部复制、粘贴进去。 rn好了,那就祝你多发财了…… 不,应该是祝我们一起发大财!这才对啊! 论坛

Mission Possible

09-27

Problem DescriptionnMr. Frog loves playing video games. However, it seems that he does not have enough gift in this field, so he often gets stuck in some certain levels. Now this annoying situation comes to him again.nnIn this mission, Mr. Frog is asked to pass andangerous region which is currently under his enemies, control. Every second he is inside this region, his enemies would attack him with everything they have. Luckily, he is wearing a powersuit and still have time to enhance it.nnpowersuit has three main attributes: health point, velocity and recover speed. In the begin¬ning of this mission, the powersuit has H health points, and is able to move at no faster than Vm/s. Once the health point of powersuit is less than 0, the powersuit would be totally destroyed and Mr. Frog would immediately be killed. At the end of every second that the powersuit still works, the suit would repair itself and recover R health points.nnAfter precise calculation, Mr. Frog has found that, to go through this area, he needs to run at least D meters distance, and every second his enemies, attack would cause A health point loss. Note that attacks take place all the time while the recover only happens at the end of every second, so you can consider that, in every whole second, the powersuit first loses A health points and then recovers R health points at the end of it, if it still works at that moment.nnMr. Frog could enhance his powersuit at any time he wants during the mission. At the beginning all three attributes of his powersuit equals to 0. He needs to pay G1 in order to increase H by 1, while G2 to increase V by 1 and G3 to increase R by 1. For any of these three values, he can only increase it by non-negative integer. For some well-known reasons, Mr. Frog does not want to finish the game too quickly, so you cannot increase the powersuit's speed to more than D m/s. Since you looks so clever, now Mr. Frog wants to know not only how he can finish this mission, but also the way to spend the least money, can you help him?n nnInputnThe first line contains only one integer T, which indicates the number of test cases. For each test case,there are five integers D,A,G1,G2,G3.n nnOutputnFor each test case,output one line “Case #x: Ans’’,where x is the case number (starting from 1) and Ans is the minimum cost required to finish the mission.n nnSample Inputn2n5 1 1 2 5n10 1 1 2 5n nnSample OutputnCase #1: 7nCase #2: 8 问答

没有更多推荐了,返回首页