0

I havn't succeeded to share anything via the "UIDocumentInteractionController", as I went through almost all the tutorials and help online I wish to find a solution:

This is the code i use:

    let fileName = "banner_1"
    let filePath = Bundle.main.path(forResource: fileName, ofType: "jpg")!
    let urlData = URL.init(fileURLWithPath: filePath)
    let nsData = NSData(contentsOf: urlData)

    let newFileName = "banner_1.jpg"
    let newFilePath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(newFileName)"
    nsData?.write(toFile: newFilePath, atomically: true)
    let newUrlData = URL.init(fileURLWithPath: newFilePath)

    documentController?.url = urlData // or newUrlData
    documentController?.uti = "net.whatsapp.image"
    documentController?.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

Everytime I press one tile of the sharing menu for whatsapp or messenger, nothing happen and i have this error log:

2018-02-02 19:56:31.293849-0300 myapp[748:116227] [core] SLComposeViewController initWithExtension: {id = net.whatsapp.WhatsApp.ShareExtension} requestedServiceType: (null)

2018-02-02 19:56:31.296021-0300 myapp[748:116227] [core] SLComposeViewController addExtensionItem: - userInfo: { NSExtensionItemAttachmentsKey = ( " {types = (\n \"public.jpeg\",\n \"public.file-url\"\n)}" ); }

2018-02-02 19:56:31.883007-0300 myapp[748:116227] [core] viewWillAppear

2018-02-02 19:56:31.883240-0300 myapp[748:116227] [core] SLComposeViewController including 1 explicit NSExtensionItems

2018-02-02 19:56:31.883311-0300 myapp[748:116227] [core] SLComposeViewController about to instantiate remote view controller with array of 1 NSExtensionItems

2018-02-02 19:56:31.886583-0300 myapp[748:116227] [core] viewDidAppear

2018-02-02 19:56:31.989725-0300 myapp[748:116362] [ops] Hub connection error Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named net.whatsapp.WhatsApp.ShareExtension" UserInfo={NSDebugDescription=connection to service named net.whatsapp.WhatsApp.ShareExtension}

2018-02-02 19:56:31.991895-0300 myapp[748:116227] [core] SLComposeViewController finished instantiate remote view controller (null) error Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named net.whatsapp.WhatsApp.ShareExtension" UserInfo={NSDebugDescription=connection to service named net.whatsapp.WhatsApp.ShareExtension} extension request identifier (null)

2018-02-02 19:56:31.992325-0300 myapp[748:116227] [core] HOST: Failed to load remote view controller with error: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named net.whatsapp.WhatsApp.ShareExtension" UserInfo={NSDebugDescription=connection to service named net.whatsapp.WhatsApp.ShareExtension}

2018-02-02 19:56:31.992433-0300 myapp[748:116227] [core] Sheet not being presented, calling premature completion

2018-02-02 19:56:31.992479-0300 myapp[748:116227] [core] SLComposeViewController completeWithResult: 0

2018-02-02 19:56:31.995770-0300 myapp[748:116227] [core] SLComposeViewController skipping explicit dismiss because isBeingDismissed is already 1 g

2018-02-02 19:56:32.001500-0300 myapp[748:116227] [core] SLComposeViewController dealloc

I did added "LSApplicationQueriesSchemes" with "whatsapp" in the plist file.

I tried with the UIDocumentInteractionControllerDelegate to find if there is any strange event but didn't helped a lot.

If you have any adea ?! Thanks, Antoine.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Antoine Nedelec
  • 655
  • 8
  • 21

1 Answers1

1

Here is working code through that you can share your image to whatsapp, All you need to do is just add LSApplicationQueriesSchemes whatsapp into your plist file for WhatsApp sharing enable.

Swift 4

func shareOnWhatsUp() {

    if UIApplication.shared.canOpenURL(URL(string: "whatsapp://app")!) {
        var documentInteractionController: UIDocumentInteractionController = UIDocumentInteractionController.init()
        let iconImage = UIImage(named: "IMG_1.jpg")
        let pathURL =   URL.init(fileURLWithPath: NSHomeDirectory() + "/Documents/whatsAppTmp.wai")

        do {
            try UIImageJPEGRepresentation(iconImage!, 1.0)?.write(to: pathURL)
        } catch {
            print(error.localizedDescription)
        }
        documentInteractionController!.url = pathURL
        documentInteractionController!.uti = "net.whatsapp.image"
        documentInteractionController!.delegate = self
        documentInteractionController!.presentOpenInMenu(from: CGRect(x: 0, y: 0, width: 0, height: 0), in: self.view, animated: true)
    }
    else {
        print("whatsup not installed")
    }
}
  • Thanks, is does open the share menu first, then the whatsapp menu if i click on whatsapp. One thing i've not figured out yet (from the whatsapp documentation: https://faq.whatsapp.com/en/iphone/23559013/), is how to open whatsapp directly with the exclusive extention (.wai, .waa, .wam) – Antoine Nedelec Feb 03 '18 at 15:50
  • 1
    for the record, the "this item cannot be shared" error was fixed by creating a class with "UIDocumentInteractionController" & "UIDocumentInteractionControllerDelegate", then instanciate it in my root controller. and setting the delegate to self in the init of that new class. – Antoine Nedelec Feb 03 '18 at 15:58
  • Dear, As per your question definition i provided you working solution to share image with the whaapp, If you still facing any error just share me your source i can check it and fix it. –  Feb 05 '18 at 06:44
  • Thanks, the problem I had here is fixed, the sharing works. The next problem I have is about the whatsapp API. I asked the question there: https://stackoverflow.com/questions/48599394/xcode-ios-how-to-use-exclusive-extension-to-immediately-present-whatsapp-w. I couldn't find anyone online that sucessfully made it works, or talking about laking it work. I guess the problem is not about the code. – Antoine Nedelec Feb 06 '18 at 02:07