解析NSURL mailto

时间:2011-09-05 16:04:15

标签: iphone ios parsing

如何解析mailto请求?

'mailto:someone@example.com?cc=someone_else@example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body'

从这个NSURL,我想提取收件人,主题和身体。我该怎么办?

由于

4 个答案:

答案 0 :(得分:10)

这是一些代码,它将解析任何URL并返回带有参数和字典中相关对象的字典。它也适用于mailto个网址 请注意:此代码假定您使用的是 ARC

@interface NSString (URLDecoding)  

- (NSString *) URLDecodedString;

@end

@implementation NSString (URLDecoding)

- (NSString *) URLDecodedString {
    NSString *result = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (__bridge CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
    return result;
}

@end

- (NSDictionary *) parameterDictionaryFromURL:(NSURL *)url {
    NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
    if ([[url scheme] isEqualToString:@"mailto"]) {
        NSString *mailtoParameterString = [[url absoluteString] substringFromIndex:[@"mailto:" length]];
        NSUInteger questionMarkLocation = [mailtoParameterString rangeOfString:@"?"].location;
        [parameterDictionary setObject:[mailtoParameterString substringToIndex:questionMarkLocation] forKey:@"recipient"];

        if (questionMarkLocation != NSNotFound) {
            NSString *parameterString = [mailtoParameterString substringFromIndex:questionMarkLocation + 1];
            NSArray *keyValuePairs = [parameterString componentsSeparatedByString:@"&"];
            for (NSString *queryString in keyValuePairs) {
                NSArray *keyValuePair = [queryString componentsSeparatedByString:@"="];
                if (keyValuePair.count == 2)
                    [parameterDictionary setObject:[[keyValuePair objectAtIndex:1] URLDecodedString] forKey:[[keyValuePair objectAtIndex:0] URLDecodedString]];
            }
        }
    }
    else {
        NSString *parameterString = [url parameterString];
        NSArray *keyValuePairs = [parameterString componentsSeparatedByString:@"&"];
        for (NSString *queryString in keyValuePairs) {
            NSArray *keyValuePair = [queryString componentsSeparatedByString:@"="];
            if (keyValuePair.count == 2)
                [parameterDictionary setObject:[[keyValuePair objectAtIndex:1] URLDecodedString] forKey:[[keyValuePair objectAtIndex:0] URLDecodedString]];
        }
    }

    return [parameterDictionary copy];
}

以下是您使用它的方式:

NSURL *mailtoURL = [NSURL URLWithString:@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
NSDictionary *parameterDictionary = [self parameterDictionaryFromURL:mailtoURL];

NSString *recipient = [parameterDictionary objectForKey:@"recipient"];
NSString *subject = [parameterDictionary objectForKey:@"subject"];
NSString *body = [parameterDictionary objectForKey:@"body"];

修改
我更新了代码以使用任何URL,收件人现在位于mailto网址的字典中。

答案 1 :(得分:2)

我会从这样的电子邮件中提取电子邮件:

NSString * mailToString = @"'mailto:someone@example.com?cc=someone_else@example.com&subject=This%20is%20the%20subject&body=This%20is%20the%20body'";

NSArray *tempArray = [mailToString componentsSeparatedByString:@"?"];

//get email address from array
NSString * emailString = [[tempArray objectAtIndex:0]description];

//clean up string
emailString = [emailString stringByReplacingOccurrencesOfString:@"'mailto:" withString:@""]; 

//and here is your email string
NSLog(@"%@",emailString);

答案 2 :(得分:1)

从iOS 7开始,使用NSURLComponents很容易实现。您可以使用以下命令创建该对象:

if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL:false) { ...

然后你可以让收件人访问NSURLComponents的path属性;和queryItems属性的参数。例如,如果我们想要获得主题,那么这样的事情就可以完成我们的工作

let queryItems = components.queryItems as? [NSURLQueryItem]
let subject = queryItems?.filter({$0.name == "subject"}).first?.value

答案 3 :(得分:0)

仅适用于mailto的NSURL类别:当mailto:url不包含?时,此方法还修复了Fabian上面的答案中的崩溃错误。它也不需要URLDecodedString类别方法。

@implementation NSURL (Additions)

- (NSDictionary *) parameterDictionaryForMailTo {
    NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
    NSString *mailtoParameterString = [[self absoluteString] substringFromIndex:[@"mailto:" length]];
    NSUInteger questionMarkLocation = [mailtoParameterString rangeOfString:@"?"].location;
    if (questionMarkLocation != NSNotFound) {
        [parameterDictionary setObject:[mailtoParameterString substringToIndex:questionMarkLocation] forKey:@"recipient"];
        NSString *parameterString = [mailtoParameterString substringFromIndex:questionMarkLocation + 1];
        NSArray *keyValuePairs = [parameterString componentsSeparatedByString:@"&"];
        for (NSString *queryString in keyValuePairs) {
            NSArray *keyValuePair = [queryString componentsSeparatedByString:@"="];
            if (keyValuePair.count == 2)
                [parameterDictionary setObject:[[keyValuePair objectAtIndex:1] stringByRemovingPercentEncoding] forKey:[[keyValuePair objectAtIndex:0] stringByRemovingPercentEncoding]];
        }
    }
    else {
        [parameterDictionary setObject:mailtoParameterString forKey:@"recipient"];
    }

    return [parameterDictionary copy];
}