Lack of a close button on UIActivityViewController - ios

I am currently doing some updates for an iOS application. For the update I am going to take advantage of iOS6 capabilities. I am using a UIActivityViewController to get this done as well as some custom UIActivity activities. The main issue I am having is that there is no "Cancel" or "Close" button. The only way you can exit out of the activity view is if you either post something to a social network, or act like you are going to and then cancel.
NSArray* dataToShare = [[NSArray alloc] initWithObjects:#"blah", nil];
// Custom activities are allocated here
NSArray* customActivities = [[NSArray alloc] initWithObjects:activities, nil];
NSArray* excludedActivities = [[NSArray alloc] initWithObjects:exclusions, nil];
activityController = [[UIActivityViewController alloc] initWithActivityItems:formatArray applicationActivities:customActivities];
activityController.excludedActivityTypes = excludedActivities;
activityController.modalPresentationStyle = UIModalPresentationFormSheet;
[[UIApplication sharedApplication].keyWindow addSubview:self.view];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:activityController.view];
[self presentViewController:activityController animated:YES completion:^{ closureCode
}];
This does everything that I need inside of the activity controller in terms of networking content, I just don't have a close button. I don't know if it is the same issue but when the view disappears if I try to bring one up my "Share" menu again it tells me
<UIActivityViewController: 0x1e0db7d0> on <ShareDelegate: 0x1e048490> which is already presenting <UIActivityViewController: 0x21b1d4f0>
Thoughts? Thanks in advance!
*I am using:
iPad 2
Objective-C/C++
XCode 4.5
iOS6

From the docs for UIActivityViewController (emphasis is mine):
When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.
On the iPad, the popover will be cancelled by the user tapping outside of the popover.
On the iPhone/iPod touch, the modal view controller will be shown with a cancel button.
In short, don't use a form sheet on the iPad.

Related

Facebook UIActivityViewController is Missing the Cancel and Post Buttons

When my app opens the Facebook UIActivityViewController, there is no navigation bar at the top of the Facebook screen and there is no Cancel or Post button - the only way to exit the screen is to kill the app. Other apps that I look at have an additional navigation bar at the top of the Facebook screen, which holds the Cancel and Post buttons.
Here is the code I am using:
NSURL *url = [NSURL URLWithString:#"http://www.mywebsite.com"];
NSArray *activityItems = #[url];
// Put together the UIActivityViewController
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAirDrop,
UIActivityTypePostToVimeo,
UIActivityTypePostToFlickr,
UIActivityTypeAddToReadingList];
// Present the UIActivityViewController
[self presentViewController:activityVC
animated:YES
completion:nil];
The Twitter, Email, and SMS screens all appear as expected. Facebook is the only one that is having problems.
Some other notes: I notice that when I open the Facebook Share on this app, the status bar turn black with white text. On another test app that I created, the status bar looks grayed-out with black text. Not sure why / what this points to, but might be a clue.
This problem seems to be app-wide, as I have 3 spots where sharing can be invoked, and it is happening in all 3 instances.
Attaching an image. There should be a Navigation bar above the "To: Public" toolbar.
Any ideas would be appreciated.
You can hide the navigation bar as per your requirement.
So, add the below code, if you want to show the navigation when UIActivityViewController is present and then hide it when UIActivityViewController is dismissed:-
//this will be called when the UIActivityViewController will be dismissed, so we are hiding the navigation
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
[[UINavigationBar appearance] setHidden:YES];
}];
//this will be called when the UIActivityViewController will be shown, so we are enabling the navigation mean unhiding it.
[self presentViewController:activityVC animated:YES completion:^{
[[UINavigationBar appearance] setHidden:NO]
//you can also add code to customize status bar
}];
I have created new app and copy pasted your code on button action. It is workiing perfectly. You can check image of it over here: https://drive.google.com/file/d/0B0FNcjA1N299NWdmZGZQWC1KbzA/view?usp=sharing
As per my knowledge, If you have done something with [UINavigationBar appearance] in your app, then and then only it makes the problem. Please check that.

Navigation bar not displayed when view controller is presented as a popover on iPad (versus push on iPhone when it is displayed)

I am converting an iPhone App to an iPad App. I wish to present several of the view controllers as popovers that were previously pushed on the iPhone by changing the segue type in the storyboard from push to popover. The view controllers have a title, a done button, and a cancel button in the navigation bar. Now, when I run the iPad App the popover appears but without the navigation bar. How can I get the navigation bar to show. I have already tried expected setting it not hidden in viewWillAppear which did not work.
I have been able to change the segue to an IBAction with the following code and got it to work. It seems somehow the popover does not have a navigation controller so I created one. `
// Device is iPad
addNoteVC = [self.storyboard instantiateViewControllerWithIdentifier:#"addNoteVC"];
popoverNav = [[UINavigationController alloc] initWithRootViewController:addNoteVC];
[addNoteVC setManagedObjectContext:self.managedObjectContext];
[popoverNav.navigationBar setTintColor:[UIColor darkTextColor]];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor darkTextColor],NSForegroundColorAttributeName,nil];
[popoverNav.navigationBar setTitleTextAttributes:textAttributes];
[addNoteVC setSelectedClient:selectedClient];
[addNoteVC setAddNoteTableViewControllerDelegate:self];
popover = [[UIPopoverController alloc] initWithContentViewController: popoverNav];
[popover setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
popover.delegate = self;
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
`
While the above code works it creates significant differences from the iPhone App code. I would rather that the iPhone and iPad stayed the same as much as possible. If adding a another navigation controller for the popover is the only solution I would prefer to do this in a custom segue so the view controller code can stay the same. In addition that approach would have the economy of one custom segue instead of changes for every view controller presented as a popover. If that indeed is the best solution, details on the custom segue would be appreciated and accepted.

How to know when a View Controller unwinds when it´s presented programmatically

I got this code that brings up the native "share" view where the user can post to facebook/twitter etc... There is a completion block, but this only get´s called when the VC shows itself, I need to know when it dismisses. Cause my app has different View Controllers for landscape / portrait mode, and I do not want to dismiss the view if the user rotates and the UIACtivityViewController is on screen.
I send a notification when the shared button is pressed to not dismiss the current view if user rotates the device. All i need now, is to know when it´s dismissed so I can reenable the function
- (IBAction)shareButtonPressed:(UIButton *)sender {
// Notify that another view is on screen to allow rotation without view disapearing.
[self sendNotificationWithName:#"landscapeViewHasPopupActive" andObject:#"empty string"];
NSString *message = #"Hello World!";
UIImage *imageToShare = [UIImage imageNamed:#"Icon.png"];
NSArray *postItems = #[message, imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:postItems
applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:^() {
}];
// Is showing landscape set to NO, and YES when this view disapears
}
In ios6 storyboards, there is a thing called an unwind segue. Add a method to the presenting controller to verify if unwind can/will happen. Check with google.
The same view controller that called the – presentViewController:animated:completion: method has it's counterpart: the – dismissViewControllerAnimated:completion: method.
When you want to dismiss the activityVC controller call the – dismissViewControllerAnimated:completion: method. Use the 'completion' block to execute the code you want when the view controller is dismissed.
Hope this helps!

UIActivityViewController inside a Container View

I use a UIActivityViewController inside a Container View. I want to share some text by using Email, twitter and facebook. While the last two works perfectly i have a problem with Email. The problem is that the composer view doesn't dismisses either by canceling the event nor by trying to send it! When the ActivityView appears i get the following message:
Launch Services: Registering unknown app identifier com.apple.mobilemail failed
Launch Services: Unable to find app identifier com.apple.mobilemail
This is strange since there is really no problem with the app identifier and i can share text with email, using ActivityViewController in other view controllers (when not in a container view).
My code, below:
- (void)openBtnTouched {
NSString *alertTitleString = [[[GlobalVariables sharedInstance].alertsArray objectAtIndex:self.selectedIndex]objectForKey:#"alertTitle"];
NSString *alertMsgString = [[[GlobalVariables sharedInstance].alertsArray objectAtIndex:self.selectedIndex]objectForKey:#"alertMessage"];
UIActivityViewController *activity;
activity = [[UIActivityViewController alloc] initWithActivityItems:#[alertTitleString,alertMsgString] applicationActivities:nil];
activity.excludedActivityTypes = #[
UIActivityTypeMessage,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard];
activity.completionHandler = ^(NSString *activityType, BOOL completed){
NSLog(#"Activity Type selected: %#", activityType);
if (completed) {
NSLog(#"Selected activity was performed.");
} else {
if (activityType == NULL) {
NSLog(#"User dismissed the view controller without making a selection.");
} else {
NSLog(#"Activity was not performed.");
}
}
};
[self presentViewController:activity animated:YES completion:NULL];
}
I'm not sure whether your app is designed for iPhone, iPad, or both, but Apple are very specific about how to display a UIActivityViewController. From their documentation:
When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.
If you're displaying it in some other way (i.e, in a container view controller) you may well encounter this sort of weird behavior. I'd recommend you display the activity view controller as recommended by Apple.

UIActivity activityViewController being presented modally on iPad instead of in popover

When using a customer UIActivity subclass in iOS 6, it's possible to specify a custom view controller that will be displayed when your action is chosen from the initial UIActionViewController's view. You do this by returning a reference to a custom view controller from your UIActivity subclass's activityViewController method.
According to the UIActivity class reference:
activityViewController
The default implementation of this method returns nil. Subclasses that provide additional UI using a view controller can override this method to return that view controller. If this method returns a valid object, the system presents the returned view controller for you, instead of calling the performActivity method. On iPad, your view controller is presented inside of a popover. On iPhone and iPod touch, your view controller is presented modally.
Your custom view controller should provide a view with your custom UI and should handle any user interactions inside those views. Upon completing the activity, do not dismiss the view controller yourself. Instead, call the activityDidFinish: method and let the system dismiss it for you.
Note that bit at the end of the first paragraph: On iPad, your view controller is presented inside of a popover. On iPhone and iPod touch, your view controller is presented modally.
However, on iPad the view controller returned by activityViewController always displays modally, no matter how I present the UIActivityViewController (either modally or via a popover). When presenting via a popover, it causes it to crash since it doesn't think it's been dismissed.
What am I doing wrong? Is this a bug in iOS 6?
Update: here's a simple Xcode project that illustrates the problem. Feel free to clone it and play around to see if you can see where we're going wrong: github.com/simonwhitaker/GSActivityDemo
As we are talking about the UIActivityViewController, which is the view showing the available activities to the user. Apple state the following...
Your app is responsible for configuring, presenting, and dismissing this view controller. Configuration for the view controller involves specifying the data objects on which the view controller should act. (You can also specify the list of custom services your app supports.) When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.
I took the last line as a sign that you have to handle how the view is presented, so I check whether the code is running on iPad and use a UIPopover accordingly. As you can sere here... https://github.com/bufferapp/buffer-uiactivity/blob/master/BufferUIActivity/Views/FirstViewController.m within the following method.
-(IBAction)openUIActivityView:(id)sender {
NSString *text = #"Hello world";
NSString *url = #"http://bufferapp.com";
NSArray *activityItems = #[text, url];
BufferUIActivity *bufferActivity = [[BufferUIActivity alloc] init];
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:#[ bufferActivity ]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self presentViewController:activityView animated:YES completion:^{
}];
} else {
// Change Rect to position Popover
self.popup = [[UIPopoverController alloc] initWithContentViewController:activityView];
[self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.width/2, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
I think the issue with the activity view controller not showing in a popover is a bug and the docs reflect the correct intent. However, I don’t know of a way to workaround this atm.
The part about dismissing the view controller, however, is a different issue. You are not supposed to dismiss the view controller that you return from -[UIActivity activityViewController], but you are responsible for dismissing the popover that you have presented, which in turn will also remove your custom view controller from the hierarchy. (And because it works this way, I’m inclined to believe that the custom view controller would normally have to be shown in the popover.)
Here’s an example with code from your example app:
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
vc.completionHandler = ^(NSString *activityType, BOOL completed){
[self.activityPopoverController dismissPopoverAnimated:YES];
};
I had the same problem in iOS 7. The solution to show the custom view in the popover is to create and show it in the -(void)performActivity method instead of returning it in -(UIViewController *)activityViewController.
You can see example code in my question/answer under this link:
iOS 7 custom UIActivity as popover
I just had the same problem but solved it by setting the ViewController to:
[yourViewController setModalPresentationStyle:UIModalPresentationPageSheet];
in
- (UIViewController *)activityViewController
hope this helps

Resources