从SKScene展示UIViewController?

时间:2015-04-29 00:45:46

标签: facebook swift twitter sprite-kit

所以在开始时我无法使用Swift / Sprite-Kit将社交媒体共享实现到我的项目中。我相信我现在已经完成了这个,但我不能触发这些方法,或者因为似乎每个人都在努力,我无法从我的SKScene中呈现视图控制器。所以我最初做的是在我的EndGame场景上创建我的按钮。然后在我的touchesEnded方法中,我调用了NSNotificationCenter。像这样:

import SpriteKit
import UIKit
import Social

class EndGameScene: SKScene {

var facebookButton: SKNode! = nil
var twitterButton: SKNode! = nil


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(size: CGSize) {
    super.init(size: size)

    // FacebookButton
    facebookButton = SKSpriteNode(imageNamed: "facebookButton")
    facebookButton.position = CGPoint(x: self.size.width * 0.3 , y: self.size.height * 0.45);
    addChild(facebookButton)

    // TwitterButton
    twitterButton = SKSpriteNode(imageNamed: "twitterButton")
    twitterButton.position = CGPoint(x: self.size.width * 0.5 , y: self.size.height * 0.45);
    addChild(twitterButton)


}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if facebookButton.containsPoint(location) {
           NSLog("Button tapped")
           NSNotificationCenter.defaultCenter().postNotificationName("FacebookID", object: nil)

        }

        if twitterButton.containsPoint(location) {
            NSLog("Button tapped")
            NSNotificationCenter.defaultCenter().postNotificationName("TwitterID", object: nil)

        }


    }

}

然后我创建了SocialViewController,其中包含应该在NSNotificationCenter的帮助下触发的方法来显示工作表。这是我用它的代码:

import UIKit
import Social

class SocialViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showTweetSheet", name: "TwitterID", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showFacebookSheet", name: "FacebookID", object: nil)
}

func showTweetSheet() {
    let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
    tweetSheet.completionHandler = {
        result in
        switch result {
        case SLComposeViewControllerResult.Cancelled:
            break
        case SLComposeViewControllerResult.Done:
            break
        }
    }

    tweetSheet.setInitialText("Test Twitter")
    tweetSheet.addImage(UIImage(named: "TestImage.png"))
    tweetSheet.addURL(NSURL(string: "http://twitter.com"))
    self.presentViewController(tweetSheet, animated: false, completion: {
    })
}

func showFacebookSheet() {
    let facebookSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    facebookSheet.completionHandler = {
        result in
        switch result {
        case SLComposeViewControllerResult.Cancelled:
            break
        case SLComposeViewControllerResult.Done:
            break
        }
    }
    facebookSheet.setInitialText("Test Facebook")
    facebookSheet.addImage(UIImage(named: "TestImage.png")) 
    facebookSheet.addURL(NSURL(string: "http://facebook.com")) 
    self.presentViewController(facebookSheet, animated: false, completion: {
    })
}

我完全不知道我可能会遗漏或做错什么。我已经阅读了几篇关于此的帖子,并且使用NSNotificationCenter应该是实现这一目标的最佳方式,但似乎对我不起作用。所以,如果那里有任何人可以帮助我,我会非常感激。提前致谢!

1 个答案:

答案 0 :(得分:-1)

您可以从https://github.com/facebook/facebook-android-sdk下载Facebook Sdk。在使用cmd提示创建密钥哈希之后。有关创建密钥哈希的更多详细信息,请参阅链接https://developers.facebook.com/docs/android/getting-started