UIWebView and define dictionary

2019-03-24 19:32发布

I had the copy and define option built in with the UIWebView. It worked just fine on the iPad but on the iPhone, it only works once when I highlight the text and use the dictionary and the second time I tried it, it doesn't show up the popover. Any idea?

UPDATE: I am getting the following error as well when dismissing the dictionary on the iPhone: Unbalanced calls to begin/end appearance transitions for UIFallbackPresentationViewController

UPDATE: When I present a UIAlertView and cancel's it the dictionary works again. Wonder why?

4条回答
我只想做你的唯一
2楼-- · 2019-03-24 19:42

At the moment there are 52 other questions on stackoverflow mentioning this error..

From reading a few i think the code apple is using is calling presentModalViewController: before the actual dictionary view is fully loaded.

I think this is just a bug which apple has to solve, Rudolf Adamkovic's sample made it clear:

  • Create "Single View Application"
  • Add UITextView
  • Run it
  • Select a word in the text view
  • Choose Define

  • Debug console tell's you something like this:

    Unbalanced calls to begin/end appearance transitions for 
    <_UIFallbackPresentationViewController: 0x74c1660>
    

I filed the bug at: https://bugreport.apple.com

Sample code: https://github.com/tiemevanveen/DefineBugDemoProject

查看更多
霸刀☆藐视天下
3楼-- · 2019-03-24 19:53

All credit for this solution goes to @acue. Please see here.

Now, here's how I solved it. Printed [UIApplication sharedApplication].keyWindow after the dictionary had been presented to find out which subview was being given focus.

In that subview, registered for UIWindowDidBecomeKeyNotification

And. when the notification got posted, called the following method :-

- (void) makeKeyWindow {
  AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [appDelegate.window makeKeyAndVisible];
}

Even though, I still see the error, the UIMenu & the Dictionary both continue to work as expected.

查看更多
爷、活的狠高调
4楼-- · 2019-03-24 20:00

I am able to reproduce the error (shows up in console), but for me the popover works every time. So I wouldn't worry about it since it doesn't seem to be causing any crashes.

The only "work around" is to disable/replace the "Define" menu item. But the only way to do that is by writing a category for a private class (which is forbidden):

@interface UIWebBrowserView : UIView

@end

@interface UIWebBrowserView (Add)


@end

@implementation UIWebBrowserView (Add)
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if(action == @selector(_define:))
        return NO;
    return [super canPerformAction:action withSender:sender];
}
@end

There are more "clever" ways of doing this without writing a category (method swizzling), but they are more dangerous.

If it's working I would just leave it alone. I have ARC enabled, so I'm not sure if that makes a difference.

查看更多
▲ chillily
5楼-- · 2019-03-24 20:05

Without seeing your code it is going to be really hard to give an answer to this question. It would be much easier if you would show your logic.

That being said, here are a couple of tips that might help:

  1. Make sure you are calling all appropriate super methods in your class. For example, if you override viewDidAppear:animated or viewDidLoad, make sure you call the super methods.
  2. Checkout this thread...maybe you are doing some sort of animation in an inappropriate place?
查看更多
登录 后发表回答