sticksen / STKWebKitViewController

Usable UI around WKWebView
MIT License
147 stars 29 forks source link

Crash on pressing Share button on iPad #5

Closed dhiraj closed 10 years ago

dhiraj commented 10 years ago

Immediate, reproducable crash on iPad when the share button is pressed, with this trace:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7f1c5710>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*** First throw call stack:
(
    0   CoreFoundation                      0x05a8b946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x05714a97 objc_exception_throw + 44
    2   UIKit                               0x0491ae47 -[UIPopoverPresentationController presentationTransitionWillBegin] + 3086
    3   UIKit                               0x0421d385 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1666
    4   UIKit                               0x0421b968 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 226
    5   UIKit                               0x0424f7ab __40+[UIViewController _scheduleTransition:]_block_invoke + 18
    6   UIKit                               0x041150ce ___afterCACommitHandler_block_invoke + 15
    7   UIKit                               0x04115079 _applyBlockToCFArrayCopiedToStack + 415
    8   UIKit                               0x04114e8e _afterCACommitHandler + 545
    9   CoreFoundation                      0x059ae9de __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    10  CoreFoundation                      0x059ae920 __CFRunLoopDoObservers + 400
    11  CoreFoundation                      0x059a435a __CFRunLoopRun + 1226
    12  CoreFoundation                      0x059a3bcb CFRunLoopRunSpecific + 443
    13  CoreFoundation                      0x059a39fb CFRunLoopRunInMode + 123
    14  GraphicsServices                    0x069e624f GSEventRunModal + 192
    15  GraphicsServices                    0x069e608c GSEventRun + 104
    16  UIKit                               0x040eb8b6 UIApplicationMain + 1526
    17  picTrove 2 pro                      0x000b0a0d main + 141
    18  libdyld.dylib                       0x06432ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

It looks like it needs a barButtonItem set on the popoverPresentationController of the UIActivityViewController to make it work. I tested this and got it to work, by:

Adding a weak, private property on the STKWebKitViewController to hold on to the shareButtonItem after it is created, and then assigned it in the fillToolbar method, like so:

@interface STKWebKitViewController ()
...
@property (nonatomic,weak) UIBarButtonItem * shareButtonItem;
...
@end

- (void)fillToolbar
{
...
    [self setToolbarItems:@[flexibleSpaceItem, backItem, flexibleSpaceItem, forwardItem, flexibleSpaceItem, reloadItem, flexibleSpaceItem, shareItem, flexibleSpaceItem] animated:NO];
    self.shareButtonItem = shareItem;
}
...
- (void)shareTapped:(UIBarButtonItem *)button
{
...
    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:self.applicationActivities];
    controller.popoverPresentationController.barButtonItem = self.shareButtonItem;
    [self presentViewController:controller animated:YES completion:nil];
}

You might want to implement it differently, of course, (especially the weak property part), but I just had to fix it and submitted the bug to help others. Thanks!

sticksen commented 10 years ago

Thanks, fixed! :+1: no new property needed, as the button-parameter in the shareTapped:-function already delivers the right UIBarButtonItem. :)