1

I have an iOS App similar to Photos App from Apple, which also has a ‘Share’ button for sharing photo, which worked well before. The code is as follows (for simplicity, I changed the sharing content to a string):

@objc func shareButtonTapped()
{
    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    self.present(vc, animated: true, completion: nil);
}

But when my iPhone was upgraded to iOS 15, the UIActivityViewController that is showed up was invisible. I attach an operation video: enter link description here Observe carefully, in fact, UIActivityViewController has a pop-up, but it has become almost transparent.

Then, I added a statement to deliberately set the background color of its view:

@objc func shareButtonTapped()
{
    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    vc.view.backgroundColor = UIColor.systemBackground;
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    self.present(vc, animated: true, completion: nil);
}

The operation video is as follows: enter link description here

This code is very simple and very standard. I don't know why this happens? Hope someone can help. Thanks in advance!


In fact, I define the shareButton within a class derived from UICollectionViewCell:

class AssetPreviewCell: UICollectionViewCell, UIScrollViewDelegate, PHLiveViewDelegate, UINavigationControllerDelegate
{
    //....
}

And the complete code is:

@objc func shareButtonTapped()
{
    guard let svc = self.findViewController() else { return }

    let vc = UIActivityViewController(activityItems: ["www.apple.com"], applicationActivities: nil);
    if let pop = vc.popoverPresentationController
    {
        pop.sourceView = someView;
        pop.sourceRect = shareButton.frame;
    }
    svc.present(vc, animated: true, completion: nil);
}

The func findViewController() is the method from enter link description here


Edit:

And I have an 'Albums' button next to the 'Share' button. When the 'Albums' button is tapped, I present another view controller for add current photo to albums or remove current photo from albums according user select or deselect. This view controller is presented quiet normally. The operation video is on enter link description here . So I think the problem is just from UIActivityViewController or something else.

mczmma
  • 119
  • 1
  • 10
  • Is `self` a proper `UIViewController` and you aren't trying to get Window/RootVC to show it? https://stackoverflow.com/questions/68387187/how-to-use-uiwindowscene-windows-on-ios-15 – Larme Oct 08 '21 at 13:21
  • Ok, so `findViewController` might be the culprit... I guess that if you try your initial code in the `UIViewController` showing the collectionView, you won't have an issue? Could you print `svc`? Is it the ViewController you think that should present the sharing? – Larme Oct 08 '21 at 14:19
  • @Larme , I print the svc, it's correct ViewController that it should be. – mczmma Oct 08 '21 at 15:12
  • And if you called `self.present(anActivityVC, animated: true)` directly inside that VC, does it work? – Larme Oct 09 '21 at 08:15
  • @Larme , I can't think out how to directly present inside the VC. And I don't think now the problem comes from svc, because there is a 'Album' button next to the 'Share' button. When the 'Album' button is tapped, I present another view controller. The operation video is https://youtu.be/3UPAARpTErM . So I think the problem is just from UIActivityViewController or something else. – mczmma Oct 09 '21 at 15:37
  • But it's not presenting a `UIActivityController`. Well, I didn't see related questions with an issue with `UIActivityController` in iOS15, so I'd suggest you try at least withouth all that `findViewController()` stuff to certify it's working elsewise... Look how to present a "default" activityController from that viewcontroller. You need to be sure it's not working there. – Larme Oct 09 '21 at 15:42
  • are you trying this on a device or simulator ? try both – Fattie Oct 09 '21 at 15:44
  • @Fattie , I test this on real devices. – mczmma Oct 09 '21 at 15:46
  • @Larme , the strangest thing is: I have another App which also has the 'Share' button and its codes are almost the same as this app (it was copied from this app), but the 'Share' button in the latter app works normally on iOS 13 to 15. – mczmma Oct 09 '21 at 15:52
  • So it's your implementation that is the culprit. Either `someView`, `shareButton.frame` (is `someView` the superview of `shareButton`?), or `findViewController()`. But at some point, it's up to you to debug. Personnally, I wouldn't rely on `findViewController()`, I would have use delegate or closure method to let know the viewcontroller that it need to present antoher viewcontroller... – Larme Oct 09 '21 at 15:59
  • 1
    @Fattie , I tested it on a simulator of iPhone 13 mini with iOS 15.0 just now, and got the same error. – mczmma Oct 09 '21 at 16:00
  • @Larme , Originally the someView is the UIStackView that contains the 'Share' button. And I also suspected whether the problem came from the someView, and tried some other views. And on iPhone, the settings of pop is unused, aren't them? – mczmma Oct 09 '21 at 16:11
  • They should be. Maybe iOS15 allow them on some screen sizes? Comment them to test. Else it leaves `findViewController()`, and there you need to be able to fake a call on `self.present()` on the initial viewcontroller... I don't know, call a timer and show one like 5 seconds after appearing... That's basic code. – Larme Oct 09 '21 at 16:14
  • hi @Larme , I use your method to self.present() the activity view controller in viewDidAppear(), and get the same almost 'transparent' activity view controller. So? – mczmma Oct 14 '21 at 11:53
  • @Larme, the answer is found. :-) – mczmma Oct 27 '21 at 15:23
  • @Fattie , the answer is found. :-) – mczmma Oct 27 '21 at 15:24
  • I dont understand your solution. Can you please post it in the context of the above code? @mczmma – Matt Jenje Oct 27 '21 at 16:32

2 Answers2

0

I find the answer. The problem is caused because I overrided the viewDidLoad function of UIActivityViewController for some reason:

override open func viewDidLoad()
{
    super.viewDidLoad();
    a_var += 1;
}

This cause problem on iOS15 while works well on iOS14 or iOS13.

Now I override the viewDidAppear(_) instead and the problem dissappears:

override open func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated);
    a_var += 1;
}

ps: @mczmma is another account of mine. This account is restricted to ask question. I don't know the reason.

michaelma
  • 90
  • 2
  • 9
0

What worked for me in my use case was just to change the sourceView from

let shareAll = [textView.text]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)

to

let shareAll = [textView.text]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = textView
self.present(activityViewController, animated: true, completion: nil)

So that on iPad the share shows above the textView, and it just works normally on iPhone.

silversam
  • 124
  • 1
  • 5