iOS AlertView App Extension

流过昼夜 提交于 2019-12-08 03:56:31

问题


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


回答1:


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.




回答2:


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
    }


来源:https://stackoverflow.com/questions/29825154/ios-alertview-app-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!