0

It looks like the Share Extension allows other "host" apps to share content with my "containing" app. How do I share content from my app with other apps using the standard iOS share sheet? I have found information on how to use UISheetPresentationController and UIActivityViewController. What is Apple's designated standard API to share content from my iOS app? I would think their designated way would show all the the apps that the share sheet can share with. Is UIActivityViewController the way that is most intended by Apple to do this?

This article looks like a nice simple tutorial: UIActivityViewController by example, but when I try the first segment of code that it teaches, it doesn't work like the article says it should.

The most significant message in the debug window is the part in the first line that says "couldn't find entitlement CopresenceCore.Entitlement.publicAPI error nil". I can't find information about what entitlement that would be. I take it I need to have an entitlement file in my project for that entitlement.

I am using an iPhone 8 device with iOS 15.6.1 in an Xcode project set for iOS Deployment Target 13.0.

I works on an iPhone 5s device with iOS 12.5.6 in a separate Xcode project set for iOS Deployment Target 11.0.

The print statement that prints "presentation done" shows in the debug window in both cases.

Here's my code:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let stringItem = "Hello there."
        
        let activityViewController = UIActivityViewController(activityItems: [stringItem], applicationActivities: nil)
        
        self.present(activityViewController, animated: true) {
            print("presentation done")
        }
        
    }
}

Here's the debug window:

2022-10-20 21:08:10.536367-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [Default] Task TrialUIActivityV[34244]/1#3 LF=0 couldn't find entitlement CopresenceCore.Entitlement.publicAPI error nil
2022-10-20 21:08:11.266049-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [Default] Task TrialUIActivityV[34244]/1#3 LF=0 couldn't find entitlement CopresenceCore.Entitlement.publicAPI error nil
2022-10-20 21:08:12.000344-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-10-20 21:08:12.001351-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] Attempt to map database failed: permission was denied. This attempt will not be retried.
2022-10-20 21:08:12.001421-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-10-20 21:08:12.062430-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] -imageForImageDescriptor: can do IO please adopt -imageForDescriptor: for IO free drawing or -prepareImageForDescriptor: if IO is allowed. (This will become a fault soon.)
2022-10-20 21:08:12.199153-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-10-20 21:08:12.199262-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] Attempt to map database failed: permission was denied. This attempt will not be retried.
2022-10-20 21:08:12.199340-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-10-20 21:08:12.199640-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [default] -imageForImageDescriptor: can do IO please adopt -imageForDescriptor: for IO free drawing or -prepareImageForDescriptor: if IO is allowed. (This will become a fault soon.)
2022-10-20 21:08:12.240705-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionReusableView that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIActivityContentFooterView: 0x131d15bc0; baseClass = UICollectionReusableView; frame = (16 474.5; 343 52); layer = <CALayer: 0x281180420>>
2022-10-20 21:08:13.864915-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x283245f90 UIStackView:0x131d56460.centerY == UIView:0x131d54de0.centerY   (active)>",
    "<NSLayoutConstraint:0x283246490 _UIActivityActionCellTitleLabel:0x131d55cc0.height >= 54.5   (active)>",
    "<NSLayoutConstraint:0x283245ea0 V:|-(>=4)-[UIStackView:0x131d56460]   (active, names: '|':UIView:0x131d54de0 )>",
    "<NSLayoutConstraint:0x283246850 'UISV-canvas-connection' UIStackView:0x131d56460.top == _UIActivityActionCellTitleLabel:0x131d55cc0.top   (active)>",
    "<NSLayoutConstraint:0x2832468a0 'UISV-canvas-connection' V:[_UIActivityActionCellTitleLabel:0x131d55cc0]-(0)-|   (active, names: '|':UIStackView:0x131d56460 )>",
    "<NSLayoutConstraint:0x28324e5d0 'UIView-Encapsulated-Layout-Height' UIView:0x131d54de0.height == 30   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283246490 _UIActivityActionCellTitleLabel:0x131d55cc0.height >= 54.5   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
presentation done
2022-10-20 21:08:18.385209-0500 TrialUIActivityViewControllerHackingWithSwiftPaulHudson[34244:1256968] [TraitCollection] Class CKBrowserSwitcherViewController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.
daniel
  • 1,446
  • 3
  • 29
  • 65
  • Yes, this is the purpose of `UIActivityViewController`. Have you tried it yet? – Rob Oct 21 '22 at 01:54
  • @Rob I'm trying it right now. I found this article [UIActivityViewController by example][1] but I'm getting an error. I updated my post with it. [1]: https://www.hackingwithswift.com/articles/118/uiactivityviewcontroller-by-example – daniel Oct 21 '22 at 02:11
  • *"...it doesn't work like the article says it should"* -- When I run the code you posted... on old iPhone 7 / iOS 13.3, it works fine. On iPhone 11 Pro / iOS 15.6.1, it also works fine -- and prints lots of warnings in the debug console. These all appear to be internal UIKit / OS warnings that we cannot avoid. You see your `print("presentation done")` because that line executes when the `activityViewController` ***has been presented*** ... not when it ***has been dismissed***. – DonMag Oct 21 '22 at 16:16
  • daniel, ignore all this kruft in the console for a second: what happens on the device? Do you see the share UI? Also, are you doing this within an existing app? So so, try it again in a blank, stand-alone project. – Rob Oct 21 '22 at 17:29
  • @Rob I am running the code in brand new clean Xcode projects I created just to try out the code. The code that doesn't work just shows a black screen. I take it that is the ViewController Screen. I had code that set the backgroundColor of the UIActivityViewController to magenta, and the iPhone 5s device showed that. I actually realized what I really need is for UICloudSharingController to see my app so that it would allow the user to share the link to my app directly. That means my Share Extension of my app needs the correct NSExtensionActivationRule and possibly other settings. – daniel Oct 21 '22 at 18:37
  • @Rob Here's my new post to learn to do what I really need to do: [How to make my iOS app appear in UICloudSharingController so that it could share a CKShare.url with my app to other users of my app?](https://stackoverflow.com/q/74155136/6657172) – daniel Oct 21 '22 at 18:39
  • OK, that cloud sharing is a separate question. But the code in this particular question worked fine for me. I put it in an `@IBAction`, hooked up to a button, and it works fine. If you have troubles, you need to give us enough code for us to reproduce your problem. The above is not sufficient. I'd suggest that in your other question, you [provide enough for someone to reproduce the problem you are having](https://stackoverflow.com/help/minimal-reproducible-example). – Rob Oct 21 '22 at 19:22
  • @Rob The code I put in the post is in a totally new Xcode project. The only thing I changed was the iOS Deployment Target. I changed it 13.0 - as low as I can without having to change too much code, since SceneDelegate requires at least 13.0. The code in my post is in viewDidAppear(_:). I excluded code for a URL object and code that changed the activity view controller's background color. That's absolutely all the code. I tried the bare minimum code. I'll update the code in the post. – daniel Oct 22 '22 at 10:43

0 Answers0