Sunday, January 24, 2016

UIAlertview issues in iOS 8 and above


Welcome all once again after long month back.

Last two day's back i had faced one issue in one of my project by using Alertview in iOS 9. The issue is External keyboard or sometime Default keyboard not worked properly . If i typed in the textfield, textfield didn't display any value in it. After a long try i found the solution because of UIAlertview causing the problem. So here is the fix,

if your app support lower then iOS 8 then we need to Whether the iOS Version is lower then 8.0 or UIAlertController class object is present either way we can use it.

Objective-C:-

if (([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending)) { 
     // use UIAlertView
}
else { 
    // use UIAlertController 
}


//or

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



Swift :-

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

//or

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



Base on the condition display UIAlertview or UIAlertController for it.

Note: 
1) Don't try and work out this based on the OS version. You need to detect abilities not OS.(Refer here.)

2) While using UIAlertController need Viewcontroller to present it, So we can't able to present UIAlertController in viewDidLoad. You will get an warning 'Warning: Attempt to present <UIAlertController: 0x13550e280> on <ViewController: 0x13560a470> whose view is not in the window hierarchy!'. So be aware of it also.

No comments:

Post a Comment