1

I had added share extension to upload file.

But i want to stop open share extension when user is not logged in application and show alert similar like Messenger application from facebook.

Facebook messenger app

How can i do this Note : I know how to do check is User logged in or not using app groups. But I want to check before open the share extension and show alert. in my case its first open the share extension and then i am showing alert. I want to check before open the share extension

SShah
  • 243
  • 2
  • 17

2 Answers2

2

Rather than directly open your custom view for share extension you could use alert first to check if user logged in or not, then if user has logged in, you can proceed to present your custom view with animation.

you can do this by adding below method on ShareViewController: SLComposeServiceViewController.

override func viewDidLoad() {
    // check if user logged in or not here and if not below code will be executed.
    self.loginAlertController = UIAlertController(title: "Please launch application from the home screen before continuing.", message: nil, preferredStyle: .alert)
    let onOk = UIAlertAction(title: "OK", style: .destructive) { alert in
        self.extensionContext?.cancelRequest(withError: NSError(domain: "loging", code: 0, userInfo: nil))
    }
    loginAlertController!.addAction(onOk)
    present(loginAlertController!, animated: true, completion: nil)
}
Sharad Paghadal
  • 2,089
  • 15
  • 29
  • Though I use this, the default UI is still showing up in the background. – Vineel Jul 13 '20 at 07:07
  • Default UI will be definitely in background, To prevent that, make another empty SLComposeServiceViewController and do this login check there. After that if user is logged in - you can redirect to your UI. – Sharad Paghadal Jul 14 '20 at 03:16
  • 1
    What do you mean by "Make another EMPTY SLComposeServiceViewController". The above code is that empty SLComposeServiceViewController which takes the user to an AlertView Controller. – Vineel Jul 14 '20 at 03:31
  • Is this allowed by Apple? – daniel May 14 '23 at 03:13
1

You should make use of App groups to communicate between the main app and extension app.(Sharing Data: NSUserDefaults and App Groups )

You can add data in main app like this :-

let mySharedDefaults = UserDefaults(suiteName: "group.yourValue")
     mySharedDefaults?.set(false, forKey: "isLoggedIn")

Then you can get data like this in your extension

   let mySharedDefaults = UserDefaults(suiteName: "group.yourValue")
         if let isLoggedIn = mySharedDefaults?.value(forKey: "isLoggedIn") as? Bool {
            if !isLoggedIn {
                     showAlert()
           }
}

Refer this for implementing app groups

Anuraj
  • 1,242
  • 10
  • 25
  • I know how to do check is User logged in or not. But I want to check before open the share extension and show alert – SShah Jul 26 '18 at 05:31
  • Your application's module must load at least onto memory for getting data about user login status. So before opening share extension, you can't get your application data related to user login status. – Sharad Paghadal Jul 26 '18 at 05:38
  • @SharadPaghadal have you seend screenshot i had added in question. How they do it.. – SShah Jul 26 '18 at 05:43
  • i'm posting an answer – Sharad Paghadal Jul 26 '18 at 05:45
  • @SShah Setting the background to clear color and showing the alert would resolve the issue.(WORK AROUND). – Anuraj Jul 26 '18 at 11:23
  • From Apple's documentation…  `synchronize()` _"… this method is unnecessary and shouldn't be used."_ https://developer.apple.com/documentation/foundation/userdefaults/1414005-synchronize – Ashley Mills Aug 09 '18 at 06:35
  • 1
    @AshleyMills Thanks .I have made the change. – Anuraj Aug 09 '18 at 07:03