I have a share dialog in a popover window which I want to trigger a callback when closed.
I've tried assigning a delegate to the UIPopoverPresentationController but none of the callbacks are being triggered when I close the share dialog in the app.
I have the callback delegate defined as PicklePopupDelegate which I just have these 3 functions for testing, none of them get triggered.
@implementation PicklePopupDelegate
- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
// Share activity was closed, handle the event here
NSLog(@"Share activity closed");
UnitySendMessage("ShareManager", "OnSharePromptClosedIOS", "");
}
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
// Share activity was closed, handle the event here
NSLog(@"Share activity closed");
UnitySendMessage("ShareManager", "OnSharePromptClosedIOS", "");
}
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController {
NSLog(@"Showing share activity");
}
@end
Then the main share function is defined here, everything seems to be working correctly except the delegate assigned to the UIPopoverPresentationController isn't triggering any of the callbacks when the share dialog is closed.
@implementation PicklePlugin
-(PicklePlugin*) initShareMessage:(char*)message initShareSubject:(char*)subject initDoShareIcon:(bool*)doShareIcon{
// Set self to the reference of the original caller
self = [super init];
// Create a new array named items
NSMutableArray *items = [NSMutableArray new];
if(strlen(message) > 0){
// Add the message to the item array
[items addObject:[NSString stringWithUTF8String:message]];
}
if(doShareIcon){
UIImage *appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
if(appIcon != nil)
[items addObject:appIcon];
}
// Initialize a new activity https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622019-initwithactivityitems
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
if(strlen(subject) > 0){
// Set the value of a key name "subject" to the subject parameter we sent on the activity we created
[activity setValue:[NSString stringWithUTF8String:subject] forKey:@"subject"];
}
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
// While the rootViewController has a parent set it to be the parent until it eventually no longer has a parent
// https://pinkstone.co.uk/how-to-avoid-whose-view-is-not-in-the-window-hierarchy-error-when-presenting-a-uiviewcontroller/
while(rootViewController.presentedViewController)
rootViewController = rootViewController.presentedViewController;
// Present the view controller using the popover style
activity.modalPresentationStyle = UIModalPresentationPopover;
[rootViewController presentViewController:activity animated:YES completion:nil];
// Create an instance of PicklePopupDelegate
PicklePopupDelegate *popoverDelegate = [[PicklePopupDelegate alloc] init];
// Get the popover presentation controller and configure it
UIPopoverPresentationController *presentationController = [activity popoverPresentationController];
presentationController.permittedArrowDirections = 0;
presentationController.sourceView = rootViewController.view;
presentationController.sourceRect = CGRectMake(CGRectGetMidX(rootViewController.view.bounds), CGRectGetMidY(rootViewController.view.bounds), 0, 0);
presentationController.delegate = popoverDelegate;
return self;
}
// stripped out other functions
@end
And in the header file I have the classes defined as:
@interface PicklePopupDelegate : UIViewController <UIPopoverPresentationControllerDelegate>
@end
@interface PicklePlugin : UIViewController <UIPopoverPresentationControllerDelegate>
// more code here
@end