简体   繁体   中英

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

在此处输入图片说明

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM