How to save user information in MSMessage?

In iMessage Extensions for iOS10, when a user clicks on an interactive message bubble:

How to save user information in a sent message so that, when pressed, the extension is able to receive this user information and recognize the type of branch message for the corresponding response?

Thanks!

+6
source share
3 answers

I just figured it out by looking at the Apple Ice Cream Code Sample .


Decision

When creating the MSMessage that you are about to send, use the NSURLComponents object to save user information in the QueryItems property.

An example :

MSMessage* message; NSURLComponents* urlComponents; // init message = [[MSMessage alloc] init]; urlComponents = [NSURLComponents componentsWithURL:[NSURL URLWithString:@"http://yourwebsite.com"] resolvingAgainstBaseURL:NO]; // Saving Custom Information as query items. [urlComponents setQueryItems:@[[NSURLQueryItem queryItemWithName:@"messageType" value:@"1"], [NSURLQueryItem queryItemWithName:@"username" value:@"Jorge"], [NSURLQueryItem queryItemWithName:@"userId" value:@"99999"], [NSURLQueryItem queryItemWithName:@"userPhoto" value:@"http://yourwebsite.com/9999.jpg"]]]; // Setting message URL [message setURL:[urlComponents URL]]; 

Final URL :

The final URL added to MSMessage will look like this:

http://yourwebsite.com?messageType=1&username=Jorge&userId=99999&userPhoto=http://yourwebsite.com/99999.jpg

These additional request elements in the URL will be ignored . I mean, if your site is not designed to process these request elements, it simply ignores them when the user picks up this message bubble and opens the URL in the browser from a device with iOS version less than 10 (iOS9, iOS8, ... )

The only drawback that I see here is the disclosure of user information to the user (when opening the URL). Perhaps Apple should create a userInfo property in MSMessage .

Receive message

And this is how you extract information from the received message:

 MSMessage* message; NSString* messageType, *username, *userId, *userPhoto; // init message = [self.activeConversation selectedMessage]; if (message) { NSURLComponents *urlComponents; NSArray* queryItems; // Extracting message URL coponents. With this URL we'll able to figure out the type of the message. urlComponents = [NSURLComponents componentsWithURL:[message URL] resolvingAgainstBaseURL:NO]; queryItems = [urlComponents queryItems]; // Extracting info from the query items. for (NSURLQueryItem* item in queryItems) { if ([[item name] isEqualToString:@"messageType"]) messageType = [item value]; else if ([[item name] isEqualToString:@"username"]) username = [item value]; else if ([[item name] isEqualToString:@"userId"]) userId = [item value]; else if ([[item name] isEqualToString:@"userPhoto"]) userPhoto = [item value]; } } 
+3
source

Since this is an iOS 10 question, I hope the next answer in Swift will be useful to others. (Original answer by @jmoukel, just converted to Swift by me).

 let message = MSMessage() guard let url = NSURL(string: "http://yourwebsite.com") else { return } guard let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) else { return } urlComponents.setQueryItems([ "messageType": "1", "username":"Jorge", "userId":"99999", "userPhoto":"http://yourwebsite.com/9999.jpg" ]) message.setURL(urlComponents.URL!) 
+4
source

MSMessage url Property is where you can save your user data.

You can also use the iMessageDataKit library. This makes it easy to configure and retrieve data:

 let message: MSMessage = MSMessage() message.md.set(value: 7, forKey: "user_id") message.md.set(value: "john", forKey: "username") message.md.set(values: ["joy", "smile"], forKey: "tags") print(message.md.integer(forKey: "user_id")!) print(message.md.string(forKey: "username")!) print(message.md.values(forKey: "tags")!) 

It also supports storing arrays.

(Disclaimer: I'm the author of iMessageDataKit )

0
source

All Articles