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

I use Image, video and LivePhoto, using UIActivityViewControlleron different social networks.

But when I use LivePhoto on WhatsApp , something like the following happens:

  • when an ActivityViewController is present -> click on WhatsApp -> it presents a contact list for the second one and dodges quickly, and when I try to print the error using the ActivityViewControllerCompletion Handler, it prints something like this:

[core] SLComposeViewController remoteViewController: didTerminateWithError: Domain Error = _UIViewServiceInterfaceErrorDomain Code = 3 "(null)" UserInfo = {Message = Service Connection Interrupted} [core] SLComposeViewController completeWithResult because it already dispenses with SLComposeViewlerdrol, because it rejects SLComposeViewController, because it dispenses with the controller 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 someone help me with you.

Thanks.

+6
source share
1 answer

Here I got a solution

I removed UIActivityControllerand used UIDocumentInteractionControlleras shown 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 the delegate method:

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)
            }
        }
    }
 }
+3
source

All Articles