When my CoreNFC application reads a tag that contains a valid URL, it attempts to open this URL.
This works for a while, but suddenly the switch no longer completes: The other application (e.g. Safari) starts to open and then hangs on a black screen. Just to debug things, I am launching my own test application and can see that applicationDidBecomeActive is called, but that's the end of it.
I created another test application that does the exact same thing as my NFC application, except that the app switch is initiated by clicking a button instead of scanning a tag. This application works fine. So I think the issue is somehow tied to NFC itself, possibly a bug.
If anyone wants to reproduce this, start by downloading the sample app here: https://developer.apple.com/sample-code/wwdc/2017/CoreNFC-Tag-Reading.zip
Once you verify that the app is running, add this code to the MessagesTableViewController right after "detectedMessages.append(contentsOf: messages)":
let message = messages[0]
let record = message.records[0]
if let type = String(data: record.type, encoding:.utf8) {
if type == "U" {
if let payloadText = String(data: record.payload, encoding:.utf8) {
var url = payloadText
let prefix = url.remove(at: url.startIndex)
if prefix == "\u{0}" {
if let realURL = URL(string: url) {
DispatchQueue.main.async {
UIApplication.shared.open(realURL, options: [:], completionHandler: nil)
}
}
}
}
}
}In my case, I am scanning a variety of tags containing one single "Well Known" NDEF Record of Type "URL" each. Some examples:
- fb://feed (launches Facebook)
- tel:+415555555 (calls number)
- twitter://timeline (opens Twitter)
- http://www.apple.com (Opens Safari)
Sooner or later, after scanning tags for a while, the problen starts to appear.
Does anyone else see this problem? Am I making a mistake, or should I report this as a possible bug?
Thanks! Martin