图像仅在swift 3中的whatsapp上共享

时间:2018-01-05 10:48:05

标签: ios swift whatsapp

我想仅在whatsapp上分享图片,但我使用的代码显示了其他在线共享平台,即信使&电子邮件。

我使用的代码:

    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)

2 个答案:

答案 0 :(得分:1)

您无法隐藏\S+中显示的所有选项,因为它取决于您共享的内容以及设备中已安装的应用,但您可以隐藏大部分内容,例如所有默认选项方式:

UIActivityViewController

或者

如果你只想分享whatapp,那么这就是你的方法。请参考以下网址:

Share image/text through WhatsApp in an iOS app

答案 1 :(得分:1)

通过在Patricks先生的代码中添加一些额外的行,我减少了满足我的需要的额外的在线共享选项,如果任何人都遇到相同的问题,我想分享。我使用下面的代码删除了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")

      }
  }