Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #225 from ParsePlatform/nlutsenko.alerts
Browse files Browse the repository at this point in the history
Use UIAlertController to present alerts if it's available.
  • Loading branch information
nlutsenko committed Jan 8, 2016
2 parents 89650e4 + 4637462 commit b4a3219
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 108 deletions.
46 changes: 38 additions & 8 deletions ParseUI/Classes/Internal/Extensions/PFUIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,44 @@

#import <UIKit/UIKit.h>

@interface PFUIAlertView : UIAlertView
NS_ASSUME_NONNULL_BEGIN

+ (void)showAlertViewWithTitle:(NSString *)title
error:(NSError *)error;
+ (void)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message;
+ (void)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle;
typedef void(^PFUIAlertViewCompletion)(NSUInteger selectedOtherButtonIndex);
typedef void(^PFUIAlertViewTextFieldCompletion)(UITextField *textField, NSUInteger selectedOtherButtonIndex);
typedef void(^PFUIAlertViewTextFieldCustomizationHandler)(UITextField *textField);

@interface PFUIAlertView : NSObject

///--------------------------------------
#pragma mark - Present
///--------------------------------------

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(nullable NSArray *)otherButtonTitles
completion:(nullable PFUIAlertViewCompletion)completion;

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message
textFieldCustomizationHandler:(PFUIAlertViewTextFieldCustomizationHandler)textFieldCustomizationHandler
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(nullable NSArray *)otherButtonTitles
completion:(nullable PFUIAlertViewTextFieldCompletion)completion;

///--------------------------------------
#pragma mark - Convenience
///--------------------------------------

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
error:(NSError *)error;
+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message;

@end

NS_ASSUME_NONNULL_END
191 changes: 174 additions & 17 deletions ParseUI/Classes/Internal/Extensions/PFUIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,191 @@

#import "PFLocalization.h"

@interface PFUIAlertView () <UIAlertViewDelegate>

@property (nonatomic, copy) PFUIAlertViewCompletion completion;

@end

@implementation PFUIAlertView

+ (void)showAlertViewWithTitle:(NSString *)title error:(NSError *)error {
///--------------------------------------
#pragma mark - Present
///--------------------------------------

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(nullable NSArray *)otherButtonTitles
completion:(nullable PFUIAlertViewCompletion)completion {
if ([UIAlertController class] != nil) {
__block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];

void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action) {
if (completion) {
// This block intentionally retains alertController, and releases it afterwards.
if (action.style == UIAlertActionStyleCancel) {
completion(NSNotFound);
} else {
NSUInteger index = [alertController.actions indexOfObject:action];
completion(index - 1);
}
}
alertController = nil;
} copy];

[alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle
style:UIAlertActionStyleCancel
handler:alertActionHandler]];

for (NSString *buttonTitle in otherButtonTitles) {
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
style:UIAlertActionStyleDefault
handler:alertActionHandler]];
}

[viewController presentViewController:alertController animated:YES completion:nil];
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
__block PFUIAlertView *pfAlertView = [[self alloc] init];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];

for (NSString *buttonTitle in otherButtonTitles) {
[alertView addButtonWithTitle:buttonTitle];
}

pfAlertView.completion = ^(NSUInteger index) {
if (completion) {
completion(index);
}

pfAlertView = nil;
};

alertView.delegate = pfAlertView;
[alertView show];
#endif
}
}

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message
textFieldCustomizationHandler:(PFUIAlertViewTextFieldCustomizationHandler)textFieldCustomizationHandler
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(nullable NSArray *)otherButtonTitles
completion:(nullable PFUIAlertViewTextFieldCompletion)completion {
if ([UIAlertController class] != nil) {
__block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:textFieldCustomizationHandler];
void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action) {
if (completion) {
UITextField *textField = alertController.textFields.firstObject;
// This block intentionally retains alertController, and releases it afterwards.
if (action.style == UIAlertActionStyleCancel) {
completion(textField, NSNotFound);
} else {
NSUInteger index = [alertController.actions indexOfObject:action];
completion(textField, index - 1);
}
}
alertController = nil;
} copy];

[alertController addAction:[UIAlertAction actionWithTitle:cancelButtonTitle
style:UIAlertActionStyleCancel
handler:alertActionHandler]];

for (NSString *buttonTitle in otherButtonTitles) {
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle
style:UIAlertActionStyleDefault
handler:alertActionHandler]];
}

[viewController presentViewController:alertController animated:YES completion:nil];
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
__block PFUIAlertView *pfAlertView = [[self alloc] init];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
for (NSString *buttonTitle in otherButtonTitles) {
[alertView addButtonWithTitle:buttonTitle];
}
textFieldCustomizationHandler([alertView textFieldAtIndex:0]);

__weak UIAlertView *walertView = alertView;
pfAlertView.completion = ^(NSUInteger index) {
if (completion) {
UITextField *textField = [walertView textFieldAtIndex:0];
completion(textField, index);
}

pfAlertView = nil;
};

alertView.delegate = pfAlertView;
[alertView show];
#endif
}
}

///--------------------------------------
#pragma mark - Convenience
///--------------------------------------

+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
error:(NSError *)error {
NSString *message = error.userInfo[@"error"];
if (!message) {
message = [error.userInfo[@"originalError"] localizedDescription];
message = [error.userInfo[@"originalError"] localizedDescription];
}
if (!message) {
message = [error localizedDescription];
message = [error localizedDescription];
}
[self showAlertViewWithTitle:title message:message];
[self presentAlertInViewController:viewController withTitle:title message:message];
}

+ (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
[self showAlertViewWithTitle:title
message:message
cancelButtonTitle:PFLocalizedString(@"OK", @"OK")];
+ (void)presentAlertInViewController:(UIViewController *)viewController
withTitle:(NSString *)title
message:(nullable NSString *)message {
[self presentAlertInViewController:viewController
withTitle:title
message:message
cancelButtonTitle:PFLocalizedString(@"OK", @"OK")
otherButtonTitles:nil
completion:nil];
}

+ (void)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle {
UIAlertView *alertView = [[self alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
[alertView show];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0

///--------------------------------------
#pragma mark - UIAlertViewDelegate
///--------------------------------------

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (self.completion) {
if (buttonIndex == alertView.cancelButtonIndex) {
self.completion(NSNotFound);
} else {
self.completion(buttonIndex - 1);
}
}
}

#endif

@end
48 changes: 19 additions & 29 deletions ParseUI/Classes/LogInViewController/PFLogInViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,6 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}

///--------------------------------------
#pragma mark - UIAlertViewDelegate
///--------------------------------------

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
NSString *email = [alertView textFieldAtIndex:0].text;
[self _requestPasswordResetWithEmail:email];
}
}

///--------------------------------------
#pragma mark - Private
///--------------------------------------
Expand Down Expand Up @@ -294,19 +283,22 @@ - (void)_forgotPasswordAction PF_EXTENSION_UNAVAILABLE("") {
NSString *title = PFLocalizedString(@"Reset Password", @"Forgot password request title in PFLogInViewController");
NSString *message = PFLocalizedString(@"Please enter the email address for your account.",
@"Email request message in PFLogInViewController");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:PFLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:PFLocalizedString(@"OK", @"OK"), nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

UITextField *textField = [alertView textFieldAtIndex:0];
textField.placeholder = PFLocalizedString(@"Email", @"Email");
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyDone;

[alertView show];
[PFUIAlertView presentAlertInViewController:self
withTitle:title
message:message
textFieldCustomizationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = PFLocalizedString(@"Email", @"Email");
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyDone;
}
cancelButtonTitle:PFLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:@[ PFLocalizedString(@"OK", @"OK")]
completion:^(UITextField * _Nonnull textField, NSUInteger selectedOtherButtonIndex) {
if (selectedOtherButtonIndex != NSNotFound) {
NSString *email = textField.text;
[self _requestPasswordResetWithEmail:email];
}
}];
}

- (void)_requestPasswordResetWithEmail:(NSString *)email {
Expand All @@ -316,13 +308,11 @@ - (void)_requestPasswordResetWithEmail:(NSString *)email {
@"Password reset success alert title in PFLogInViewController.");
NSString *message = [NSString stringWithFormat:PFLocalizedString(@"An email with reset instructions has been sent to '%@'.",
@"Password reset message in PFLogInViewController"), email];
[PFUIAlertView showAlertViewWithTitle:title
message:message
cancelButtonTitle:PFLocalizedString(@"OK", @"OK")];
[PFUIAlertView presentAlertInViewController:self withTitle:title message:message];
} else {
NSString *title = PFLocalizedString(@"Password Reset Failed",
@"Password reset error alert title in PFLogInViewController.");
[PFUIAlertView showAlertViewWithTitle:title error:error];
[PFUIAlertView presentAlertInViewController:self withTitle:title error:error];
}
}];
}
Expand Down Expand Up @@ -480,7 +470,7 @@ - (void)_loginDidFailWithError:(NSError *)error {
} else {
message = PFLocalizedString(@"Please try again", @"Generic login failed alert message in PFLogInViewController");
}
[PFUIAlertView showAlertViewWithTitle:title message:message];
[PFUIAlertView presentAlertInViewController:self withTitle:title message:message];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PFLogInFailureNotification object:self];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ - (void)objectsDidLoad:(NSError *)error {

cell.state = PFPurchaseTableViewCellStateDownloading;
[PFPurchase downloadAssetForTransaction:transaction
completion:^(NSString *filePath, NSError *downloadError) {
if (!downloadError) {
completion:^(NSString *filePath, NSError *error) {
if (!error) {
cell.state = PFPurchaseTableViewCellStateDownloaded;
} else {
cell.state = PFPurchaseTableViewCellStateNormal;

NSString *title = PFLocalizedString(@"Download Error",
@"Download Error");
[PFUIAlertView showAlertViewWithTitle:title error:downloadError];
[PFUIAlertView presentAlertInViewController:self withTitle:title error:error];
}
}
progress:^(int percentDone) {
Expand Down Expand Up @@ -175,7 +175,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[PFPurchase buyProduct:product.productIdentifier block:^(NSError *error) {
if (error) {
NSString *title = PFLocalizedString(@"Purchase Error", @"Purchase Error");
[PFUIAlertView showAlertViewWithTitle:title error:error];
[PFUIAlertView presentAlertInViewController:self withTitle:title error:error];
}
}];
}
Expand Down

0 comments on commit b4a3219

Please sign in to comment.