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

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

问题描述

如何将您的Cocoa应用程序设置为默认的Web浏览器?



我想创建一个默认情况下启动的应用程序,当用户点击HTTP或其他应用程序中的HTTPS链接(Mail,iChat等)。

解决方案

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



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



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

 < 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>安全http网址< / string>
< key> CFBundleURLSchemes< / key>
< array>
< string> https< / string>
< / array>
< / dict>
< / array>

2)编写网址处理程序方法



当希望使用应用程序打开URL时,操作系统将调用此方法。添加此方法的对象无关紧要,将在下一步中明确传递给事件管理器。 URL处理程序方法应该看起来像这样:

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

// TODO:此处的自定义URL处理代码
}

3)注册网址处理程序方法



接下来,告诉事件管理器当要使用应用程序时调用哪个对象和方法加载URL。在这里的代码中,我通过 self 作为事件处理程序,假设我们调用 setEventHandler 定义 getUrl:withReplyEvent:方法的对象。



您应该在应用程序的初始化代码中添加这个代码。

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

某些应用程序,包括Adobe AIR的早期版本,使用备用WWW!/ OURL AppleEvent请求应用程序打开URL,为了与这些应用程序兼容,您还应该添加以下内容:

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

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

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



我们必须使用Launch Services API才能执行此操作。在这种情况下,我们将应用设置为HTTP和HTTPS链接的默认角色处理程序:

  CFStringRef bundleID =(CFStringRef )[[NSBundle mainBundle] bundleIdentifier]; 
OSStatus httpResult = LSSetDefaultHandlerForURLScheme(CFSTR(http),bundleID);
OSStatus httpsResult = LSSetDefaultHandlerForURLScheme(CFSTR(https),bundleID);
// TODO:检查httpResult和httpsResult是否有错误



自定义网址方案



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


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

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.).

解决方案

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) Add the URL schemes your app can handle to your application's info.plist file

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) Write an URL handler method

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) Register the URL handler method

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];

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) Set your app as the default browser

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.

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.)

Custom URL schemes

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天全站免登陆