Search code examples
iosswiftipad

Shared text iPhone and iPad


I need to share text on iPhone and iPad. My code works for iPhone but does not work for iPad.

I am using a static function to call from trailingSwipeActionsConfigurationForRowAt options of my tableView:

static func dialogCompartir(titulo: String, descripcion: String, view: UIViewController, url: String) -> Void {
    // Set the link to share.
    if let link = NSURL(string: url) {
        let objectsToShare = [descripcion,link] as [Any]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        if UIDevice.current.userInterfaceIdiom == .pad {
            print("ipad")
            view.present(activityVC, animated: true, completion: nil)
            if let popOver = activityVC.popoverPresentationController {
                popOver.sourceView =  view.view
              //popOver.sourceRect =
              //popOver.barButtonItem
            }
        } else if UIDevice.current.userInterfaceIdiom == .phone {
            print("iphone")
            view.present(activityVC, animated: true, completion: nil)
        }
    }
}

iPad error:

[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:0x60000188f890 LPLinkView:0x7fdd2a70d200.leading == UILayoutGuide:0x6000002ac0e0'UIViewLayoutMarginsGuide'.leading   (active)>",
    "<NSLayoutConstraint:0x600001889d10 H:[LPLinkView:0x7fdd2a70d200]-(59)-|   (active, names: '|':_UIActivityContentTitleView:0x7fdd2a70cde0 )>",
    "<NSLayoutConstraint:0x6000018e50e0 H:|-(0)-[_UIActivityContentTitleView:0x7fdd2a70cde0]   (active, names: '|':_UINavigationBarContentView:0x7fdd27dae070 )>",
    "<NSLayoutConstraint:0x6000018e5590 _UIActivityContentTitleView:0x7fdd2a70cde0.trailing == _UINavigationBarContentView:0x7fdd27dae070.trailing   (active)>",
    "<NSLayoutConstraint:0x600001893520 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7fdd27dae070.width == 0   (active)>",
    "<NSLayoutConstraint:0x60000188f840 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x6000002ac0e0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIActivityContentTitleView:0x7fdd2a70cde0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000188f890 LPLinkView:0x7fdd2a70d200.leading == UILayoutGuide:0x6000002ac0e0'UIViewLayoutMarginsGuide'.leading   (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.

How can I solve this problem of sharing text on iPad for Xcode 11, iOS 13?

My TableViewController has no constraint warnings.


Solution

  • This has worked for me in iPad, xcode11, ios13:

            if UIDevice.current.userInterfaceIdiom == .phone{
                print("===== iPhone")
                view.present(activityVC, animated: true, completion: nil)
    
            }else if UIDevice.current.userInterfaceIdiom == .pad{
                print("===== iPad")
                view.present(activityVC, animated: true, completion: nil)
    
                if let popOver = activityVC.popoverPresentationController {
                    popOver.sourceView = view.tableView
                    popOver.sourceRect = view.tableView.rectForRow(at: indexPath)
                    popOver.permittedArrowDirections = UIPopoverArrowDirection.any
    
                }
    
            }
    

    Greetings