Skip to content

Instantly share code, notes, and snippets.

@futuretap
Created November 8, 2010 17:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save futuretap/667952 to your computer and use it in GitHub Desktop.
Save futuretap/667952 to your computer and use it in GitHub Desktop.
Intercept mailto URLs in a UIWebView and send them to a MFMailComposeViewController
- (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;
}
@futuretap
Copy link
Author

@lindon-fox
Copy link

You could add a direct link to your answer on SO: http://stackoverflow.com/a/4126204/377384

@ima747
Copy link

ima747 commented May 1, 2013

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