swift spritekit Facebook share button

前端 未结 2 1813
暖寄归人
暖寄归人 2020-12-10 08:48

Hey there my app is almost ready for release but i want to add a Facebook share button. The thing is i have no idea how the communication between the scene and the viewcontr

2条回答
  •  爱一瞬间的悲伤
    2020-12-10 09:29

    func lkFaceBookShare() {
        var serviceType: String = SLServiceTypeFacebook
        if !SLComposeViewController.isAvailableForServiceType(serviceType) {
            self.showUnavailableAlertForServiceType(serviceType)
        }
        else {
            var composeViewController: SLComposeViewController = SLComposeViewController.composeViewControllerForServiceType(serviceType)
            var keyWindow: UIWindow = UIApplication.sharedApplication().keyWindow
            var rect: CGRect = keyWindow.bounds
            UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0.5)
            self.view!.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
            var viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            composeViewController.addImage(viewImage)
            var initalTextString: String = String(format: "Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8")
            composeViewController.initialText = initalTextString
            var vc: UIViewController = self.view.window.rootViewController
            vc.presentViewController(composeViewController, animated: true, completion: { _ in })
        }
    }
    
    func showUnavailableAlertForServiceType(serviceType: String) {
        var serviceName: String = ""
        if serviceType == SLServiceTypeFacebook {
            serviceName = "Facebook"
        }
        else if serviceType == SLServiceTypeSinaWeibo {
            serviceName = "Sina Weibo"
        }
        else if serviceType == SLServiceTypeTwitter {
            serviceName = "Twitter"
        }
    
        var alertView: UIAlertView = UIAlertView(title: "Account", message: "Please go to the device settings and add a \(serviceName) account in order to share through that service", delegate: nil, cancelButtonTitle: "Dismiss", otherButtonTitles: "")
        alertView.show()
    }
    

    Swift Conversion of Obj-C answer posted by a very helpful user...... original post

提交回复
热议问题