图片仅在 swift 3 中的 whatsapp 上分享
Image share only on whatsapp in swift 3
我只想在 whatsapp 上分享图片,但我使用的代码显示了其他在线分享平台,即 Messenger 和电子邮件。
我使用的代码:
func share(shareText:String?,shareImage:UIImage?){
var objectsToShare = [AnyObject]()
if let shareTextObj = shareText{
objectsToShare.append(shareTextObj as AnyObject)
}
if let shareImageObj = shareImage{
objectsToShare.append(shareImageObj)
}
if shareText != nil || shareImage != nil{
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController, animated: true, completion: nil)
}else{
print("There is nothing to share")
}
}
并分享
let imageToShare = UIImage(named: "05")
share(shareText: "", shareImage: imageToShare)
您无法隐藏 UIActivityViewController
中显示的所有选项,因为这取决于您分享的内容以及设备中安装的应用程序,但您可以像隐藏所有默认选项一样隐藏大部分选项像这样:
let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityController.excludedActivityTypes = [
UIActivityType.assignToContact,
UIActivityType.print,
UIActivityType.addToReadingList,
UIActivityType.saveToCameraRoll,
UIActivityType.openInIBooks,
UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
UIActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
]
present(activityController, animated: true, completion: nil)
或
如果您只想在 whatapp 中分享,那么这里是分享的方法。请参考下面url:
Share image/text through WhatsApp in an iOS app
通过在帕特里克先生的代码中添加一些额外的行,我减少了满足我需要的额外在线共享选项,如果有人面临同样的问题,我想分享给那些人。我使用下面的代码删除了除 WhatsApp 之外的所有其他共享选项,它只显示 WhatsApp 和更多在屏幕上共享的选项。谢谢
func shareImg(){
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityType.postToTwitter,
UIActivityType.postToWeibo,
UIActivityType.message,
UIActivityType.mail,
UIActivityType.print,
UIActivityType.copyToPasteboard,
UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll,
UIActivityType.addToReadingList,
UIActivityType.postToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo,
UIActivityType.airDrop
]
self.present(activityViewController, animated: true, completion: nil)
activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
if !completed {
print("User canceled")
self.shareItems.removeAll()
return
}
print("share successfully")
}
}
我只想在 whatsapp 上分享图片,但我使用的代码显示了其他在线分享平台,即 Messenger 和电子邮件。
我使用的代码:
func share(shareText:String?,shareImage:UIImage?){
var objectsToShare = [AnyObject]()
if let shareTextObj = shareText{
objectsToShare.append(shareTextObj as AnyObject)
}
if let shareImageObj = shareImage{
objectsToShare.append(shareImageObj)
}
if shareText != nil || shareImage != nil{
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController, animated: true, completion: nil)
}else{
print("There is nothing to share")
}
}
并分享
let imageToShare = UIImage(named: "05")
share(shareText: "", shareImage: imageToShare)
您无法隐藏 UIActivityViewController
中显示的所有选项,因为这取决于您分享的内容以及设备中安装的应用程序,但您可以像隐藏所有默认选项一样隐藏大部分选项像这样:
let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityController.excludedActivityTypes = [
UIActivityType.assignToContact,
UIActivityType.print,
UIActivityType.addToReadingList,
UIActivityType.saveToCameraRoll,
UIActivityType.openInIBooks,
UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
UIActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
]
present(activityController, animated: true, completion: nil)
或
如果您只想在 whatapp 中分享,那么这里是分享的方法。请参考下面url:
Share image/text through WhatsApp in an iOS app
通过在帕特里克先生的代码中添加一些额外的行,我减少了满足我需要的额外在线共享选项,如果有人面临同样的问题,我想分享给那些人。我使用下面的代码删除了除 WhatsApp 之外的所有其他共享选项,它只显示 WhatsApp 和更多在屏幕上共享的选项。谢谢
func shareImg(){
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [
UIActivityType.postToTwitter,
UIActivityType.postToWeibo,
UIActivityType.message,
UIActivityType.mail,
UIActivityType.print,
UIActivityType.copyToPasteboard,
UIActivityType.assignToContact,
UIActivityType.saveToCameraRoll,
UIActivityType.addToReadingList,
UIActivityType.postToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo,
UIActivityType.airDrop
]
self.present(activityViewController, animated: true, completion: nil)
activityViewController.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
if !completed {
print("User canceled")
self.shareItems.removeAll()
return
}
print("share successfully")
}
}