Popover crashing on iPad (SpriteKit game) - ios

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.

Related

UIActivityViewController view frame is cut in ios 9.0

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.

UIActivityViewController Style in iOS10

I am new to ios and I have a problem on displaying UIActivityViewController. I have done the code and it works fine on ios8&9 iPad, but when I tested on ios10 iPad 5th and the UIActivityViewController frame was restricted by its parent view controller.
Problem need to be fixed:
The desired layout:
UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
vc.excludedActivityTypes = #[UIActivityTypeMessage];
vc.popoverPresentationController.barButtonItem = self.shareButton;
vc.popoverPresentationController.permittedArrowDirections =
UIPopoverArrowDirectionAny;
vc.popoverPresentationController.delegate = self;
vc.modalPresentationStyle = UIModalPresentationPopover;
Can anyone give me some hints on that? Thanks in advance.

iOS native way to share files to any possible apps

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

UIActivityViewController has a weird size and shape

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

Display UIActionSheet in iphone the same as ipad

I have a a UIButton, and I would like to add a UIActionSheet.
How can I make the iphone version look like so:
If there is a way of doing it another way, meaning not with UIActionSheet, I'm open to hear other ways.
Hope I was clear enough. If you have questions, please feel free to ask.
You can do this in a native way in iOS8 only with the following
UIPopoverPresentationController *popOverController = [[UIPopoverPresentationController alloc] init];
popOverController.popoverContentSize = CGSizeMake(150, 160);
[popOverController setDelegate:self];
popOverController.sourceView = self.view;
popOverController.sourceRect = sender.frame;
popOverController.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:popOverController
animated:YES
completion:nil];
you can use one of custom controls from cocoacontrols for iOS7 support
Good luck

Resources