I encounter this bug when using UIActivityViewController in order to create a simple UI for sharing assets in my app. This works well on ios 10 and above, but in ios 9 this annoy issue appears. I've already researched for solutions but didn't find any help. Could you guys please take a look?
My code is just simple as below:
NSString *url=#"http://itunes.apple.com/us/app/APPNAME/idXXXXXXXXX";
NSString * title =[NSString stringWithFormat:#"Download ECG app %# and get free reward points!",url];
NSArray* dataToShare = #[title];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeAirDrop];
[self presentViewController:activityViewController animated:YES completion:^{}];
In ios 9, it looks like THIS
Please try to update animated:NO when you present the Activity View, if the view displayed correctly with animated:NO then self.view.translatesAutoresizingMaskIntoConstraints = NO; should be the cause of this problem.
Related
Recently I discovered that it’s necessary to use Popover to display the ActivityViewController on iPad. I found this website as a main reference:
http://pinkstone.co.uk/how-to-share-things-with-a-uiactivityviewcontroller/
It’s perfectly explained, but I can’t make it work from my SpriteKit game. I double-checked with other examples and all seems to be in its place... but this crash on iPad anyways, without any meaningful message (on iPhone it works). I have no idea on what’s wrong. If somebody experienced the same, any clue will be very welcome!
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:#[twitterText] applicationActivities:nil];
UIViewController* viewController = self.view.window.rootViewController;
activityController.modalPresentationStyle = UIModalPresentationPopover;
[viewController presentViewController:activityController animated:YES completion:nil];
UIPopoverPresentationController *popController = [activityController popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionDown;
popController.sourceRect = CGRectMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame),200,200);
The solution was adding
popController.sourceView = self.view;
before the sourceRect. Thanks and kudos to Warren Burton.
I'm currently implementing an iOS app using objective-C, which has a function that users may share a file with other friend or other app (like upload the file to Dropbox, Google Drive, attach the file to Mail, share the file to Facebook Messenger, Whatsapp, via Bluetooth, etc).
Is there a native way to implement this share function that can detect all apps which allows sharing a file, while I don't need to do it one by one?
You want to use UIActivityViewController. Below is an example from NSHipster, which is probably the most comprehensive article I've seen on the subject. And Apple has some good documentation here.
NSString *string = ...;
NSURL *URL = ...;
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[string, URL]
applicationActivities:nil];
[navigationController presentViewController:activityViewController
animated:YES
completion:^{
// ...
}];
This provide full answer for iPhone and iPad devices
ViewController *contentViewController = [[ViewController alloc] init];
// Present the view controller using the popover style.
contentViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:contentViewController
animated:YES
completion:nil];
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController =[contentViewController popoverPresentationController];
presentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
presentationController.sourceView = self.view;
presentationController.sourceRect = self.view.frame;
UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9
I'm trying to use UIActivityController in my app. Unfortunately the size of the modal is far too large.
On the left, the modal I want, on the right, the modal I have :
And here is the code I use to display the modal when I click on the Share button :
NSString *textToShare = #"Hello !";
NSURL *myWebsite = [NSURL URLWithString:#"http://stackoverflow.com/"];
NSArray *objectsToShare = #[textToShare, myWebsite];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
// Email subject
[activityVC setValue:#"Hello !" forKey:#"subject"];
[self presentViewController:activityVC animated:YES completion:nil];
What am I doing wrong ?
Thanks in advance.
You're doing nothing wrong, you're just using a differently sized device! Try running in the simulator using iPhone 6 or iPhone 6S and I bet you'll see exactly what you want on the left. Looks like you're just running it on a smaller device than what you want.
I just got this bug report for me app...the activity view controller is suddenly this weird narrow shape whether I'm on an actual phone or the view controller.
This is happening with some plain vanilla code that hasn't been touched in months:
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[message] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
What could be going wrong? I can't even think of where to start troubleshooting this one.
Have you tried reproducing this bug??? If we don't have the exact scenario of how this bug is getting created then we can't make a solution for it!!! Try updating the items in the array and check if the bug still exist... This type of things happens sometimes but until and unless we don't reproduce this scenario it can't be termed as bug.
Any way if you are working in universal app add below line of code before presenting the ActivityView
ActivityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
I will suggest, instead of looking for a solution dig out the problem first. I have never seen such weird behaviour of ActivityView before this and if we know why this happened then it will be helpful to every iOS developer.
If you are on iPad, try setting the popoverPresentationController property of sourceRect
NSString *string = NSLocalizedString(#"shareString", nil);
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[string] applicationActivities:nil];
activityVC.popoverPresentationController.sourceView = self.view;
activityVC.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height-50, 1.0, 1.0);
[self presentViewController:activityVC animated:YES completion:nil];
In some cases it may happen.
NSArray *Items = [NSArray arrayWithObjects:
#"Checking Test App", nil];
UIActivityViewController *activity=[[UIActivityViewController alloc]initWithActivityItems:Items applicationActivities:nil];
[self presentViewController:activity animated:YES completion:nil];
or
NSString *string = NSLocalizedString(#"shareString", nil);
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:#[string] applicationActivities:nil];
[activityViewController setCompletionWithItemsHandler:
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *error)
{
if ( completed ) {
NSLog(#"sharing complete");
} else
{
NSLog(#"cancelled");
}
}];
[self presentViewController:activityViewController animated:YES completion:^{
}];
You should check frames for your self's view. Probably its width less than the width of the activityViewController and you get this bug.
Maybe self.view.frame.size.width is the problem. You can find your the frame with NSLog self.view. Simple workaround could be:
[self.view.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];
These kinds of bugs do occur and they really test your patience and give you a challenge. Although I cannot give a definite answer, I can give you some tips on how to troubleshoot this!
What I suggest is that you first try to re-create this on your development machine. Then try to play around and see what's causing the issue. Here are some things to try out.
Run this on several devices with different OS's so that you might be able to determine a pattern.
Try and change the "initWithActivityItems" value(s) and see
whether the problem occurs.
See whether the issue exists if you try to create the
ActivityViewController from a different view controller too.
Go through your code and see if there are any warnings that you
have simply ignored. Specially if you're using Storyboards for
creating the view.
I know this is not an answer, but I cannot post such a long response as a comment.
Hope this helps!
In iPadOS you have to set the PopoverPresentationController's Source View and Source Rect properties. The following code will make it so the ActivityViewController comes from the center of the screen with the default size
For Swift 5
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width / 2.0, y: self.view.bounds.size.height-50, width: 1.0, height: 1.0)
I can reproduce this on iOS 12 when:
The controller that is presenting the activityViewController (self in this case) is itself presented with UIModalPresentationOverFullScreen
Somewhere in the presentation chain beneath self, there is another UIActivityAlertViewController.
The solution in my case is to switch self back to UIModalPresentationFullScreen.
We would need to set the sourceView and sourceRect (optional but may needed to be set for iPad).
We may try below snippet
activityViewController.popoverPresentationController?.sourceView = sender.self
activityViewController.popoverPresentationController?.sourceRect = sender.frame
I'am sharing a NSString and NSURL, using the native UIActivityViewController
The user is logged in into Facebook both in Settings and also in the Facebook App
but after iOS 8.3 i'am getting this error... moreover, on older devices, the dialog confirmation is obscured by the keyboard, making it impossible to confirm or decline
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:applicationActivities];
NSArray *excludeActivities = #[UIActivityTypeAssignToContact];
vc.excludedActivityTypes = excludeActivities;
if (IsUserInterfaceIdiomPad) {
vc.popoverPresentationController.sourceView = self.navigationController.view;
}
[self.navigationController presentViewController:vc animated:YES completion:^{
}];
Yes, this is a bug that went out in Version 29 of the Facebook application. We (Facebook) are working on a fix and hope that it will ship soon.