Search code examples
iosios-app-extensionuialertcontrollercustom-keyboard

iOS AlertView App Extension


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

enter image description here


Solution

  • 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
        }