How do I retrieve the name of participants in MSConversation?
Asked Answered
B

1

7

MSConversation gives us the local participant and remote participants. However, I am unable to retrieve the display name of self or other. How do I get these names? https://developer.apple.com/reference/messages/msconversation

let ids = activeConversation?.remoteParticipantIdentifiers
let otherId = ids?[0].uuidString
let ownId = activeConversation?.localParticipantIdentifier.uuidString
let predicate = CNContact.predicateForContacts(withIdentifiers: [otherId!]);

do { 
    let contacts = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: [CNContactFamilyNameKey, CNContactGivenNameKey, CNContactNicknameKey, CNContactIdentifierKey])

    for contact in contacts{
        print(contact.givenName)
        print(contact.familyName)
        print(contact.identifier)
    }

} catch let err{
    print(err)
}

As above I have tried to search CNContactsStore, the UUID from MSConversation is different from CNContact.

Bedight answered 10/8, 2016 at 4:19 Comment(0)
E
10

Unfortunately this is not possible to retrieve any names. The only thing you can do is retrieving the UUID of the local and remote participants. Then you can display there names only into the transcript of the conversation. To do so, when you set a new MSMessage, don't forget the sign $ in the string :

    let message = MSMessage(session: theCurrentSession)
    let layout = MSMessageTemplateLayout()
    layout.caption = "$\(uuidOfTheParticipant) said something"
    message.layout = layout
    

Note : In objective C you don't need to put "\( )" after the "$", this is used only in swift ;)

This will automatically display the name of the corresponding UUID at the bottom of the MSMessage. Have a look here if you want to learn more about the MSMessage layout : https://developer.apple.com/reference/messages/msmessagetemplatelayout

Also, keep in mind that the UUID of the participant is relative to the conversation itself, it will be consistant into one conversation, but will be different for each participants (the UUID identifying me on my device will be different on others devices). Also if a user uninstall your app and reinstall it, all UUID will be different.

So to answer your question you can not rely on this UUID to identificate any user with the CNContact, they are different ;)

Eisenstein answered 10/8, 2016 at 9:57 Comment(5)
thanks for the 'magical' "$(uuid)", can you explain how the dollar part comes about? I cannot find any ref as yetBedight
Hum honestly I don't really know. This comes from the WWDC, and I agree with you, it is a magical $. I guess it's something coming from the SDK. If someone could add some more explanations on that, it would be great :)Eisenstein
I just tried to assign it to a label.text, NSLog all no go. Looks like this translation is specific to message layout. Weird!Bedight
Yes indeed, this is explained in the WWDC video about iMessage. The name of participants will be display only in the transcript of the conversation, meaning the only place you can set it is in the MSMessage layout. As it is still in beta, they might implement more feature in the future...Eisenstein
I tried using this API, but could only get it to show the participants phone number rather than their name. Can you still get it to show the name?Outrank

© 2022 - 2024 — McMap. All rights reserved.