10

I share some contents using the following code

var textToShare = "Test"
let activityVC = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact]

presentViewController(activityVC, animated: true, completion: nil)

But when I press the cancel button or when the content is shared successfully, the App shows a blank screen.

How to fix this issue ?

UPDATE:

The blank screen just appears when I select mail or sms apps for the sharing target, For Telegram, Twitter and Facebook it is working perfect.

I commented all the code inside lifecycle methods, Still same issue.

override func viewDidAppear(animated: Bool)
{
    //setControlsAreHidden(true)
}

override func viewWillAppear(animated: Bool)
{
    //if dataAddedToView
    //{
    //     activityIndicator?.removeFromSuperview()
    //}

}

override func viewWillDisappear(animated: Bool)
{
    //setControlsAreHidden(false)
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • this demo project may help you: https://github.com/genedelisa/ActivityDemo – Dharmesh Kheni Aug 18 '15 at 06:20
  • I've checked your code, and in my project it works correctly, I think maybe problem can be somewhere in `viewWillAppear` of your presenting view controller. Can you share more code? – Vitalii Gozhenko Aug 22 '15 at 12:13
  • Yes I think the problem is in code that you're not showing. Code in the presenting view controller would relevant. – rholmes Aug 23 '15 at 11:51
  • are you doing this on the main thread? could you show us what code you have at obvious points in the presentation lifecycle? –  Aug 23 '15 at 12:09
  • @VitaliyGozhenko I don't have any code inside viewWillAppear – Morteza Soleimani Aug 23 '15 at 12:12
  • @robdashnash Check the updated question, I don't have any code inside lifecycle methods – Morteza Soleimani Aug 23 '15 at 12:15
  • Please show your code for your `completionWithItemsHandler` and dismissing the activity controller. – NRitH Aug 27 '15 at 19:56
  • Better if you can share the code with dropbox for now. – MobileGeek Aug 28 '15 at 06:39
  • Just throwing it out there, but your overrides should be calling the super method. e.g. `override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) }`. Some base view controllers do work in these functions. – Brian Nickel Aug 28 '15 at 16:13
  • Hello, is it possible to reproduce this problem in 2022? My team is afraid of using Share Activity because they had this problem in the past. Resulting in much inferior user experience. – unobatbayar Jan 24 '23 at 02:30

5 Answers5

2

As you are presenting MFMailComposeViewController and MFMessageComposeViewController above your current UIViewController so when you dismiss the MFMailComposeViewController or MFMessageComposeViewController after sending the message then your UIViewController viewWillAppear method will be called as the view is appearing again, so in my opinion check in viewWillAppear or viewWillDisappear method whether you are making your view nil.

Rajat
  • 10,977
  • 3
  • 38
  • 55
2

I was having this same problem, and was able to solve it by adjust the sizing constraints of my primary UIView.

Code before:

override func loadView() {
    self.view                                           = UIView()
    self.view.translatesAutoresizingMaskIntoConstraints = false
}

Code after:

override func loadView() {
    self.view                                           = UIView()
}

So just remove self.view.translatesAutoresizingMaskIntoConstraints = false

This might not apply to everyone, but based on other peoples' answers, I gather there are various issues with how views are displayed which can cause the same problem. If my fix doesn't work for you, I suggest commenting out all unnecessary code until the problem goes away, and then methodically bring small bits back online, testing along the way to identify your issue.

Dave Blue
  • 141
  • 4
1

You need to first check that your view controller is not being destroyed while you're presenting content. Some debug statements in the presenting view controller's lifecycle management routines (e.g., ViewWillDisappear) could help.

If the problem stems from the ViewController disappearing, you'll need to recreate it appropriately.

Also look at any place where you provide content: is it being triggered to draw at the right times?

Additionally, ensure you're using APIs as required (e.g., calling the superclass in all methods where it's required, such as the view controller lifecycle routines). See:

These are the areas I would focus my attention on and/or provide the relevant code for.

Community
  • 1
  • 1
rholmes
  • 4,064
  • 3
  • 25
  • 34
1

I don't see any calls to the super implementation in your viewDidAppear, viewWillAppear or viewWillDisappear methods.

What happens if you do?:

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)
    //setControlsAreHidden(true)
}

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)
    //if dataAddedToView
    //{
    //     activityIndicator?.removeFromSuperview()
    //}

}

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(animated)
    //setControlsAreHidden(false)
}
fdiaz
  • 2,600
  • 21
  • 27
1

Maybe you're not presenting the activity view controller in the proper view, try something like this:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
   activityViewController.popoverPresentationController.sourceView = self.view;
}

It's a objective C code, but just as example of how to set the view.

If you need how to check the iOS version:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
Pablo A.
  • 2,042
  • 1
  • 17
  • 27