I am using the IOS Contacts Framework to get imageData strong> iPhone user. If imageData strong> is available, I get it and must encode it into a string. This is because I am using the Multiber Connectivity Framework , and I would like to display the image next to the peer display name in the list of peers being viewed. After initialization, I send the encoded imageData strong> using the discoveryInfo dictionary parameter of the MCNearbyServiceAdvertiser class .
For this, I tried to encode imageData strong> using UTF8 and Base64 , but both failed.
This is the code I tried for the UTF8 process :
// Retrieve user image and initials from global userContact variable
func retrieveUserImageAndInitials() -> Dictionary<String,String> {
let userFirstNameInitial = "\(userContact.firstName[userContact.firstName.startIndex])"
let userLastNameInitial = "\(userContact.lastName[userContact.lastName.startIndex])"
if userContact.imageData != nil {
print("Image data found")
if let dataString = NSString(data: userContact.imageData!, encoding: NSUTF8StringEncoding) as String! {
print("Image data encoded successfully")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial,"imageData":dataString]
}
else {
print("Image data encoded with failures")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
else {
print("Image data not found")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
And this is the code for decoding using UTF8 :
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
foundPeers.append(peerID)
let userInitials = info!["firstNameInitial"]! + info!["lastNameInitial"]!
if let dataString = info!["imageData"] {
print("Data string found")
if let imageData = dataString.dataUsingEncoding(NSUTF8StringEncoding) {
print("Image data fetched succesfully!")
imagesOfFoundPeers.append(UIImage(data: imageData)!)
}
else {
print("Image data not fetched")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
}
else {
print("Data string not found")
print("Image with initials used instead")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
delegate?.foundPeer()
}
When using UTF8 encoding, the String string that I get from the encoding process prints as zero, so the code can never send it through the advertiser.
This is the code I tried for the Base64 process :
// Retrieve user image and initials from global userContact variable
func retrieveUserImageAndInitials() -> Dictionary<String,String> {
let userFirstNameInitial = "\(userContact.firstName[userContact.firstName.startIndex])"
let userLastNameInitial = "\(userContact.lastName[userContact.lastName.startIndex])"
if userContact.imageData != nil {
print("Image data found")
let dataString = userContact.imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
print("Image data encoded successfully")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial,"imageData":dataString]
}
else {
print("Image data not found")
return ["firstNameInitial":userFirstNameInitial, "lastNameInitial":userLastNameInitial]
}
}
And this is the code for decoding using Base64 :
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?)
{
foundPeers.append(peerID)
let userInitials = info!["firstNameInitial"]! + info!["lastNameInitial"]!
if let dataString = info!["imageData"] {
print("Data string found")
let imageData = NSData(base64EncodedString: dataString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
let userImage = UIImage(data: imageData!)
print("Image data fetched succesfully!")
imagesOfFoundPeers.append(userImage!)
}
else {
print("Data string not found")
print("Image with initials used instead")
imagesOfFoundPeers.append(imageFromText(userInitials,
font: UIFont(name: "Pacifico", size: 24.0)!, maxWidth: 50.0, color: UIColor.mainColor()))
}
delegate?.foundPeer()
}
Base64 String, , discoveryInfo, , , MCNearbyServiceAdvertiser.
:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid discoveryInfo passed to MCNearbyServiceAdvertiser'
MCNearbyServiceAdvertiser MPCManagement:
override init()
{
super.init()
userContact = retrieveUserContact()
peer = MCPeerID(displayName: userContact.fullName)
dataSession = MCSession(peer: peer)
dataSession.delegate = self
deviceBrowser = MCNearbyServiceBrowser(peer: peer, serviceType: "dummy-mpc")
deviceBrowser.delegate = self
deviceAdvertiser = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: retrieveUserImageAndInitials(), serviceType: "dummy-mpc")
deviceAdvertiser.delegate = self
}
- , ?
!