2016-06-15 7 views
1

私はtwitter経由で共有を実装しようとしていますが、機能はよく見えますが、ボタンをクリックするとGameSceneでエラーが発生します。Swift2 SpriteKit twitter共有エラー

このラインラインで: let skView = self.view as! SKView エラー:スレッド1 SIGARBT

アイブ氏は一日中、この上で立ち往生されて。すべてのヘルプは

var vc = GameViewController() 
vc.showTweetSheet() // button click causes error^^^

// GameView

class GameViewController: UIViewController { 

func showTweetSheet() { 
    let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 
    tweetSheet.completionHandler = { 
     result in 
     switch result { 
     case SLComposeViewControllerResult.Cancelled: 
      //Add code to deal with it being cancelled 
      break 

     case SLComposeViewControllerResult.Done: 
      //Add code here to deal with it being completed 
      //Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards? 
      break 
     } 
    } 

    tweetSheet.setInitialText("Test Twitter") //The default text in the tweet 
    tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like? 
    tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on 

    self.presentViewController(tweetSheet, animated: false, completion: { 
     NSNotificationCenter.defaultCenter().postNotificationName("Whatever", object: nil) 
    }) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let scene = GameScene(fileNamed:"GameScene") { 
     // Configure the view. 
     let skView = self.view as! SKView //ERROR HAPPENS HEREE (ONLY ON CLK 
     skView.showsFPS = false 
     skView.showsNodeCount = false 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

     /* Set the scale mode to scale to fit the window */ 
     scene.scaleMode = .AspectFill 
     scene.size = self.view.bounds.size 

     skView.presentScene(scene) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "ThisIsTheMethodName", name: "Whatever", object: nil) 
    } 
} 

答えて

0

あなたの道は、あなたがGameViewControllerの新しいインスタンスを作成するのではなく、現在のGameViewControllerに

var vc = GameViewController() // This creates new instance of GameViewController 

アクセスしている、機能しません。素晴らしいことです現在のgameViewControllerにアクセスするには、委任やNSNotificationCenterを使用するのが一般的です。

shareSheet関数をGameSceneに配置し、rootViewControllerに表示することもできます。

self.view?.window?.rootViewController?.presentViewController(tweetSheet... 

代わりに、再利用可能なヘルパーを作成することをお勧めします。今後のゲームでは、このコードを再利用する方がはるかに簡単です。

私はこれを達成するためにSwift 2でプロトコル拡張を使用します。あなたがshareSheetを見せたいシーンで今

import SpriteKit 
    import Social 

/// URLs 
private struct URL { 
      static let iTunesApp = NSURL(string: "Your iTunes app link") 
      static let facebookApp = NSURL(string: "Your Facebook app link") 
      static let facebookWeb = NSURL(string: "Your Facebook web link") 
    } 

    /// Text strings 
    private struct TextString { 
     static let shareSheetText = "Can you beat my score" 
     static let error = "Error" 
     static let enableSocial = "Please sign in to your account first" 
     static let settings = "Settings" 
     static let ok = "OK" 
    } 

    /// Social 
    protocol Social {} 
    extension Social where Self: SKScene { 

    /// Open facebook 
func openFacebook() { 
     guard let facebookApp = URL.facebookApp else { return } 
     guard let facebookWeb = URL.facebookWeb else { return } 

     if UIApplication.sharedApplication().canOpenURL(facebookApp){  
      UIApplication.sharedApplication().openURL(facebookApp) 
    } else { 
     UIApplication.sharedApplication().openURL(facebookWeb) 
    } 
} 

    /// Share to facebook 
    func shareToFacebook() { 

     guard SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) else { 
      showAlert() 
      return 
     } 

    let facebookSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
    facebookSheet.completionHandler = { result in 

      switch result { 

      case .Cancelled: 
       print("Facebook message cancelled") 
       break 

      case .Done: 
       print("Facebook message complete") 
       break 
      } 
     } 

     let text = TextString.shareSheetText // 
     facebookSheet.setInitialText(text) // share sheet text for Facebook will not work anymore if app is installed 
    facebookSheet.addImage(Your UIImage) 
    //facebookSheet.addURL(URL.iTunesApp) // seems to mess up image, Facebook only 

     self.view?.window?.rootViewController?.presentViewController(facebookSheet, animated: true, completion: nil) 
    } 

    /// Show alert 
    private func showAlert() { 
     let alertController = UIAlertController(title: TextString.error, message: TextString.enableSocial, preferredStyle: .Alert) 

     let okAction = UIAlertAction(title: TextString.ok, style: .Cancel) { _ in } 
      alertController.addAction(okAction) 

     let settingsAction = UIAlertAction(title: TextString.settings, style: .Default) { _ in 

      if let url = NSURL(string: UIApplicationOpenSettingsURLString) { 
      UIApplication.sharedApplication().openURL(url) 
     } 
    } 
    alertController.addAction(settingsAction) 

    self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 
     } 
    } 

(わずか2つのメソッドをコピーして、さえずりに変更し、コードが同じである)新しい迅速なファイルを作成し、SocialHelperのようなもの、それを呼び出すと、このコードを追加します。あなたは単にプロクトココールに準拠するだけです。

class YourScene: SKScene, Social {.... 

このメソッドをシーン自体の一部であるかのように呼び出します。

openFacebook() // opens app or safari 
shareToFacebook() 

が、これは

を役に立てば幸い
関連する問題