我正在尝试通过 Twitter 实现共享,功能看起来不错,但是当单击按钮时,我的 GameScene 出现错误。
在这条线上:
让 skView = self.view as! SKView
错误:线程 1 SIGARBT
我整天都被困在这个问题上。任何帮助都会很棒
var vc = GameViewController()
vc.showTweetSheet() // button click causes error ^ ^ ^
//游戏 View
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)
}
}
最佳答案
你的方法不工作,你正在创建一个新的 GameViewController 实例而不是访问当前的 GameViewController
var vc = GameViewController() // This creates new instance of GameViewController
访问当前 gameViewController 的最佳方法通常是使用委托(delegate)或 NSNotificationCenter 而不是获取对它的引用。
您也可以将您的 shareSheet 函数放入 GameScene 并在 rootViewController 上显示它。
self.view?.window?.rootViewController?.presentViewController(tweetSheet...
作为替代方案,我建议您制作一个可重复使用的助手,以便在您以后的游戏中重用此代码会容易得多。
我使用 Swift 2 中的协议(protocol)扩展来实现这一点。创建一个新的 swift 文件,将其命名为 SocialHelper 并添加此代码(只需复制 2 个方法并将其更改为 twitter,代码是相同的)
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)
}
}
现在在您想要显示 shareSheet 的场景中,您只需遵守协议(protocol)
class YourScene: SKScene, Social {....
并调用该方法,就好像它是场景本身的一部分一样。
openFacebook() // opens app or safari
shareToFacebook()
希望对你有帮助
关于ios - Swift2 SpriteKit 推特分享错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37826020/