<p>How do you set your Cocoa application as the default web browser?</p>

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

[{'answer_id': 49512, 'author': 'georgebrock', 'author_id': 5168, 'author_profile': 'https://Stackoverflow.com/users/5168', 'pm_score': 7, 'selected': True, 'text': '<p>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.</p>\n\n<p><strong>1) Add the URL schemes your app can handle to your application\'s info.plist file</strong></p>\n\n<p>To add support for <code>http://</code> and <code>https://</code> 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.</p>\n\n<pre class="lang-xml prettyprint-override"><code>&lt;key&gt;CFBundleURLTypes&lt;/key&gt;\n&lt;array&gt;\n    &lt;dict&gt;\n        &lt;key&gt;CFBundleURLName&lt;/key&gt;\n        &lt;string&gt;http URL&lt;/string&gt;\n        &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;\n        &lt;array&gt;\n            &lt;string&gt;http&lt;/string&gt;\n        &lt;/array&gt;\n    &lt;/dict&gt;\n    &lt;dict&gt;\n        &lt;key&gt;CFBundleURLName&lt;/key&gt;\n        &lt;string&gt;Secure http URL&lt;/string&gt;\n        &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;\n        &lt;array&gt;\n            &lt;string&gt;https&lt;/string&gt;\n        &lt;/array&gt;\n    &lt;/dict&gt;\n&lt;/array&gt;\n</code></pre>\n\n<p><strong>2) Write an URL handler method</strong></p>\n\n<p>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:</p>\n\n<pre><code>- (void)getUrl:(NSAppleEventDescriptor *)event \n    withReplyEvent:(NSAppleEventDescriptor *)replyEvent\n{\n  // Get the URL\n  NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] \n    stringValue];\n\n  //TODO: Your custom URL handling code here\n}\n</code></pre>\n\n<p><strong>3) Register the URL handler method</strong></p>\n\n<p>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 <code>self</code> as the event handler, assuming that we\'re calling <code>setEventHandler</code> from the same object that defines the <code>getUrl:withReplyEvent:</code> method.</p>\n\n<p>You should add this code somewhere in your application\'s initialisation code.</p>\n\n<pre><code>NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];\n[em \n  setEventHandler:self \n  andSelector:@selector(getUrl:withReplyEvent:) \n  forEventClass:kInternetEventClass \n  andEventID:kAEGetURL];\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>[em\n  setEventHandler:self \n  andSelector:@selector(getUrl:withReplyEvent:) \n  forEventClass:\'WWW!\' \n  andEventID:\'OURL\'];\n</code></pre>\n\n<p><strong>4) Set your app as the default browser</strong></p>\n\n<p>Everything we\'ve done so far as told the OS that your application is <em>a browser</em>, now we need to make it <em>the default browser</em>.</p>\n\n<p>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:</p>\n\n<pre><code>CFStringRef bundleID = (CFStringRef)[[NSBundle mainBundle] bundleIdentifier];\nOSStatus httpResult = LSSetDefaultHandlerForURLScheme(CFSTR("http"), bundleID);\nOSStatus httpsResult = LSSetDefaultHandlerForURLScheme(CFSTR("https"), bundleID);\n//TODO: Check httpResult and httpsResult for errors\n</code></pre>\n\n<p>(It\'s probably best to ask the user\'s permission before changing their default browser.)</p>\n\n<p><strong>Custom URL schemes</strong></p>\n\n<p>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 <code>com.example.MyApp</code> you should consider using <code>x-com-example-myapp://</code> URLs.</p>\n'}, {'answer_id': 111572, 'author': 'Raphael Schweikert', 'author_id': 11940, 'author_profile': 'https://Stackoverflow.com/users/11940', 'pm_score': 2, 'selected': False, 'text': '<p>If you just want to change the default helper app for http(s), you can do so in the Safari preferences. There you’ll find a drop down which will let you select all the registered handler applications for http. To automatically have the app set itself as the default browser see the previous instructions.</p>\n'}, {'answer_id': 65479328, 'author': 'vauxhall', 'author_id': 4691224, 'author_profile': 'https://Stackoverflow.com/users/4691224', 'pm_score': 0, 'selected': False, 'text': '<p>In order to appear as an option on <code>System Preferences &gt; General &gt; Default web browser</code> (at least for macOS 11) you need to add the document types for <strong>HTML</strong> and <strong>XHTML</strong> to the <em>Info.plist</em> (after the 4 steps already described on the <a href="https://stackoverflow.com/a/49512/4691224">accepted answer</a>), like this:</p>\n<pre><code>&lt;key&gt;CFBundleDocumentTypes&lt;/key&gt;\n&lt;array&gt;\n    &lt;dict&gt;\n        &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n        &lt;string&gt;HTML document&lt;/string&gt;\n        &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n        &lt;string&gt;Viewer&lt;/string&gt;\n        &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n        &lt;array&gt;\n            &lt;string&gt;public.html&lt;/string&gt;\n        &lt;/array&gt;\n    &lt;/dict&gt;\n    &lt;dict&gt;\n        &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n        &lt;string&gt;XHTML document&lt;/string&gt;\n        &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n        &lt;string&gt;Viewer&lt;/string&gt;\n        &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n        &lt;array&gt;\n            &lt;string&gt;public.xhtml&lt;/string&gt;\n        &lt;/array&gt;\n    &lt;/dict&gt;\n&lt;/array&gt;\n</code></pre>\n'}, {'answer_id': 66104677, 'author': 'Donald Timberlake', 'author_id': 15168824, 'author_profile': 'https://Stackoverflow.com/users/15168824', 'pm_score': 2, 'selected': False, 'text': '<h1>macOS Big Sur and Up</h1>\n<p>Copy and paste this code into your info.plist</p>\n<pre><code>&lt;key&gt;CFBundleURLTypes&lt;/key&gt;\n    &lt;array&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleURLName&lt;/key&gt;\n            &lt;string&gt;Web site URL&lt;/string&gt;\n            &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;http&lt;/string&gt;\n                &lt;string&gt;https&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleURLName&lt;/key&gt;\n            &lt;string&gt;http URL&lt;/string&gt;\n            &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;http&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleURLName&lt;/key&gt;\n            &lt;string&gt;Secure http URL&lt;/string&gt;\n            &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;https&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;HTML document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.html&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;XHTML document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.xhtml&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n    &lt;/array&gt;\n    &lt;key&gt;CFBundleDocumentTypes&lt;/key&gt;\n    &lt;array&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;GIF image&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;com.compuserve.gif&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;HTML document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.html&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;XHTML document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.xhtml&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;JavaScript script&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;com.netscape.javascript-\u200bsource&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;JPEG image&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.jpeg&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;MHTML document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.ietf.mhtml&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;HTML5 Audio (Ogg)&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.xiph.ogg-audio&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;HTML5 Video (Ogg)&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.xiph.ogv&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;PNG image&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.png&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;SVG document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.svg-image&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;Plain text document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;public.text&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;HTML5 Video (WebM)&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.webmproject.webm&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;WebP image&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.webmproject.webp&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;org.chromium.extension&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n        &lt;dict&gt;\n            &lt;key&gt;CFBundleTypeIconFile&lt;/key&gt;\n            &lt;string&gt;document.icns&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeName&lt;/key&gt;\n            &lt;string&gt;PDF Document&lt;/string&gt;\n            &lt;key&gt;CFBundleTypeRole&lt;/key&gt;\n            &lt;string&gt;Viewer&lt;/string&gt;\n            &lt;key&gt;LSItemContentTypes&lt;/key&gt;\n            &lt;array&gt;\n                &lt;string&gt;com.adobe.pdf&lt;/string&gt;\n            &lt;/array&gt;\n        &lt;/dict&gt;\n    &lt;/array&gt;\n</code></pre>\n<p><em><strong>Your app will be shown in the system preferences and will be default browser</strong></em></p>\n<h3>Make sure you do this</h3>\n<pre><code>    func application(_ application: NSApplication, open urls: [URL]) {\n// do a for loop, I recommend it\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/49510', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5168/']