0

我没有成功通过“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 的共享菜单的一个图块时,什么都没有发生,我有这个错误日志:

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

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

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

2018-02-02 19:56:31.883240-0300 myapp[748:116227] [核心] SLComposeViewController 包括 1 个显式 NSExtensionItems

2018-02-02 19:56:31.883311-0300 myapp[748:116227] [核心] SLComposeViewController 即将使用 1 个 NSExtensionItems 数组实例化远程视图控制器

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

2018-02-02 19:56:31.989725-0300 myapp[748:116362] [ops] 集线器连接错误错误域=NSCocoaErrorDomain 代码=4097“连接到名为 net.whatsapp.WhatsApp.ShareExtension 的服务”UserInfo={NSDebugDescription=connection到名为 net.whatsapp.WhatsApp.ShareExtension} 的服务

2018-02-02 19:56:31.991895-0300 myapp[748:116227] [核心] SLComposeViewController 完成实例化远程视图控制器(空)错误错误域 = NSCocoaErrorDomain 代码 = 4097“连接到名为 net.whatsapp.WhatsApp.ShareExtension 的服务" UserInfo={NSDebugDescription=连接到名为 net.whatsapp.WhatsApp.ShareExtension} 的服务扩展请求标识符 (null)

2018-02-02 19:56:31.992325-0300 myapp[748:116227] [核心] 主机:无法加载远程视图控制器并出现错误:错误域 = NSCocoaErrorDomain 代码 = 4097“连接到名为 net.whatsapp.WhatsApp 的服务。 ShareExtension" UserInfo={NSDebugDescription=连接到名为 net.whatsapp.WhatsApp.ShareExtension} 的服务

2018-02-02 19:56:31.992433-0300 myapp[748:116227] [核心] 工作表未呈现,调用过早完成

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

2018-02-02 19:56:31.995770-0300 myapp[748:116227] [核心] SLComposeViewController 跳过显式关闭,因为 isBeingDismissed 已经是 1 g

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

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

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

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

4

1 回答 1

1

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

斯威夫特 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")
    }
}
于 2018-02-03T10:12:37.587 回答