UIActivityViewController calls ViewWillAppear but not ViewWillDisappear
I find that the call UIActivityViewController
does not call ViewWillDisappear
, but when the user returns from the VC, it does call ViewWillAppear
. This process has two steps as it first displays half the screen so that the user can select things like text or email. Cancel here goes back to the calling VC without being called ViewWillAppear
. It is perfectly. However, if you choose a link option such as text, the VC will be replaced with a full-text text VC that never invokes ViewWillDisappear
.
I could call ViewWillDisappear
manually, however, while doing this, I don't know if the user has selected "cancel" on a smaller half screen before going full screen.
The code calling ActivityVC looks like this:
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:shareAray
applicationActivities:nil];
[self presentViewController:activityController
animated:YES completion:nil];
Any reason ViewWillDisappear
not being called on return is calling ViewWillAppear
? Is there anyway to see if the user hit "undo" on the smaller half-screen before so that I don't call myself ViewWillDisappear
if they don't continue the valid share to the full screen?
source to share
As Gutblender showed via our exchange above, UIActivityVC does not call ViewWillDisappear due to modalPresentationStyle. Apple doesn't seem to be grasping this right as it has to call ViewWillDisappear when going full screen. However, since it wasn't I had to call it myself. However, it's not just about calling ViewWillDisappear, because Apple's first screen (the little half-screen for selecting the link option) will NOT call ViewWillAppear or ViewWillDisappear if the user cancels at this point. If they don't work and move on, even if they are canceled in the messaging screen that is displayed next, the VC's ViewWillAppear is called with ViewWillDisappear, which has never been called. Thus,to keep the same calls to ViewWillAppear and ViewWillDisappear (I have NSNotification objects that I don't want to register more than once), I use the code below.
[self viewWillDisappear:YES];
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:shareAray
applicationActivities:nil];
[self presentViewController:activityController
animated:YES completion:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed){
if (!activityType || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[self viewWillAppear:YES];
}
}];
As an explanation, the activityType
above will be NULL if the user cancels the first small half-screen. If they go to the next screen (which is full screen) it will contain the activityType even if they are canceled. So if they get canceled on the first little half screen, I need to make viewWillAppear be called differently, the normal process will automatically call viewWillAppear.
source to share
The method presentViewController
will be called viewWillAppear:
on the presented view , and then it viewWillDisappear:
will be called on the presented view when it is about to disappear. To intercept these calls, subclass UIActivityViewController
and implement overrides for these methods.
As for how to determine if a user is canceled or not, I would suggest doing it the way it is usually done for Windows Forms. Add a public "dialog result result" (it may display as get
-one) that is set to one of some value ranges, so the calling (or presenting) view controller can determine why the called (or self.presentedViewController
in this case) is rejected when it was done.
source to share