UIAlertController if iOS 8, otherwise UIAlertView

后端 未结 11 2007
清酒与你
清酒与你 2020-12-16 10:19

I want to conform to the UIAlertController used in iOS 8 since UIAlertView is now deprecated. Is there a way that I can use this without breaking support for iOS 7? Is there

相关标签:
11条回答
  • 2020-12-16 10:53
    // Above ios 8.0
    float os_version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (os_version >= 8.000000)
    {
          //Use UIAlertController    
    }
    else
    {
         //UIAlertView
    }
    
    0 讨论(0)
  • 2020-12-16 10:53

    Create simple utility function to reduce code

    CODE :

    // pass minimum required iOS version
    BOOL isOSSupported(NSString *minRequiredVersion)
    {
        NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
        BOOL isOSSupported = ([currSysVer compare:minRequiredVersion options:NSNumericSearch] != NSOrderedAscending) && 
                                      ![currSysVer isEqualToString:@"Unknown"];
        return isOSSupported;
    }
    


    USE :

    if(isOSSupported("8.0")
    {
    // Code for iOS8 and above
    }
    else
    {
    // Code for iOS7 and below
    }
    



    Or Use system constant NSFoundationVersionNumber_iOS_7_1 as below

    if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1)
    {
       // Code for iOS8 and above
    }
    else
    {
       // Code for iOS7 and below
    }
    


    for more options Link

    0 讨论(0)
  • 2020-12-16 10:55

    Objective C (as mentioned above)

    if ([UIAlertController class]) {
        // use UIAlertController
    
    } else {
        // use UIAlertView
    
    }
    

    Swift

    if objc_getClass("UIAlertController") == nil  {
           // use UIAlertView 
    
    } else {
      // use UIAlertController
    
    }
    

    Don't use if NSClassFromString("UIAlertController") == nil It is not working because the signature for this method is func NSClassFromString(_ aClassName: String!) -> AnyClass!

    0 讨论(0)
  • 2020-12-16 10:57

    Try below code. It works fine for both iOS 8 and below version.

    if (IS_OS_8_OR_LATER) {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                 actionWithTitle:@"OK"
                                 style:UIAlertActionStyleCancel
                                 handler:^(UIAlertAction *action)
                                 {
    
                                 }];
    [alertVC addAction:cancelAction];
    
    [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
    
    }];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    0 讨论(0)
  • 2020-12-16 10:58

    I have written one class that wrap the UIAlertView and use UIAlertController. For the programmer is transparently hence is sufficient import this classes in the project. The utility of this classes is when in a old project there are more UIAlertView to change. Link: https://github.com/kennymuse/UIAlertView

    0 讨论(0)
提交回复
热议问题