Swift 3 Photo Share Extension with Firebase Database Not Working After First Send

295 views
Skip to first unread message

Emma Yang

unread,
Oct 14, 2017, 11:56:40 PM10/14/17
to Firebase Google Group

I am developing an iOS photo sharing extension in Swift 3 that captures a user-selected photo in the iOS Photos app along with a user-entered caption and stores it in Firebase. The photo is stored in Firebase storage. The caption and the path of the photo in Firebase Storage are stored in Firebase Realtime Database.

The problem I'm encountering is that the share extension stops working after first send. The strange thing is, if I do a similar approach in a regular View Controller in the iOS app, the code works. I noticed two issues with the Share Extension:

Issue #1: The share extension view isn't dismissed completely. In a normal situation, the view would return to a "gallery" mode of Photos. However, the share extension view is going away but the share menu with the complete list of apps that you can use to share is not dismissing.



Issue #2: the data isn't being sent up to Firebase storage or Firebase database.

Below please find my ShareViewController code:

import UIKit
import Social
import Firebase
import MobileCoreServices

class ShareViewController: SLComposeServiceViewController {

    var ref: DatabaseReference!
    var storageRef: StorageReference!

    override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here
        return true
    }

    override func didSelectPost() {
        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.

        FirebaseApp.configure()

        ref = Database.database().reference()
        storageRef = Storage.storage().reference()

        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.

        if let item = self.extensionContext?.inputItems[0] as? NSExtensionItem {
            for ele in item.attachments!{
                let itemProvider = ele as! NSItemProvider

                if itemProvider.hasItemConformingToTypeIdentifier("public.jpeg"){
                    itemProvider.loadItem(forTypeIdentifier: "public.jpeg", options: nil, completionHandler: { (item, error) in

                        do {
                            var imgData: Data!
                            if let url = item as? URL{
                                imgData = try Data(contentsOf: url)
                            }

                            if let img = item as? UIImage{
                                imgData = UIImagePNGRepresentation(img)
                            }

                            var updateRef = self.ref.child("demo_group").child("demo_patient").child("demo_updates").childByAutoId()
                            var updateStorageRef = self.storageRef.child("demo_photos" + "/\(Double(Date.timeIntervalSinceReferenceDate * 1000)).jpg")

                            updateRef.child("event_name").setValue(self.contentText)
                            updateRef.child("sender").setValue("demo_ff")

                            let metadata = StorageMetadata()
                            metadata.contentType = "image/jpeg"

                            updateStorageRef.putData(imgData, metadata: metadata) { (metadata, error) in
                                if let error = error {
                                    print("Error uploading: \(error)")
                                    return
                                }
                                // use sendMessage to add imageURL to database
                                updateRef.child("photos").childByAutoId().setValue(metadata?.path)
                            }

                        } catch let err{
                            print(err)
                        }
                    })
                }
            }
        }

        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    override func configurationItems() -> [Any]! {
        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
        return []
    }

}


Please let me know if there's anything I can do to fix this! I would be greatly appreciated.

Martello Jones

unread,
Oct 15, 2017, 10:34:45 AM10/15/17
to fireba...@googlegroups.com
I recently had a similar issue.
1- I hadn’t to enable shared groups and shared keychain to enable fire base to maintain the auth session. Even fire base storage worked after doing this.

2- in the extension calling FirebaseApp.config() twice causes the crash in the extension. My fix was simply to do:


if let app = FirebaseApp.app() {
// don’t call config
}else {
FirebaseApp.configure()
}

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/98bc7558-3e93-4d82-a940-a84a374204ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Martello M. Jones
_____________________________

Principal Mobile Systems Consultant
Konsölt.Beyond Technology Corp.

mart...@konsoltcorp.com

Martello Jones

unread,
Oct 16, 2017, 10:54:31 AM10/16/17
to fireba...@googlegroups.com
Were you able to proceed after doing as mentioned in my previous post?

Martello M. Jones
_____________________________

Principal Mobile Systems Consultant
Konsölt.Beyond Technology Corp.

mart...@konsoltcorp.com


}

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/98bc7558-3e93-4d82-a940-a84a374204ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages