After iOS 8, can I continue to use UIActionSheet and UIAlertView? - ios

I have apps which use UIActionSheet and UIAlertView.
In iOS8, Apple's documentation and some websites say that they are deprecated in iOS8.
UIActionSheet documentation
Important: UIActionSheet is deprecated in iOS 8. (Note that
UIActionSheetDelegate is also deprecated.) To create and manage action
sheets in iOS 8 and later, instead use UIAlertController with a
preferredStyle of UIAlertControllerStyleActionSheet.
But in Xcode 6 with deployment target 8.0 does not generate warning for the use of UIActionSheet and UIAlertView.
Normally Xcode generates warning for deprecated API.
Why doesn't Xcode generate warnings for UIActionSheet and UIAlertView?
Does this mean Apple does not actually deprecate those classes?
It is very dangerous if they actually deprecated them and Xcode does not generate warnings.
In another apple's documentation "What's New in iOS 8.0" says:
The new UIAlertController class replaces the UIActionSheet and UIAlertViewclasses as the preferred way to display alerts in your app.
https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
And UIActionSheet and UIAlertView is not listed on Deprecated APIs section of the documentation of the above URL.
So, this documentation means UIAlertController is preferable but UIActionSheet and UIAlertView are also still available in iOS 8.
Should I replace UIActionSheet and UIAlertView in my apps with new UIAlertController class?
Or can I continue to use UIActionSheet and UIAlertView?

It's always best to not use depreciated code, it all adds up for well written code.
So yeah, use UIAlertController.

Yes you should. Preferable leads to deprecated which leads to being cut off suddenly.

Yes, you should replace your code.
When I tried to use in my code some of the functions and delegate methods for both of the Classes UIActionSheet and UIAlertView were not working.
I was getting issues and weird results each time.
Therefore, you should not use deprecated APIs.
I am sure about this, I think if the app is uploaded to App Store with deprecated APIs then that App can be rejected.

It takes a while to figure out the new method, so you might find this code useful. I’m supporting older versions of iOS so I use a conditional to decide which to use. This code runs in my app delegate. If you are running it in a view controller, replace
[self.window.rootViewController presentViewController:alert animated:true completion:nil];
with
[self presentViewController:alert animated:YES completion:nil];
The #define is in my .pch but I put it here since it is what I use in the conditional.
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
- (void)applicationWillEnterForeground:(UIApplication *)application {
if (self.window.rootViewController) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")) {
[self displayUIAlertController];
} else {
[self displayUIAlertView];
}
}
}
- (void)displayUIAlertController {
NSString *alertMessage = [NSString stringWithFormat:#"Do you want to resume playing %# or start a new session?", GAME_NAME_TITLE];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Welcome Back"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// You can add as many actions as you want
UIAlertAction *startNewSession = [UIAlertAction actionWithTitle:#"Start New Session"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self startNewSession];
}];
UIAlertAction *doNothingAction = [UIAlertAction actionWithTitle:#"Resume"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// Do nothing
}];
// Add actions to the controller so they will appear
[alert addAction:doNothingAction];
[alert addAction:startNewSession];
// Finally present the action
[self.window.rootViewController presentViewController:alert animated:true completion:nil];
}
- (void)displayUIAlertView {
NSString *messageWithTitle = [NSString stringWithFormat:#"Do you want to resume playing %# or start a new session?", GAME_NAME_TITLE];
self.alertView = [[UIAlertView alloc] initWithTitle:#"Welcome Back"
message:messageWithTitle
delegate:self
cancelButtonTitle:#"Resume"
otherButtonTitles: #"Start New Session",nil];
[self.alertView show];
}
#pragma mark - Alert on restart
// buttonIndex 0 is cancel and the game continues
// buttonIndex 1 is Start New Session and the old results are saved and new session started
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self startNewSession];
}
}

Deprecated code typically means that the prior versions are not supported from the version marked and in the future. If you were building a legacy app (or an app that will be supporting prior versions) one should use the UIActionSheet and UIAlertView, however if you are updating code for iOS8+ you should use UIAlertController. The nice thing is that the code you wrote previously will not be affected so users can use a legacy version of the app without problem.

You should be able to continue using UIAlertView and UIActionSheet in iOS8 since they were deprecated in iOS8 and not in iOS7. That said, I've run into similar problems => UIAlertView automatic newline gone in iOS8?. So it appears that we may need to move to UIAlertController faster than we anticipated.

Related

UIAlertView crashs in iOS 8.3

recently i start receiving crash reports for UIAlertView only by users that use iOS 8.3
Crashlytics reports:
Fatal Exception: UIApplicationInvalidInterfaceOrientation
Supported orientations has no common orientation with the application, and [_UIAlertShimPresentingViewController shouldAutorotate] is returning YES
The line where that crash happens is [alertView show] :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
[alertView show];
that code is in the app for a long time and now it starts crashing. Did anyone experience a similar behaviour and has fixed the problem?
The main thing is :
UIApplicationInvalidInterfaceOrientation Supported orientations has no common orientation with the application
It means you have somewhere implemented
- (NSUInteger)supportedInterfaceOrientations {
return UIDeviceOrientationPortrait; // or UIInterfaceOrientationPortrait
}
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait = 0
In that function MUST be returned Mask like:
UIInterfaceOrientationMaskPortrait which is 1
try by implementing this
- (BOOL)shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
check this also : Orientation issue in Lanscape mode while opening camera in iOS 7 in iPhone
Better than this you should start using UIAlertController. It has much better functionality and also for UIAlertAction you don't have to include delegate methods.
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html
After trying the solutions here, none of them worked for me. I'm supporting iOS 6-8 in an app, and, more than that, use some libraries that use UIAlertView internally, so simply conditionally compiling to use UIAlertController when available was not an option.
I came up with a solution that solved the problem for me. Your mileage may vary. I include the header file in the Header Prefix file so that it's sure to be included anywhere a UIAlertView is shown.
I'm posting this here for anyone who's stumbling on this problem and the solutions found around the net don't work. Hopefully it's helpful.
https://gist.github.com/joshhudnall/cdc89b61d0a545c85d1d
I created a helper for displaying UIAlertView when before iOS8 and UIAlertController after iOS8, this solves the rotation crash and also displays nicely in all versions.
https://github.com/dannyshmueli/DSAlertDisplayer

Quit iOS Application Programmatically with UIAlertView

I'm aborting my iOS Application by below methods
-(void)cancelSelected
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:#"Are you sure you want to exit?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
alert = nil;
}
Method 1 :
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
abort();
}
Method 2 :
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex)
[NSException raise:#"Disagree terms and conditions." format:#"Quit, Cancel"];
}
Shall I do this to quit my iOS Application programmatically?
Will this abort() method leads to reject my app?
Thanks!
See QA1561:
Q: How do I programmatically quit my iOS application?
A: There is no API provided for gracefully terminating an iOS
application.
In iOS, the user presses the Home button to close applications. Should
your application have conditions in which it cannot provide its
intended function, the recommended approach is to display an alert for
the user that indicates the nature of the problem and possible actions
the user could take — turning on WiFi, enabling Location Services,
etc. Allow the user to terminate the application at their own
discretion.
Yes the codes above will result in a reject. Use this code instead in your OK button of alert:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
Yes, generally you will get rejected for that.
Just present an alert to the user with a singe option, so they must approve to dismiss the alert. Then, if they dismiss (approve) they can use the app and if they don't they can't and must quit the app manually.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Game Over" message:#"Your time is up" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *close = [UIAlertAction actionWithTitle:#"close" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
**exit(0);**
}];
UIAlertAction *playagain = [UIAlertAction actionWithTitle:#"Play again" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[self viewDidLoad];
}];
[alert addAction:close];
[alert addAction:playagain];
[self presentViewController:alert animated:YES completion:nil];
Use exit(0) for close current application
You can use below code to Quit iOS Application Programmatically with UIAlertView :-
Step 1:
Delegate "UIAlertViewDelegate" to your viewcontroller.h
for example:
#interface User_mail_List : UIViewController< UIAlertViewDelegate >
Step 2:
//create your UIAlertView
UIAlertView *exit_alertView= [[UIAlertView alloc] initWithTitle:#"Bizbilla !" message:#"\nAre you sure you want to Exit ?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[exit_alertView show];
Step 3:
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if(alertView==exit_alertView){//exit Alert Fns Start,,,,,
if(buttonIndex==1){
exit(0);
}
}//exit Alert Fns End,,,,,
}
thanks,
On your alertview button click
You can use: exit(0)?
Or,
[[NSThread mainThread] exit], using this you can quit ios app.
How about calling fatalError()
function? I've just used it, everything works as expected. (Hope this will not cause a rejection though.)

UIDocumentInteractionController disable open in certain apps

I'm currently using a UIDocumentInteractionController for open in functionality. When it opens it shows a list of all apps on the device that can handle that file type.
Is there a way to disable my app sending a document to specific apps, even if they support that file type? For example - if I have a PDF file open in my app and iBooks is on the iPad, if I tap the iBooks icon in the UIDocumentInteractionController, I don't want it to send it to the app.
Ideally - I see this as building a blacklist (or whitelist). For example, it would be great to do this:
- (void) documentInteractionController: (UIDocumentInteractionController *) controller willBeginSendingToApplication: (NSString *) application {
// if app is blacklisted
if ([application isEqualToString:#"com.schimera.WebDAVNavigator"]) {
[self.interactionController dismissMenuAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"FAIL" message:#"NO" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return
}
}
With this however, the document is still sent to the application even if it is "black listed".
Is this approach at all possible?
Cheers!
Change the UIDocumentInteractionController's URL to an invalid value if the app is blacklisted. In the method -[UIDocumentInteractionControllerDelegate documentInteractionController: willBeginSendingToApplication:].
-(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application
{
if([application isEqualToString:#"com.evilcorp.badapp"){
controller.URL = nil;
}
}
To offer open file in a specific app (as white list), just add (using swift 3.0):
docController.uti = #"com.yourapp.package";

UIActionSheet dismisses on 2nd click of the button

I have an actionSheet with EDIT and DELETE buttons, both being other buttons This is the Code i have written for it
-(void)method1
{
action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Edit", #"Delete", nil];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[action showInView:self.view];
[action release];
}
I have used the deleate method to assign actions to method..
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
// do something
}
if(buttonIndex == 1)
{
// do something
}
}
now the problem is that the actionsheet does not dismiss at one click of either of the buttons.. Please help me with some solution.
This appears to be a bug in iOS4.0. I had this issue in my simulator. I changed the version to 4.3 and 5.0 and it seemed ok.
Edit:
Seems that my issue was more specifically to do with the actionsheet being launched twice by a delegate method "-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField"
Not sure why this is called twice in this case but not others (again I assume a iOS 4.0 bug that's been fixed in later releases). My workaround is to keep track if it's been called already and not call it a second time.
Edit 2
I would suggest doing something like:
-(void)method1
{
if(hasLaunchedActionSheet)
{
return;
}
hasLaunchedActionSheet = YES;
...
and:
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex
{
hasLaunchedActionSheet = NO;
...
For me, the issue's not so much in Xcode as it is in the iOS SDK itself calling my event twice. I'm not sure how you're calling method1 so it might be a different issue with a different event.
You are using wrong delegate methods, for button interaction, you should use:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
instead of:
-(void)actionSheet:(UIActionSheet *)action didDismissWithButtonIndex:(NSInteger)buttonIndex

iOS - Push notification alert is not shown when the app is running

I've integrated push notifications in my app. Users will receive push notification to join a group. When the user clicks Join, I've to handle something in the code. And so I'm implementing:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
This is working fine when the app is not running.
When the app is running, I don't see any UIAlertView. How can make my app show the push notification alert so that user can still decide whether to join or not?
I used code like this in my application delegate to mimic the notification alert when the app was active. You should implement the appropriate UIAlertViewDelegate protocol method(s) to handle what happen when the user taps either of the buttons.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = #"Close";
NSString *showTitle = #"Show";
NSString *message = [[userInfo valueForKey:#"aps"] valueForKey:#"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Some title"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
[alertView release];
} else {
//Do stuff that you would do if the application was not active
}
}
For anyone might be interested, I ended up creating a custom view that looks like the system push banner on the top but adds a close button (small blue X) and an option to tap the message for custom action. It also supports the case of more than one notification arrived before the user had time to read/dismiss the old ones (With no limit to how many can pile up...)
Link to GitHub: AGPushNote
The usage is basically on-liner:
[AGPushNoteView showWithNotificationMessage:#"John Doe sent you a message!"];
And it looks like this on iOS7 (iOS6 have an iOS6 look and feel...)
You have to show the alert yourself if you want to. This is intentional behavior as documented here http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html below Listing 2-6
only this function will be invoked and you have to explicitly show the alert on that case no notification will come if app is running in which you have implement the notification.Put the break point there and handle the notification call when function called and show your customized alert there.
For showing alert view while running application you have to use
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
and by accessing the userInfo variable
The app will still receive the -application:didReceiveRemoteNotification message in your App Delegate, but you'll have to act on the message yourself (i.e. the alert isn't displayed by default).
The userInfo parameter contains an object with the key notificationType, which you can use to identify the push message.
Here is a version which support UIAlertController
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if (state == UIApplicationStateActive) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:notification.alertTitle
message:notification.alertBody
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self.navigationController presentViewController:alert animated:YES completion:nil];
}
}
** Please note my app uses self.navigationController in App Delegate, just hook on to any ViewController to present ( show ) the Alert **

Resources