iosipadmultipeer-connectivitympc

Invalid displayName passed to MCPeerID


When I tried to pass device udid + device name, i received below crash

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid displayName passed to MCPeerID'


Solution

  • init(displayName myDisplayName: String)
    

    From Apple Docs:

    The maximum allowable length is 63 bytes in UTF-8 encoding.

    The displayName parameter may not be nil or an empty string.

    This method throws an exception if the displayName value is too long, empty, or nil.

    In your case, you are passing udid + device name which is exceeding the limit allowed i.e. 63 bytes (63 characters)

    and will throw an exception saying:

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid displayName passed to MCPeerID

    Just to Debug:

    print lengthOfBytes for your udid + device name before passing it in init(displayName myDisplayName: String) it would definitely exceeds the limit allowed.

    print((UIDevice.current.name+udid).lengthOfBytes(using: String.Encoding.utf8))