在 Swift 3 中使用 UIActivityViewController 在 WhatsApp 上分享实时照片不起作用
Sharing Live photo on WhatsApp in Swift 3 using UIActivityViewController not working
我正在使用 UIActivityViewController
在不同的社交媒体上分享图片、视频和 LivePhoto。
但是当我在 WhatsApp 上分享 LivePhoto 时,会发生如下情况:
- 当 ActivityViewController 存在时 -> 单击 WhatsApp -> 它会显示第二个联系人列表并迅速关闭,当我尝试使用
ActivityViewController
完成处理程序打印错误时,它会打印如下内容:
[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)
}
})
谁能帮我一下。
谢谢。
这里我得到了解决方案
我删除了 UIActivityController
并使用了 UIDocumentInteractionController
,如下所示:
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)
}
}
然后在它的委托方法中:
对于 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)
}
}
}
}
我正在使用 UIActivityViewController
在不同的社交媒体上分享图片、视频和 LivePhoto。
但是当我在 WhatsApp 上分享 LivePhoto 时,会发生如下情况:
- 当 ActivityViewController 存在时 -> 单击 WhatsApp -> 它会显示第二个联系人列表并迅速关闭,当我尝试使用
ActivityViewController
完成处理程序打印错误时,它会打印如下内容:
[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)
}
})
谁能帮我一下。
谢谢。
这里我得到了解决方案
我删除了 UIActivityController
并使用了 UIDocumentInteractionController
,如下所示:
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)
}
}
然后在它的委托方法中:
对于 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)
}
}
}
}