0
let alertControllerLoad = UIAlertController()
let alertViewLoad = UIAlertView()

These are both established as global variables in my inbox class. I check if the device is iOS8 or above before I call UIAlertController but since it creates an instance of the UIAlertController in the class it crashes on ios7 devices. I need them to be global variables since I call them from multiple functions. Is there a work around using init() or is this something that doesnt have a work around.

2 Answers2

0

Unfortunately there are no preprocessor calls in Swift, Swift could be conditionally compiled based on the evaluation of build configurations in two functions only as of today.

os() for a choice of OSX or iOS and arch() for a choice of x86_64, arm, arm64 or i386.

So you can't use the Objective-C #if/#endif conditions for checking the iOS version.

But I think you can make a trick, as you do not need to know the exact iOS version, but only if UIAlertController exists. And you can check it in the following way:

if (objc_getClass("UIAlertController") == nil) {
   // iOS 7
} else {
   // iOS 8+
}

Then you do not need to have global references to either UIAlertController or UIAlertView but instantiate them locally from inside the if and call.

teamnorge
  • 784
  • 5
  • 9
  • I don't quite think you understand my problem. Or I don't understand your solution. Where would I put your slice of code? I show() or present the alertView/Controller in the viewDidAppear and dismissWithClick in a separate function. So if its not a global variable I can't do this. –  Jul 26 '15 at 02:44
  • You can put everything in a separated singleton class as I did it in Objective-C. Let say you have only two methods `showAlertWithMessage` where you specify, `parentViewControlle`r, who instantiated, String message and optional `okHandler` and `cancelHandler`, what to call when user press button(s). The second method is `hide()`. – teamnorge Jul 26 '15 at 03:37
  • The challenge here, is that when you use UIAlertView you need someone to listed to UIAlertViewDelegate protocol, the possible workaround could be to assign your AppDelegate and specify those handlers to call. Then you do not have UIAlertView/UIAlertController code in your viewControllers at all, and could initialize them not only from any method within viewController, but from any viewController within the project. – teamnorge Jul 26 '15 at 03:43
  • Okay, thanks for the help! I'm working on the code now. Everything seems to work but you never know until you compile –  Jul 26 '15 at 04:43
  • Do you know if its possible to change the preferredStyle of alertControler.... from alert to actionSheet? UIAlertController(title: "Default AlertController", message: "", preferredStyle: UIAlertControllerStyle.Alert) –  Jul 26 '15 at 07:02
0

For anyone else that might stumble upon this,

class Alerts {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var iOS8Up: Bool = true

lazy var alertControllerLoad = Alerts.set()
let alertViewLoad = UIAlertView()


class func set() -> UIAlertController {

    //get the device model to know what image to post for refresh
    var device : UIDevice = UIDevice.currentDevice();
    var systemVersion = device.systemVersion;
    let numberFormatter = NSNumberFormatter()
    let systemVersionNumber = numberFormatter.numberFromString(systemVersion)
    let systemVersionFloatValue = systemVersionNumber!.floatValue

    if(systemVersionFloatValue < 8.0) {
        return UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
    }
    else{
        //returns with actionSheet as preferredStyle
        //will crash on anything less than iOS8
        return UIAlertController()
    }


}

}

this allows you to make a AlertController a global variable even if you're running iOS7. I spent far too long on this issue.