如何使用swift在SpriteKit游戏中创建“分享到Facebook按钮”? [英] How can I create a 'share to Facebook button' in a SpriteKit game using swift?

查看:146
本文介绍了如何使用swift在SpriteKit游戏中创建“分享到Facebook按钮”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Xcode中的Game模板制作了一个简单的游戏,以swift编码。我创建了一个shapeNode,当它被触及时,我希望这段代码运行:

I have made a simple game using the Game template in Xcode, coded in swift. I created a shapeNode, and when it is touched, I would like this code to run:

        if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
            var controller = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            controller.setInitialText("Testing Posting to Facebook")
            //self.presentViewController(controller, animated:true, completion:nil)
        }

此代码在GameViewController.swift文件中运行,但出现此错误。这个错误发生在注释行上。

This code is run in the GameViewController.swift file, but gives this error. This error occurs on the commented line.

Could not cast value of type 'UIView' (0x379480d0) to 'SKView' (0x37227ad0).


推荐答案

更新:如果您定位iOS 9或更高版本是做这项工作的一些小变化。您需要将正确的URL方案添加到您的info.plist中,否则检查应用程序是否安装无效。

Update: If you are targeting iOS 9 or above there are some small changes to make this work. You will need to add the correct URL schemes to your info.plist otherwise the check to see if the app is installed will not work.

注意:这是一个更好的主意现在使用UIActivityController进行共享。这允许你只使用1个按钮,你可以共享各种服务。

NOTE: Its is a better idea to now use UIActivityController for sharing. This allows you to only use 1 button and you can share to all sorts of services.

http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/

要在SKScene中显示viewController,您需要使用rootViewController

To present a viewController in a SKScene you need to use the rootViewController

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

我使用一个小助手来使用swift 2协议扩展,所以你可以在任何你喜欢的应用程序中使用它,Facebook部分看起来像这样,twitter基本上是一样的。

I use a little helper for this using swift 2 protocol extensions, so you can use it anywhere you like in your app. The Facebook part looks like this, twitter is basically the same.

import SpriteKit
import Social

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

/// Text strings
private struct TextString {
    static let shareSheetText = "Your share sheet text"
    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 = URLString.facebookApp else { return }
    guard let facebookWeb = URLString.facebookWeb else { return }

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

/// Share to facebook
func shareToFacebook() {

    guard SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) else {
        showAlert()
        return
    }

    guard let facebookSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook) else { return }
    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)
    facebookSheet.setInitialText(String.localizedStringWithFormat(text, "add your score property")) // same as line above but with a score property
    facebookSheet.addImage(Your UIImage)
    facebookSheet.add(URLString.iTunesApp)

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

   // MARK: - Private Methods

   /// 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 = URL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.shared.openURL(url)
        }
    }
    alertController.addAction(settingsAction)

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

要使用助手,只需转到需要调用的SKScene方法和实现协议

To use the helper you simply go to the SKScene you need to call the methods and implement the protocol

 class YourScene: SKScene, Social {....

现在,当按下Facebook节点/按钮时,您可以调用方法,就好像它们是场景本身的一部分一样。

Now when the Facebook node/button is pressed you can call the methods as if they are part of the scene itself.

openFacebook()    // opens app or safari
shareToFacebook() // opens share sheet textField

全都归功于swift 2和协议扩展。关于这一点很酷的是说你想在一个普通的UIKit应用程序中使用这个助手,而不是所有你需要做的是导入UIKit而不是spriteKit

all thanks to swift 2 and protocol extensions. The cool bit about this is say you want to use this helper in a regular UIKit app, than all you have to do is import UIKit instead of spriteKit

 import UIKit

并将协议扩展名更改为

and change the protocol extension to this

 extension Social where Self: UIViewController {....

它非常好,非常灵活我认为

Its quite nice and very flexible I think

希望这有助于。

这篇关于如何使用swift在SpriteKit游戏中创建“分享到Facebook按钮”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆