ios 解析NSURL邮件到

6yt4nkrj  于 2022-12-15  发布在  iOS
关注(0)|答案(5)|浏览(194)

如何解析mailto请求?

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

我想从这个NSURL中提取收件人、主题和正文,该怎么做?
谢谢

ezykj2lf

ezykj2lf1#

下面的代码可以解析任何URL,并返回一个字典,其中包含参数和字典中关联的对象。它也可以用于mailto URL。
请注意:此代码假定您使用的是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"];

编辑:

我更新了代码,以工作与任何网址和收件人现在在字典中的mailto网址。

41zrol4v

41zrol4v2#

我会像这样从那里提取电子邮件:

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);
fnx2tebb

fnx2tebb3#

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

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

然后您可以让接收者访问NSURLComponents的路径属性;以及queryItems属性的参数。例如,如果我们想获取主题,类似于下面这样的代码就可以完成我们的工作

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

j1dl9f464#

仅用于mailto的NSURL类别:此方法还修复了上述Fabian答案中的一个崩溃错误,即mailto:url不包含?。它也不需要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];
}
ylamdve6

ylamdve65#

- (NSDictionary *) parameterDictionaryFromURL:(NSURL *)url {
    NSMutableDictionary *parameterDictionary = [[NSMutableDictionary alloc] init];
    NSURLComponents * urlComponents = [NSURLComponents componentsWithString:url.absoluteString];
    for (NSURLQueryItem *item in urlComponents.queryItems) {
        parameterDictionary[item.name] = item.value;
    }
    if ([url.scheme isEqualToString:@"mailto"]) {
        NSUInteger questionMarkLocation = [url.resourceSpecifier rangeOfString:@"?"].location;
        if (questionMarkLocation == NSNotFound) {
            parameterDictionary[@"recipient"] = url.resourceSpecifier;
        } else {
            parameterDictionary[@"recipient"] = [url.resourceSpecifier substringToIndex:questionMarkLocation];
        }
    }
    return [parameterDictionary copy];
}

相关问题