iOS AlertView App Extension - ios

I'm working in a custom keyboard (iOS App Extension). I have a UICollectionView in my Keyboard Layout, so when one item is selected I want to show a message (UIAlerView for example).
Here is my code:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
...
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
}
I get this error: 'Feature not available in extensions of type com.apple.keyboard-service'
So...is there any way to show a message from an App Extension?
EDIT:
Here is an example. The IKEA Emoticons Keyboard shows a message (like an Android Toast after selecting one item).
I also have tried this library:
iOS Toast Library

Sad to say that, but there's no way to show a UIAlertView in keyboard extension. Actually, nothing above the frame of InputViewController can be showed. It's pretty clear in the Apple's doc:
...a custom keyboard can draw only within the primary view of its UIInputViewController object... it is not possible to display key artwork above the top edge of a custom keyboard’s primary view, as the system keyboard does on iPhone when you tap and hold a key in the top row.
As for message inside the keyboard, there are some useful libraries that can help us with it. For example https://github.com/scalessec/Toast and https://github.com/sergeylenkov/PTHintController.

Finally I solved the problem. It's not the best solution, but at least I get the effect that I wanted.
I've created a View simulating a Toast in the xib file and set it to hidden.
When the item is selected, I show the "faked" Toast for 2 seconds and hide it again.
self.popUpView.hidden = NO;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
self.popUpView.hidden = YES;
});
I don't know if it's a good solution, but I really had to find a solution for that.
For Swift you can use this :
self.popUpView.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.popUpView.isHidden = true
}

Related

Toast Message in ios with multiline

I Am looking for a Toast Message Library such as MBProgressHUD, which contains loading screen with message and without, detail text and only text message.
But MBProgressHUD not have the support for multiline toast message only in showing text message
Can anyone suggest me a Library.
Thanks in advance.
Please use detailsLabelText property of MBProgressHUD. It supports multiline text.
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.detailsLabelText = #"Your multiline text here ......."
hud.removeFromSuperViewOnHide = YES;
hud.margin = 10.f;
[hud hide:YES afterDelay:2];
This library does what you need DCToastView allowing you to provide toast messages from the top or bottom side of the screen using auto layout and stackviews it renders as many lines as you need:
You will just need to add the pod
pod 'DCToastView'
Import it where you want to use it.
import DCToastView
And use it
ToastPresenter.shared.show(in: self.view, message: "This is a toast")
You can pass the following properties to the show method:
view: The view in which the toast is going to be presented
message: The message that the toast will show
toastPlace: The place which can be .down or .up
backgroundColor: The background color of the toast; defaults to black
textColor: The text color of the message; defaults to white
timeOut: The amount of seconds for the toast to dismiss if not provided it means that the toast will be sticky (will remain until touched); defaults to nil
roundness: How round the toast will be: .none, .low, .mid, .high; defaults to .mid
This is something, which was needed by you. Created with lot of Animation Options, screen positions, and duration. Even you can provide your own duration. Check that out below.
AVIToast Link
ToastThis Supports Multiline toasts.
I'm able to display multiline messages with MBProgressHUD using this code:
MBProgressHUD *h = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
h.mode = MBProgressHUDModeText;
h.labelText = n.userInfo[#"message"];
h.labelFont = [UIFont boldSystemFontOfSize:12];
[h hide:YES afterDelay:1];

UIActionSheet from Popover with iOS8 GM

Anyone is getting this message while trying to show UIActionSheet from popover?
Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
Previously to the GM I used some workaround for converting the UIActionSheet to UIAlertController and this is working fine.
However it seems that Apple tried to solve the UIActionSheet issues and I didn't want to use my workaround - but it seems that I have no choice...
To support iPad, include this code:
alertView.popoverPresentationController?.sourceView = self.view
alertView.popoverPresentationController?.sourceRect = self.view.bounds
// this is the center of the screen currently but it can be any point in the view
self.presentViewController(alertView, animated: true, completion: nil)
If you are presenting the action sheet after the user makes a selection on a cell within a UITableView. I found that this works decently well:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Directions"
message:#"Select mode of transportation:"
preferredStyle:UIAlertControllerStyleActionSheet];
alert.popoverPresentationController.sourceView = cell;
alert.popoverPresentationController.sourceRect = cell.bounds;
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
//...
[self presentViewController:alert animated:YES completion:nil];
You need to provide popoverPresentationController for iPad support. In this, you either specify barButtonItem or sourceView. This another thread may help you: Swift UIAlertController - ActionSheet iPad iOS8 Crashes
Actually it is something buggy (I believe) in Xcode for iPhone and iPad designs for now.
In iPhone same code works perfect and you can see the alert message at same position (always). But for iPad you need to define the alert box's position with alert.popoverPresentationController.sourceView = self.view;
alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0 - 105, self.view.bounds.size.height / 2.0 + 70, 1.0, 1.0); 105 and 70 are the approximate dimension differences for iPad portrait design due to different anchor point.
In iPhone design UIAlertController comes with 'Modal View' but unfortunately if you use same code for iPad it will not be a 'Modal View'. Which means that you need to write extra code for disabling touches in iPad design. I think it is weird.
In iPad design you need to consider that anchor point is different. It is the bubble triangle point, not the upper left of AlertView.
These are the weird things I see. I think that there must be a standard and if someone wants to go out standards, fine, there can be other options.
UIAlertController being iOS8 only, and needing to support iOS7, I am using it. I ran into this on a Master view in a Master/Detail layout on iPad. I was able to work around it (not exactly fix it) by raising the UIActionSheet from the parent UISplitViewController using [actionSheet showInView:]. Good luck.
If you want to present it in the centre with no arrows on iPads and normally on iPhones: [Swift 3+]:
if let popoverController = alertController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
self.present(alertController, animated: true, completion: nil)

UIAlertView has scrollable title and message on iOS7

I have a UIAlertView with text in it that can be displayed on iOS6 without any problem.
However in iOS7 the title and message of this alert are in a scrollview.
I'm creating the alert with following code
self.newCategoryAlertView = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"MessageTitleNewCategory", nil)
message:NSLocalizedString(#"MessageTextNewCategory", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"ButtonCancel", nil)
otherButtonTitles:NSLocalizedString(#"ButtonOK", nil), nil] autorelease];
self.newCategoryAlertView.tag = alertViewTypeNewCategory;
self.newCategoryAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.newCategoryAlertView textFieldAtIndex:0].delegate = self;
[self.newCategoryAlertView textFieldAtIndex:0].autocapitalizationType = UITextAutocapitalizationTypeSentences;
[[self.newCategoryAlertView textFieldAtIndex:0] setReturnKeyType:UIReturnKeyDone];
[[self.newCategoryAlertView textFieldAtIndex:0] setKeyboardAppearance:UIKeyboardAppearanceDefault];
[self.newCategoryAlertView textFieldAtIndex:0].enablesReturnKeyAutomatically = YES;
[self.newCategoryAlertView show];
The result on iOS7 is this:
I tried to make the the text shorter with no success and the documentation of UIAlertView didn't give me a hint.
How do I workaround this behavior?
EDIT:
This is how it looks when scrolled down. As you can see the complete message is visible.
In my opinion that's a good behaviour, as there's no space in the screen to show the keyboard and also de UIAlert. I'm almost sure that if you make the app work in portrait orientation, the alert will show ok with no scrollable title and message.
If the app only runs in landscape orientation, and you don't want to have a scrollable view, I guess the best solution will be maybe to keep the UIAlert message, but taking away the UIAlert tilte.
I hope this helped!
I have used a custom control URBAlertView. I found a similar problem with this & I have reported that to the author on the github here.
There was a fix provided by the author under the commit here. However you can also refer the top three commits on the repo here.
I hope that may help you all to figure out a solution

UIAlertView addSubview in iOS7

Adding some controls to UIAlertView was deprecated in iOS7 using addSubview method. As I know Apple promised to add contentView property.
iOS 7 is released now and I see that this property is not added. That is why I search for some custom solution with ability to add progress bar to this alertView. Something for example similar to TSAlertView, but more ready for using in iOS 7.
Here is a project on Github to add any UIView to an UIAlertView-looking dialog on iOS7.
(Copied from this StackOverflow thread.)
It took me only 1 day to create my own alert view that looks exactly like Apple's
Take a screenshot of Apple's alert for reference (font sizes, spacings, width)
Create a xib with title, message, custom view and tables for buttons (Apple uses tables instead of UIButton now, default table cell is good enough). Note you need 3 button tables: two for left and right buttons (whenever the number of buttons is 2), another one for the other cases (one button or more than 2 buttons).
Implement all the methods from UIAlertView on your custom alert.
Show/Dismiss - you can create a specific modal window for your alerts but I just put my alerts on top of my root view controller. Register your visible alerts to a static array. If showing the first alert/dismissing the last, change tint mode of your window/view controller to dimmed/to automatic and add/remove a dimming view (black with alpha = 0.2).
Blurred background - use Apple's sample code (I used opaque white)
3D dynamic effects - use Apple's sample code (5 lines of code). If you want a nice effect, take a slightly bigger snapshot in step 5 and add inverse animators for alert background and foreground.
EDIT:
Both blurred background and the paralax effect sample code can be found in "iOS_RunningWithASnap" WWDC 2013 sample code
Paralax effect:
UIInterpolatingMotionEffect* xAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis] autorelease];
xAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
xAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIInterpolatingMotionEffect* yAxis = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.y"
type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis] autorelease];
yAxis.minimumRelativeValue = [NSNumber numberWithFloat:-10.0];
yAxis.maximumRelativeValue = [NSNumber numberWithFloat:10.0];
UIMotionEffectGroup *group = [[[UIMotionEffectGroup alloc] init] autorelease];
group.motionEffects = #[xAxis, yAxis];
[self addMotionEffect:group];
The blurred background is the only complicated thing. If you can use an opaque color instead, use it. Otherwise it's a lot of experimenting. Also note that blurred background is not a good solution when the background is dark.
For the show/dismiss animationg, I am using the new spring animation method:
void (^animations)() = ^{
self.alpha = 1.0f;
self.transform = CGAffineTransformIdentity;
};
self.alpha = 0.0f;
self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
[UIView animateWithDuration:0.3
delay:0.0
usingSpringWithDamping:0.7f
initialSpringVelocity:0.0f
options:UIViewAnimationOptionCurveLinear
animations:animations
completion:^(BOOL completed) {
//calling UIAlertViewDelegate method
}];
I wrote a full implementation of UIAlertView that mimics the complete UIAlertView API, but adds the contentView property we've all wanted for so long: SDCAlertView.
(source: github.io)
For those who love simple and effective methods with out having to write lines of code. Here is a cool solution without using any other private frame works for adding subviews to ios 7 alert views,i.e.
[alertView setValue:imageView forKey:#"accessoryView"];
Sample code for better understanding,
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 85, 50)];
UIImage *wonImage = [UIImage imageNamed:#"image.png"];
[imageView setImage:wonImage];
//check if os version is 7 or above
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
[alertView setValue:imageView forKey:#"accessoryView"];
}else{
[alertView addSubview:imageView];
}
Hope it helps some one,thanks :)
For IOS7
UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:#"Enter Form Name"
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView1.alertViewStyle = UIAlertViewStyleSecureTextInput;
UITextField *myTextField = [alertView1 textFieldAtIndex:0];
[alertView1 setTag:555];
myTextField.keyboardType=UIKeyboardTypeAlphabet;
[alertView1 show];
There wont be UIAlertView with custom views in iOS7, nor contentView which Apple changed its mind about, so addSubview is impossible now in UIAlertView.
A good alternative will be SVProgressHUD, according to many threads in Apple's forum.
Edit:
There is officially no addSubview nor subclassing for UIAlertView in iOS7.
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
Other good alternatives:
ios-custom-alertview by wimagguc
MZFormSheetController.
You can find simple solution without extra classes here
It is based on setting accessoryView for ordinary UIAlertView.
PKAlertController (https://github.com/goodpatch/PKAlertController) is great library. I tested a lot of similar libraries and just this satisfied all my requirements.
Why it is cool:
Supports custom view
Supports iOS7
It is view controller
It behaves and looks like native alert view, including motion effects
Customizable
Similar interface like in UIAlertController

Display UIProgressView with percentage in iOS

How can I display a UIProgressView with a percentage?
As Vlad mentioned you can have a method which updates the value of progressView and its associated label. Call the following method when any update happens.
- (void)updateProgress
{
CGFloat stepSize = 0.1f;
[self.progressView setProgress:self.progressView.progress+stepSize];
self.progressLabel.text = [NSString stringWithFormat:#"Completed : %.0f",self.progressView.progress*100];
}
The progress indicator control does not offer the option of also displaying progress percent.
In order to achieve this, one solution would be to add a label positioned below / above your progress bar, and at each step when you refresh the progress on your progress indicator view, you also update the string value in on your label to display the percentage loaded.
There is one widget already available in iOS SDK - UIProgressView
Where you can set and get its property: progress
Hope this may help you.

Resources