{"_id":"59546dc1cb02cc0015729004","project":"55e67aaa9cc7c62b00c4a1ea","version":{"_id":"55e67aab9cc7c62b00c4a1ed","project":"55e67aaa9cc7c62b00c4a1ea","__v":10,"createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["55e67aac9cc7c62b00c4a1ee","55e67b5556007d23005fee7d","55e67b5dde6fef23009480ca","55e680efde6fef23009480db","55e6829485a9741900314e99","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","589b8e1fdbb7cd190026732c","58b8ca5e3265d70f001788d4"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"589b8e1fdbb7cd190026732c","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"55e67aab9cc7c62b00c4a1ed","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"user":"55d29988486de50d00327118","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-06-29T03:02:25.630Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":17,"body":"Near Field Communication (or NFC) is a reasonably modern technology that allows mobile devices to interact with terminals, swipe cards and fobs. \n\nAndroid and iOS 11 have support to give access to turn the phone into an NFC *reader*. With this detail you can send that data back to Carnival as Custom Attributes, and use these for audiences and automation.\n\n\n## iOS\nCoreNFC was released in iOS 11 and Xcode 9. In the callback of the CoreNFC class, decode the payload and send relevant data to Carnival. Note: Only NDEF formatted cards and devices can be read.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::available(iOS 11.0, *)\\nfunc readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {\\n    for message in messages {\\n        for record in message.records {\\n            print(record.payload)\\n            let optIdentifier = String(bytes: record.identifier, encoding: .utf8)\\n\\n            if let identifier = optIdentifier {\\n                let attrs = CarnivalAttributes()\\n                attrs.setString(identifier, forKey: \\\"NFC_ID\\\")\\n                Carnival.setAttributes(attrs, withResponse: nil)\\n            }\\n        }\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n## Android \n\nOn Android, the NFC library will fire an intent where you'll be able to decode the payload. Similar to iOS, you should send the data to Carnival from there.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// From the NFC Basics doc.\\n@Override\\nprotected void onNewIntent(Intent intent) {\\n    super.onNewIntent(intent);\\n    ...\\n    if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {\\n        Parcelable[] rawMessages =\\n            intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);\\n        if (rawMessages != null) {\\n            NdefMessage[] messages = new NdefMessage[rawMessages.length];\\n            for (int i = 0; i < rawMessages.length; i++) {\\n                messages[i] = (NdefMessage) rawMessages[i];\\n            }\\n            // Process the messages array.\\n           \\t// Send to Carnival.\\n        }\\n    }\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n## Further Reading\n\n * [iOS Documentation](https://developer.apple.com/documentation/corenfc)\n * [Android Documentation](https://developer.android.com/guide/topics/connectivity/nfc/index.html)\n * [Collecting User Data](doc:collecting-user-data)","excerpt":"","slug":"working-with-nfc","type":"basic","title":"Working with NFC"}
Near Field Communication (or NFC) is a reasonably modern technology that allows mobile devices to interact with terminals, swipe cards and fobs. Android and iOS 11 have support to give access to turn the phone into an NFC *reader*. With this detail you can send that data back to Carnival as Custom Attributes, and use these for audiences and automation. ## iOS CoreNFC was released in iOS 11 and Xcode 9. In the callback of the CoreNFC class, decode the payload and send relevant data to Carnival. Note: Only NDEF formatted cards and devices can be read. [block:code] { "codes": [ { "code": "@available(iOS 11.0, *)\nfunc readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {\n for message in messages {\n for record in message.records {\n print(record.payload)\n let optIdentifier = String(bytes: record.identifier, encoding: .utf8)\n\n if let identifier = optIdentifier {\n let attrs = CarnivalAttributes()\n attrs.setString(identifier, forKey: \"NFC_ID\")\n Carnival.setAttributes(attrs, withResponse: nil)\n }\n }\n }\n}", "language": "swift" } ] } [/block] ## Android On Android, the NFC library will fire an intent where you'll be able to decode the payload. Similar to iOS, you should send the data to Carnival from there. [block:code] { "codes": [ { "code": "// From the NFC Basics doc.\n@Override\nprotected void onNewIntent(Intent intent) {\n super.onNewIntent(intent);\n ...\n if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {\n Parcelable[] rawMessages =\n intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);\n if (rawMessages != null) {\n NdefMessage[] messages = new NdefMessage[rawMessages.length];\n for (int i = 0; i < rawMessages.length; i++) {\n messages[i] = (NdefMessage) rawMessages[i];\n }\n // Process the messages array.\n \t// Send to Carnival.\n }\n }\n}", "language": "java" } ] } [/block] ## Further Reading * [iOS Documentation](https://developer.apple.com/documentation/corenfc) * [Android Documentation](https://developer.android.com/guide/topics/connectivity/nfc/index.html) * [Collecting User Data](doc:collecting-user-data)