Home › Forums › Ask the Flomies › Write non-URL data to tag
Tagged: iOS, NDEF, Objective C
-
AuthorPosts
-
June 28, 2018 at 5:30 pm #63310
I am curious if there is a way to write an arbitrary string as an NDEF message using the iOS SDK/Objective C. I see that FmNdefMessage has a createURIWithString method, but it has no other public method to create a message from anything else.
Thanks!June 29, 2018 at 4:47 am #63314Hi Jeffrey,
Yes this is possible. You would need to create a FmNdefRecord with the specific details you’re looking for.
You can use Mime type records or Well Known Type records to store plain text.
Here is an example of a mime type record.TNF –
kTNFMimeMedia
Type –74 65 78 74 2f 70 6c 61 69 6e
(“text/plain” in hex)
id –nil
payload –<insert your text as ASCII bytes here>
(Use NSString’sdataUsingEncoding:NSASCIIStringEncoding
to convert your string to bytes)You would then use the
initWithNdefRecords
initializer inFmNdefMessage
Hope this helps,
Scott- This reply was modified 6 years, 4 months ago by Scott.
June 29, 2018 at 11:41 am #63321Hey Scott — thanks for the reply. I admit that my Objective C is far from fantastic, but in the version of FmNdefMessage that I have, the method you mention is marked as private (or whatever the Objective C equivilent is)
– (id)initWithNdefRecords:(NSArray *)ndefRecords;
Same with the FmNdefRecord. All of the properties you mention are “readonly”, and the only public constructors are
+ (FmNdefRecord *)createURIWithURL:(NSURL *)url;
+ (FmNdefRecord *)createURIWithString:(NSString *)uriString;None of the functions you mention appear in the autocomplete, and I get an error when I try to compile them. I get the error:
No known class method for selector ‘initWithTnf:andType:andId:andPayload:’
Can you point me to a code example somewhere?
Thanks
June 29, 2018 at 1:08 pm #63322Hi Jeffrey,
What version of the SDK are you using?
You may have to update.
flomio-sdk-ios repoHere is how I would do it:
NSData *type = [@"text/plain" dataUsingEncoding:NSASCIIStringEncoding]; NSData *payload = [@"Hello World!" dataUsingEncoding:NSASCIIStringEncoding]; FmNdefRecord *record = [[FmNdefRecord alloc] initWithTnf:kTNFMimeMedia andType:type andId:nil andPayload: payload]; FmNdefMessage *message = [[FmNdefMessage alloc] initWithNdefRecords:@[record]]; [tag writeNdef:message success:^(BOOL success) { if (success){ NSLog(@"Tag Writing Successful"); } }];
Hope that helps,
ScottJune 29, 2018 at 2:13 pm #63327I thought I had the master version of the SDK from Github, but I updated it and the code seems to work, so thanks!
June 30, 2018 at 4:43 am #63330 -
AuthorPosts
The topic ‘Write non-URL data to tag’ is closed to new replies.