简体   繁体   English

(OSX)用户单击浏览器中的电子邮件链接时,操作系统如何将电子邮件地址传送到默认邮件应用程序?

[英](OSX) When user clicks an email link in browser, how does the OS convey the e-mail address to the default mail app?

My dad is annoyed that when he clicks an e-mail hyperlink, OSX tries to open Mail, which he has never used and doesn't want to learn. 我父亲很生气,当他单击电子邮件超链接时,OSX试图打开Mail,他从未使用过,也不想学习。 He'd like it to just open a browser window to his ISP-provided email service (bleh). 他希望它只是打开一个浏览器窗口,打开其ISP提供的电子邮件服务(正常)。

I'm trying to write a program that basically handles that interaction, but I cannot figure out how the e-mail address from the hyperlink is passed along to the application. 我正在尝试编写一个基本上处理该交互的程序,但是我无法弄清楚超链接中的电子邮件地址是如何传递到应用程序的。 Essentially... When I click a mailto hyperlink for foo@example.com, How does the OS tell Mail.app (or whatever the default client is) to compose a new e-mail to foo@example.com? 本质上...当我单击foo@example.com的mailto超链接时,操作系统如何告诉Mail.app(或任何默认客户端)将新的电子邮件撰写为foo@example.com?

First, Mail.app passively tells Launch Services that it handles the "mailto" URL scheme by including the following in its Info.plist file: 首先,Mail.app通过在Info.plist文件中包含以下内容,被动地告诉Launch Services它处理“ mailto” URL方案:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Email Address URL</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>mailto</string>
        </array>
        <key>LSIsAppleDefaultForScheme</key>
        <true/>
    </dict>
    ...
</array>

Next, it sets up a handler for the kInternetEventClass / kAEGetURL Apple Event. 接下来,它为kInternetEventClass / kAEGetURL Apple Event设置处理程序。 In Cocoa, that looks something like: 在可可粉中,它类似于:

NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];

You could implement the handler method like this: 您可以像这样实现处理程序方法:

- (BOOL)handleGetURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
    NSAppleEventDescriptor* directObjectDescriptor  = [event paramDescriptorForKeyword:keyDirectObject];
    NSString*               urlString               = [directObjectDescriptor stringValue];
    NSURL*                  url                     = [NSURL URLWithString:urlString];
    // ... do something with url ...
}

For a "mailto" URL, much of the parsing behavior of NSURL isn't effective because the URLs don't conform to RFC 1808 (the resource specifier doesn't start with "//"). 对于“ mailto” URL, NSURL许多解析行为都是无效的,因为这些URL不符合RFC 1808(资源说明符不是以“ //”开头)。 You can really only get the scheme (mailto) and the resource specifier using the -resourceSpecifier method. 您实际上只能使用-resourceSpecifier方法获得方案(mailto)和资源说明符。 Some mailto URLs might have query-like syntax, such as "?subject=Some%20subject%20text", on them, but NSURL won't help you pick that apart. 某些mailto URL上可能具有类似查询的语法,例如“?subject = Some%20subject%20text”,但NSURL不能帮您区分。 So, you'll need to do that manually. 因此,您需要手动执行此操作。 (You might consider constructing a bogus URL by injecting "//" at the front of the resource specifier and having NSURL parse that.) (您可以考虑通过在资源说明符的前面插入“ //”并使用NSURL解析伪造的URL。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM