Tushar Lathiya
Tushar Lathiya

Reputation: 1038

How to Just launch app from share extension without post popup in Swift?

I am a begginer in iOS app development and I would like to launch my app on copy link from another app. added shared extension on click it is displaying popup. But my requirement is it should not display popup and directly open my app on click of my shared extension.

What I did:

1) Added rules in info.plist

<dict>
    <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
    <integer>1</integer>
</dict>

Screen short :

enter image description here Please someone help me to fix this issue. Thanks

Update : After adding below code popup is not coming but my app is not opening

objective C :

    - (BOOL)isContentValid {
    return YES;
}
#ifdef HIDE_POST_DIALOG
- ( NSArray * ) configurationItems
{
    return @[];
}
#endif

- ( void ) didSelectPost
{
#ifdef HIDE_POST_DIALOG
    return;
#endif
}

CGFloat m_oldAlpha = 1.0;
#ifdef HIDE_POST_DIALOG
- ( void ) willMoveToParentViewController: ( UIViewController * ) parent
{
    m_oldAlpha = [ self.view alpha ];
    [ self.view setAlpha: 0.0 ];
}
#endif

#ifdef HIDE_POST_DIALOG
- ( void ) didMoveToParentViewController: ( UIViewController * ) parent
{
    // Restore the original transparency:
    [ self.view setAlpha: m_oldAlpha ];
}
#endif



#ifdef HIDE_POST_DIALOG
- ( id ) init
{
    if ( self = [ super init ] )
    {
        [ [ NSNotificationCenter defaultCenter ] addObserver: self selector: @selector( keyboardWillShow: ) name: UIKeyboardWillShowNotification    object: nil ];
    }

    return self;
}
#endif

#ifdef HIDE_POST_DIALOG
- ( void ) keyboardWillShow: ( NSNotification * ) note
{
    [ self.view endEditing: true ];
}
#endif

Swift :

override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here
        return true
}
override func didSelectPost() {
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }
override func viewDidAppear(_ animated: Bool) {
    self.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.size.height)

    UIView.animate(withDuration: 0.25, animations: { () -> Void in
        self.view.transform = .identity
    })
}

Upvotes: 16

Views: 3319

Answers (5)

Mobias
Mobias

Reputation: 31

If someone got any problems how to hide this pop up window, I had the SLComposeServiceViewController type in my VC. Remove this type and add the normal UIViewController and the Pop up Window doesn't pop up anymore.

Make also sure create a new VC in the storyboard and change the initial VC to it.

Upvotes: 1

Lenka Pitonakova
Lenka Pitonakova

Reputation: 1106

Do this from viewDidLoad, but you need to set a small timer, otherwise it won't work:

override func viewDidLoad() {
    super.viewDidLoad()
    Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(self.didSelectPost), userInfo: nil, repeats: false)
  }

Upvotes: 4

ha100
ha100

Reputation: 1592

calling the completion on request from viewDidLoad should be sufficient

override func viewDidLoad() {

    super.viewDidLoad()
    ⋮
    // custom logic
    ⋮
    self.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
}

Upvotes: 2

isHidden
isHidden

Reputation: 860

If you want to hide the dialog, you need to process data in viewDidLoad instead of didSelectPost, after that to redirect your app use this code:

           self.dismiss(animated: false, completion: {
               let _ = responder?.perform(selectorOpenURL, with: url)
               self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
            })

Be sure that you set all flags and url scheme.

Upvotes: 1

726a
726a

Reputation: 89

Add this in your share view controller. You're welcome.

override func viewDidAppear(_ animated: Bool) {
    self.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.size.height)

    UIView.animate(withDuration: 0.25, animations: { () -> Void in
        self.view.transform = .identity
    })
}

Upvotes: -3

Related Questions