title: iOS app will not start from custom URL: locks up Phone
body: <p>See <strong>Update</strong> at the bottom.</p>
  
  <p>I have an iOS app written in Swift with Xcode 6.2. It defines a custom URL scheme in info.plist, and, when the app is running in the background, I can successfully open the app from within Safari, touching a link for my custom scheme. It enters here:</p>
  
  <pre><code>func application(application: UIApplication, openURL url2: NSURL?, sourceApplication: String?, annotation: AnyObject?) -&gt; Bool { ... }
  </code></pre>
  
  <p>and all that works.</p>
  
  <p>To try the same thing when the app is not running in the background, I double-clicked <code>home</code> and swiped up to remove my app from the list of running apps. When the app is <em>not</em> running in the background, when I touch the same link in Safari, the phone locks up. In iOS8, the screen shows Safari, but stops responding to touches. In iOS7, the screen animates to show the launch screen of my app, and then the phone locks up, not responding to touches. In either OS, the <code>home</code> and <code>power</code> buttons don't work unless I hard reset the phone. But, in either OS, if I hit <code>run</code> on Xcode on my desktop to launch a new instance of my app (the phone is connected with USB) then instantly the phone springs back to life and works normally.</p>
  
  <p>At a loss to understand this, I put in a logger method that writes a log to <code>NSUserDefaults.standardUserDefaults()</code>. I can see that nonvolatile log next time I run the app. I call that logger as the first statement in the function above and also in </p>
  
  <pre><code>func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: AnyObject?) -&gt; Bool { ... }
  </code></pre>
  
  <p>and when the phone locks up neither of those routines has ever been called.</p>
  
  <p>I never have any problems launching the app from Xcode or just as a normal app - it's in testflight beta on the app store now. Only when launching from a custom URL, and then only when the app is not running in the background. I looked at <a href="https://stackoverflow.com/questions/27517945/app-crashes-when-not-backgrounded-and-opened-using-url-scheme?rq=1">App crashes when not backgrounded and opened using URL Scheme</a> and <a href="https://stackoverflow.com/questions/26178324/application-openurl-in-swift/26194372#26194372">application openURL in Swift</a> and <a href="https://stackoverflow.com/questions/17929759/custom-url-scheme-for-ios-app-when-not-backgrounding?rq=1">Custom URL Scheme for iOS App when not backgrounding</a> but my app doesn't crash, it doesn't even start. And how can it lock up the phone?</p>
  
  <p>I tried using the technique suggested at <a href="https://stackoverflow.com/questions/24020000/subclass-uiapplication-with-swift">Subclass UIApplication with Swift</a> to execute custom code at program start in a file called <code>main.swift</code>:</p>
  
  <pre><code>import Foundation
  import UIKit
  Globals.logNV("About to run UIApplicationMain") // log to NSUserDefaults
  UIApplicationMain(C_ARGC, C_ARGV, NSStringFromClass(EWApplication), NSStringFromClass(AppDelegate)) // Subclass UIApplication
  </code></pre>
  
  <p>and on normal program launch that works fine, I get the message into my nonvolatile log. But when the phone locks up, when I try to launch from a URL with the app not running, then not even that message gets into the log.</p>
  
  <p>I wondered if this might be a bug in how iOS handles <code>Info.plist</code>, so I changed the order of things there. Magically, the program now worked perfectly! But this is not repeatable. I can make a (theoretically harmless) change to <code>Info.plist</code> that causes the program not to run from a link, then unmake the change so the file is exactly as it was before, and still have the program not run. Something in the build causes iOS to lock up trying to launch the program, but it doesn't seem to be something I can control. I tried changing <code>CFBundleURLName</code> to something unrelated, and I used a different custom URL in <code>CFBundleURLSchemes</code>. No joy, still most builds fail. In all my tests, for one build of a program, the results are the same on iOS7/iPhone4 and iOS8/iPhone5.</p>
  
  <ul>
  <li>How can I find out what iOS is doing? </li>
  <li>What can I have done to my app to make it lock up the phone?</li>
  <li>Any suggestions as to how to debug this?</li>
  <li>Thanks!</li>
  </ul>
  
  <p><strong>Update</strong> </p>
  
  <p>After <em>much</em> fooling about, it seems this may be an artefact of downloading the app direct from Xcode to the phone. If Xcode loads the app onto the phone, then, whether or not Xcode is actually connected to the phone, some app builds lock the phone up at launch from an external URL when the app is not already running. If the identical source code is built and sent to iTunes for a beta test and downloaded onto the phone with Testflight, then the bug doesn't appear. I have two different builds that behave like this now. I'm not sure this is universal but it doesn't seem implausible at the moment.</p>