How can I parse a mailto request ?
'mailto:someone@example.com?cc=someone_else@example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body'
From this NSURL, I want to extract the recipient, the subject and the body. How should I do ?
Thanks
Since iOS 7 this is easily doable with NSURLComponents. You can create that object with:
Then you can get the recipient accessing the path property of NSURLComponents; and the parameters with the queryItems property. For instance, if we wanted to get the subject, something like this would do our job
Here is some code that will parse any URL and return a dictionary with the parameters and the associated objects in a dictionary. It works for
mailto
URLs, too.Please note: This code assumes you're using ARC!
And here is how you use it:
EDIT:
I updated the code to work with any URL and recipients are now in the dictionary for
mailto
URLs.I would pull the email from that like this:
NSURL category for just mailto: This method also has a fix for a crash bug in Fabian's answer above when mailto: url doesn't contain a ?. It also doesn't require the URLDecodedString category method.