<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>farp.blog &#187; MacOS</title>
	<atom:link href="http://fredandrandall.com/blog/tag/macos/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredandrandall.com/blog</link>
	<description>Bloggin&#039; about whatever</description>
	<lastBuildDate>Tue, 04 Nov 2014 07:15:30 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>How to launch your Mac/iOS app with a custom URL</title>
		<link>http://fredandrandall.com/blog/2011/07/30/how-to-launch-your-macios-app-with-a-custom-url/</link>
		<comments>http://fredandrandall.com/blog/2011/07/30/how-to-launch-your-macios-app-with-a-custom-url/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 23:02:09 +0000</pubDate>
		<dc:creator><![CDATA[Randall]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[custom url]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPhone SDK]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[objc]]></category>

		<guid isPermaLink="false">http://fredandrandall.com/blog/?p=473</guid>
		<description><![CDATA[One interesting feature of iOS and Mac apps is the ability for them to be launched by a custom URL. What this means is that you can set up your app to respond to different links in different ways. Clicking &#8230; <a href="http://fredandrandall.com/blog/2011/07/30/how-to-launch-your-macios-app-with-a-custom-url/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://fredandrandall.com/blog/wp-content/uploads/2011/07/http.jpg"><img class="alignleft size-medium wp-image-488" title="http" src="http://fredandrandall.com/blog/wp-content/uploads/2011/07/http-300x197.jpg" alt="" width="210" height="138" /></a>One interesting feature of iOS and Mac apps is the ability for them to be launched by a custom URL. What this means is that you can set up your app to respond to different links in different ways.</p>
<p>Clicking a link that says myTwitterApp://sendTweet?tweet=hello could send a tweet from your twitter app. It could also be used for configuring your app. Maybe you have an email app that needs some special server configurations. It could be set up so that you just have to visit a webpage on your server and click a link and have it automatically configure your email settings.</p>
<p>Facebook uses the URL scheme for authentication in their iPhone app. If I have an app that wants to authenticate with Facebook, it will try and talk to the Facebook app on the phone so I don&#8217;t have to authenticate again in my other app.</p>
<p>I think this is a pretty underused feature and I would bet there are TONS of clever things people can do with it. So how do you actually do it?<span id="more-473"></span></p>
<p>I&#8217;m going to show you how I&#8217;m adding it to the next versions of Thoughtback.</p>
<p>Your first step is adding a URL type to your app&#8217;s info.plist. You will add the same key if you&#8217;re making an iOS or Mac app. The raw key is CFBundleURLTypes.</p>
<p><a href="http://fredandrandall.com/blog/wp-content/uploads/2011/07/urltype.png"><img class="aligncenter size-medium wp-image-474" title="urltype" src="http://fredandrandall.com/blog/wp-content/uploads/2011/07/urltype-300x66.png" alt="" width="300" height="66" /></a></p>
<p>Once the key is added links that start with thoughtback:// will launch the apps on both iOS and OS X.</p>
<p>Just launching from a URL is fine, but how do you get any information out of the URL that was clicked? That&#8217;s where the process differs between the two platforms.</p>
<p><strong>iOS</strong><br />
Go into your applicationdelegate and override either of these two methods.</p>
<pre>- (BOOL)application:(UIApplication *)application 
           handleOpenURL:(NSURL *)url

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
  sourceApplication:(NSString*)sourceApplication 
         annotation:(id)annotation</pre>
<p><em></em>I should mention that the first method is deprecated, but the second one is only available on iOS 4.2 or later.</p>
<p><strong>Mac OS</strong><br />
This is slightly different on the mac, URL handling is not part of the NSApplicationDelegate protocol.</p>
<p>What you&#8217;ll need to do is tell the application to respond to an Apple Event.</p>
<pre class="brush: objc; title: ; notranslate">
-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self
                           andSelector:@selector(handleGetURLEvent:withReplyEvent:)
                         forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
}
</pre>
<p><strong>Parsing the URL</strong><br />
So now that you have the URL that launched your app, you&#8217;ll want to get the information out of the string. The most typical way to send information is with a query string. Here&#8217;s an example that Thoughtback will use.</p>
<p>thoughtback://send?thought=Interesting Thought&amp;redirect=http://fredandrandall.com</p>
<p>We want to pull out a few different things from that. First we want to grab &#8220;send&#8221; because that&#8217;s the action thoughtback will take. Then we want to get the thought that will be sent and the url that Thoughtback will redirect to when it&#8217;s done sending the thought.</p>
<p>To parse out &#8220;send&#8221; we can use NSURL&#8217;s host method.</p>
<p>To get the query string, we can use NSURL&#8217;s query method.</p>
<p>Parsing the query string is a little more difficult but this block of code, that I found on <a href="http://stackoverflow.com/questions/6591280/how-to-parse-an-asset-url-in-objective-c">StackOverflow</a>, will put it into an NSDictionary for you.</p>
<pre class="brush: objc; title: ; notranslate">
NSString *query = [url query];
NSArray *queryPairs = [query componentsSeparatedByString:@&quot;&amp;&quot;];
NSMutableDictionary *pairs = [NSMutableDictionary dictionary];
for (NSString *queryPair in queryPairs) {
  NSArray *bits = [queryPair componentsSeparatedByString:@&quot;=&quot;];
  if ([bits count] != 2) { continue; }

  NSString *key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSString *value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

  [pairs setObject:value forKey:key];
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fredandrandall.com/blog/2011/07/30/how-to-launch-your-macios-app-with-a-custom-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
