Created
November 8, 2010 17:11
-
-
Save futuretap/667952 to your computer and use it in GitHub Desktop.
Intercept mailto URLs in a UIWebView and send them to a MFMailComposeViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { | |
if ([[[request URL] scheme] isEqualToString:@"mailto"]) { | |
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; | |
mailViewController.mailComposeDelegate = self; | |
NSArray *rawURLparts = [[[request URL] resourceSpecifier] componentsSeparatedByString:@"?"]; | |
if (rawURLparts.count > 2) { | |
return NO; // invalid URL | |
} | |
NSMutableArray *toRecipients = [NSMutableArray array]; | |
NSString *defaultRecipient = [rawURLparts objectAtIndex:0]; | |
if (defaultRecipient.length) { | |
[toRecipients addObject:defaultRecipient]; | |
} | |
if (rawURLparts.count == 2) { | |
NSString *queryString = [rawURLparts objectAtIndex:1]; | |
NSArray *params = [queryString componentsSeparatedByString:@"&"]; | |
for (NSString *param in params) { | |
NSArray *keyValue = [param componentsSeparatedByString:@"="]; | |
if (keyValue.count != 2) { | |
continue; | |
} | |
NSString *key = [[keyValue objectAtIndex:0] lowercaseString]; | |
NSString *value = [keyValue objectAtIndex:1]; | |
value = (NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, | |
(CFStringRef)value, | |
CFSTR(""), | |
kCFStringEncodingUTF8); | |
[value autorelease]; | |
if ([key isEqualToString:@"subject"]) { | |
[mailViewController setSubject:value]; | |
} | |
if ([key isEqualToString:@"body"]) { | |
[mailViewController setMessageBody:value isHTML:NO]; | |
} | |
if ([key isEqualToString:@"to"]) { | |
[toRecipients addObjectsFromArray:[value componentsSeparatedByString:@","]]; | |
} | |
if ([key isEqualToString:@"cc"]) { | |
NSArray *recipients = [value componentsSeparatedByString:@","]; | |
[mailViewController setCcRecipients:recipients]; | |
} | |
if ([key isEqualToString:@"bcc"]) { | |
NSArray *recipients = [value componentsSeparatedByString:@","]; | |
[mailViewController setBccRecipients:recipients]; | |
} | |
} | |
} | |
[mailViewController setToRecipients:toRecipients]; | |
[self presentModalViewController:mailViewController animated:YES]; | |
[mailViewController release]; | |
return NO; | |
} | |
return YES; | |
} |
You could add a direct link to your answer on SO: http://stackoverflow.com/a/4126204/377384
Changing line 30 to
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Should make it ARC friendly... Also it seems to handle HTML body data as well (at least an embedded link I have) so optionally isHTML can be turned to YES. Optimally you would check the body content for HTML and then toggle as needed... but that's more than I need.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're on Stackoverflow, vote it up: http://stackoverflow.com/questions/4125532/readymade-solution-to-parse-mailto-urls-in-cocoa