0

I have heavily integrated Speechkit into one of my app's view controllers. Speechkit is only available on iOS 10, but I also need my app to run on iOS 9 devices.

Right now, my app crashes on launch on iOS 9 devices; how can I prevent Speechkit from crashing iOS versions 9 and earlier? Can I create two separate view controller files, or do I have to put if #available(iOS 10, *) { around every single Speechkit reference?

Edit: What can I do instead of this?

import Speech
class ViewController2: UIViewController, SFSpeechRecognizerDelegate {

if #available(iOS 9, *) { // ERROR: Expected Declaration
private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
}

func doSomeStuffWithSpeech() {
...
}

...

}
owlswipe
  • 19,159
  • 9
  • 37
  • 82

1 Answers1

3

I have heavily integrated Speechkit

If that's the case, I think creating two separated viewControllers might be easier -or more logical-, you can decide which one should viewed based on the #available(iOS 10.0, *)

Let's assume that you will present the ViewController2 based on tapping a button in another ViewController (In the code snippet, I called it PreviousViewController):

class PreviousViewController: UIViewController {
    //...

    @IBAction func presentApproriateScene(sender: AnyObject) {
        if #available(iOS 10.0, *) {
            // present the ViewController that heavily integrated with Speechkit
            // maybe by perfroming a segue:
            performSegueWithIdentifier("segue01", sender: self)

            // or maybe by getting the it from the storyboard
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc1 = storyboard.instantiateViewControllerWithIdentifier("vc1")
            presentViewController(vc1, animated: true, completion: nil)

        } else {
            // present the ViewController that does not suupport Speechkit
            // maybe by perfroming a segue:
            performSegueWithIdentifier("segue02", sender: self)

            // or maybe by getting the it from the storyboard
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc2 = storyboard.instantiateViewControllerWithIdentifier("vc2")
            presentViewController(vc2, animated: true, completion: nil)
        }
    }

    //...
}

Also, you can use it when declaring variables:

class ViewController: UIViewController {
    //...

    if #available(iOS 10.0, *) {
        private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
    } else {
        // ...
    }

    //...
}

But again, as you mentioned, if you have "heavy" integration with Speechkit, I assume that making two Viewcontrollers would be more logical.

Hope this helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • So just coming back to this after a while—how can I do this if I have IBOutlets in the Speechkit view controller (tied to the storyboard), and if this is the main view controller, not one that is segued to from another? – owlswipe Dec 25 '16 at 22:45
  • I didn't got it :) do you mean that it is the initial ViewController? – Ahmad F Dec 26 '16 at 07:01
  • yep, initial view controller – owlswipe Dec 26 '16 at 14:50
  • well, simply you do the same behavior to determine what viewController should be the root :) – Ahmad F Dec 26 '16 at 14:54
  • And how can I do this with IBOutlets tied to the storyboard? Do I need to re-hookup every IBOutlet in each view controller to the storyboard? – owlswipe Dec 26 '16 at 15:38
  • I think you got it wrong... you should have two separated ViewControllers and also two viewControllers in the storyboard, they should be very similar (if not identical) and each one of them is connected to one of the UIViewControllers. From storyboard scene (ViewController) you can assign only 1 viewcontroller to it, but the opposite is not right, i.e the UIViewcontroller can be assigned to more than one scene at storyboard. – Ahmad F Dec 26 '16 at 15:47
  • Ah I see! Thanks for your help :D – owlswipe Dec 26 '16 at 15:55
  • Glad to help :) – Ahmad F Dec 26 '16 at 16:51