斯威夫特的社交分享

时间:2017-05-17 16:28:30

标签: ios swift

我正在编写一个功能,当单击按钮时会生成屏幕截图,然后让您在Twitter,Instagram和Facebook上分享这张照片。  所以除了Instagram之外,我的所有应用程序都有一个功能:

func socialShare(sharingText: String?, sharingImage: UIImage?) {
    var sharingItems = [AnyObject]()

    if let text = sharingText {
        sharingItems.append(text as AnyObject)
    }
    if let image = sharingImage {
        sharingItems.append(image)
    }
    let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

    activityViewController.excludedActivityTypes = [UIActivityType.addToReadingList, UIActivityType.print, UIActivityType.assignToContact, UIActivityType.copyToPasteboard]

    self.present(activityViewController, animated: true, completion: nil)
}

还有Instagram共享功能:

var docController: UIDocumentInteractionController!

func documentsDirectory() -> String {
    return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}

func postImageToInstagram(sharingImage: UIImage?) {



    let instagram = URL(string: "instagram://app")!


    if UIApplication.shared.canOpenURL(instagram) {


        let imageURL = URL(fileURLWithPath: documentsDirectory())

        let fullPath = imageURL.appendingPathComponent("instagram.igo")

        do {
            try UIImagePNGRepresentation(sharingImage!)!.write(to: fullPath)

            let rect = CGRect(x: 0, y: 0, width: 0, height: 0)

            let igImageHookFile = URL(string: "file://\(fullPath)")

            docController = UIDocumentInteractionController(url: igImageHookFile!)

            docController.uti = "com.instagram.photo"
            docController.presentOpenInMenu(from: rect, in: self.view, animated: true)
        } catch {
            print(error)
        }  
    }
    else {
        print("Instagram not installed")
    }

}

我如何一起使用它?因为它们只能单独工作......

1 个答案:

答案 0 :(得分:1)

我发现Instagram没有包含在默认的<style> body { margin: 0 } </style>活动中,这有点令人讨厌。但这并不意味着您无法为Instagram添加自己的UIActivityViewController以及您已经创建的方法中已经发布到Instagram的逻辑。请参阅SO上的this帖子,了解如何创建自己的UIActivity。它甚至还包含一个链接到一个花哨的GitHub仓库,它为大多数遗留的社交服务提供自定义UIActivity,包括Instagram。