Search code examples
iosobjective-cswift3uiactivityviewcontrollerapple-live-photos

Sharing Live photo on WhatsApp in Swift 3 using UIActivityViewController not working


I am sharing Image,video and LivePhoto using UIActivityViewController on Different social media.

But when i am sharing LivePhoto on WhatsApp ,something like below is happening :

  1. when ActivityViewController present -> click on WhatsApp -> it present contact list for second and quickly dismiss and when i try to print error using ActivityViewController Completion handler it print something like this :

[core] SLComposeViewController remoteViewController: didTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted} [core] SLComposeViewController completeWithResult: 0 [core] SLComposeViewController skipping explicit dismiss because isBeingDismissed is already 1 SLComposeViewController dealloc

I have tried with this code : 

PHImageManager.default().requestImageData(for: selectedAsset, options: nil, resultHandler: { (imgData, str, image, info) in

                activityItems.append(imgData!)

                let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
                activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
                activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems:[Any]?, error: Error?) in
                    //Do whatever you want
                    print("activityType ----- \(activityType) || error ----- \(error)")
                }
                // present the view controller
                DispatchQueue.main.async {
//                    self.present(activityViewController, animated: true, completion: nil)
                    self.navigationController?.present(activityViewController, animated: true, completion: nil)

                }
            })

can anyone help me with please.

Thank you.


Solution

  • Here I got the solution

    I have removed UIActivityController and Used UIDocumentInteractionController like below :

    let imageLocalPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("public.jpeg")
                    
                    if let imageData = imgData {
                        do {
                            try imageData.write(to: imageLocalPath, options: .atomic)
                            self.documentInteractionController = UIDocumentInteractionController(url: imageLocalPath)
    //                        self.documentInteractionController.uti = "net.whatsapp.image"
                            self.documentInteractionController.uti = "public.image"
                            self.documentInteractionController.delegate = self
                            self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
                        } catch {
                            print(error)
                        }
                    }
    

    Then In delegate method of it :

    For WhatsApp :

    func documentInteractionController(_ controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) {
            print("Application ----- \(String(describing: application))")
            
        if(check for whatsApp condition){
            let imageLocalPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("whatsAppTmp.wai")
            if let imageData = selectedImageData {
                do {
                    try imageData.write(to: imageLocalPath, options: .atomic)
                    controller.uti = "net.whatsapp.image"
                    controller.url = imageLocalPath
                } catch {
                    print(error)
                }
            }
        }
     }