您如何将 Cocoa 应用程序设置为默认 Web 浏览器? [英] How do you set your Cocoa application as the default web browser?

查看:20
本文介绍了您如何将 Cocoa 应用程序设置为默认 Web 浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Cocoa 应用程序设置为默认网络浏览器?

How do you set your Cocoa application as the default web browser?

我想创建一个应用程序,当用户点击其他应用程序(邮件、iChat 等)中的 HTTP 或 HTTPS 链接时,该应用程序默认启动.

I want to create an application that is launched by default when the user clicks on an HTTP or HTTPS link in other applications (Mail, iChat etc.).

推荐答案

制作可充当默认 Web 浏览器的应用程序需要执行四个步骤.前三个步骤允许您的应用充当相关 URL 方案(HTTP 和 HTTPS)的角色处理程序,最后一步使其成为这些方案的默认角色处理程序.

There are four steps to making an app that can act as the default web browser. The first three steps allow your app to act as a role handler for the relevant URL schemes (HTTP and HTTPS) and the final step makes it the default role handler for those schemes.

1) 将您的应用可以处理的网址方案添加到您应用的 info.plist 文件中

要添加对 http://https:// 的支持,您需要将以下内容添加到应用程序的 info.plist 文件中.这告诉操作系统您的应用程序能够处理 HTTP 和 HTTP URL.

To add support for http:// and https:// you'd need to add the following to your application's info.plist file. This tells the OS that your application is capable of handling HTTP and HTTP URLs.

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>http URL</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>http</string>
        </array>
    </dict>
    <dict>
        <key>CFBundleURLName</key>
        <string>Secure http URL</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>https</string>
        </array>
    </dict>
</array>

2) 编写 URL 处理程序方法

当操作系统想要使用您的应用程序打开 URL 时,将调用此方法.将此方法添加到哪个对象并不重要,它将在下一步中显式传递给事件管理器.URL 处理程序方法应如下所示:

This method will be called by the OS when it wants to use your application to open a URL. It doesn't matter which object you add this method to, that'll be explicitly passed to the Event Manager in the next step. The URL handler method should look something like this:

- (void)getUrl:(NSAppleEventDescriptor *)event 
    withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
  // Get the URL
  NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] 
    stringValue];

  //TODO: Your custom URL handling code here
}

3) 注册 URL 处理程序方法

接下来,告诉事件管理器在想要使用您的应用加载 URL 时调用哪个对象和方法.在这里的代码中,我将 self 作为事件处理程序传递,假设我们从定义 getUrl:withReplyEvent:<的同一对象调用 setEventHandler/code> 方法.

Next, tell the event manager which object and method to call when it wants to use your app to load an URL. In the code here I'm passed self as the event handler, assuming that we're calling setEventHandler from the same object that defines the getUrl:withReplyEvent: method.

您应该将此代码添加到应用程序初始化代码的某处.

You should add this code somewhere in your application's initialisation code.

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

某些应用程序(包括 Adob​​e AIR 的早期版本)使用替代的 WWW!/OURL AppleEvent 来请求应用程序打开 URL,因此为了与这些应用程序兼容,您还应该添加以下内容:

Some applications, including early versions of Adobe AIR, use the alternative WWW!/OURL AppleEvent to request that an application opens URLs, so to be compatible with those applications you should also add the following:

[em
  setEventHandler:self 
  andSelector:@selector(getUrl:withReplyEvent:) 
  forEventClass:'WWW!' 
  andEventID:'OURL'];

4) 将您的应用设置为默认浏览器

到目前为止,我们所做的一切都告诉操作系统您的应用程序是浏览器,现在我们需要将其设为默认浏览器.

Everything we've done so far as told the OS that your application is a browser, now we need to make it the default browser.

我们必须使用 Launch Services API 来执行此操作.在本例中,我们将我们的应用设置为 HTTP 和 HTTPS 链接的默认角色处理程序:

We've got to use the Launch Services API to do this. In this case we're setting our app to be the default role handler for HTTP and HTTPS links:

CFStringRef bundleID = (CFStringRef)[[NSBundle mainBundle] bundleIdentifier];
OSStatus httpResult = LSSetDefaultHandlerForURLScheme(CFSTR("http"), bundleID);
OSStatus httpsResult = LSSetDefaultHandlerForURLScheme(CFSTR("https"), bundleID);
//TODO: Check httpResult and httpsResult for errors

(最好在更改默认浏览器之前征得用户的许可.)

(It's probably best to ask the user's permission before changing their default browser.)

自定义网址架构

值得注意的是,您还可以使用这些相同的步骤来处理您自己的自定义 URL 方案.如果您要创建自定义 URL 方案,最好将其基于您的应用程序包标识符以避免与其他应用程序发生冲突.因此,如果您的包 ID 是 com.example.MyApp,您应该考虑使用 x-com-example-myapp:// URL.

It's worth noting that you can also use these same steps to handle your own custom URL schemes. If you're creating a custom URL scheme it's a good idea to base it on your app's bundle identifier to avoid clashes with other apps. So if your bundle ID is com.example.MyApp you should consider using x-com-example-myapp:// URLs.

这篇关于您如何将 Cocoa 应用程序设置为默认 Web 浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆