ios - XCODE/IOS - 通过 UIDocumentInteractionController 共享

标签 ios swift share whatsapp uidocumentinteractioncontroller

我没有成功地通过“UIDocumentInteractionController”分享任何东西,因为我浏览了几乎所有的教程和在线帮助我希望找到一个解决方案:

这是我使用的代码:

    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)

每次我按 whatsapp 或 messenger 共享菜单的一个图 block 时,什么也没有发生,我有这个错误日志:

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

我确实在 plist 文件中添加了“LSApplicationQueriesSchemes”和“whatsapp”。

我尝试使用 UIDocumentInteractionControllerDelegate 来查找是否有任何奇怪的事件,但没有太大帮助。

如果你有任何想法?! 谢谢,安托万。

最佳答案

这是工作代码,通过它您可以将图像分享到 whatsapp,您需要做的只是将 LSApplicationQueriesSchemes whatsapp 添加到您的 plist 文件中以启用 WhatsApp 共享。

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")
    }
}

关于ios - XCODE/IOS - 通过 UIDocumentInteractionController 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592180/

相关文章:

css - 跨元素共享 css 和 ts

ios - UIActivityViewController,iOS, swift

ios - trigger.io native iOS 模块在检查器中工作,而不是应用程序

ios - CoreData 迁移问题

ios - VoIP 通知和使用 apns-priority 10 发送的通知有什么区别?

swift - 两个标记之间的快速方向

android - 无法将内容从 Android 应用分享到 Facebook

iPhone iOS如何按需重绘UINavigationBar?

ios - Swift Connect UITableView 用于多个子类别

ios - 如何在拍摄的照片上画线?