ios - How can I create a 'share to Facebook button' in a SpriteKit game using swift? -
i have made simple game using game template in xcode, coded in swift. created shapenode, , when touched, code run:
if slcomposeviewcontroller.isavailableforservicetype(slservicetypefacebook){ var controller = slcomposeviewcontroller(forservicetype: slservicetypefacebook) controller.setinitialtext("testing posting facebook") //self.presentviewcontroller(controller, animated:true, completion:nil) }
this code run in gameviewcontroller.swift file, gives error. error occurs on commented line.
could not cast value of type 'uiview' (0x379480d0) 'skview' (0x37227ad0).
update: if targeting ios 9 or above there small changes make work. need add correct url schemes info.plist otherwise check see if app installed not work.
note: better idea use uiactivitycontroller sharing. allows use 1 button , can share sorts of services.
http://useyourloaf.com/blog/querying-url-schemes-with-canopenurl/
to present viewcontroller in skscene need use rootviewcontroller
self.view?.window?.rootviewcontroller?.presentviewcontroller(...
i use little helper using swift 2 protocol extensions, can use anywhere in app. facebook part looks this, twitter 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 account first" static let settings = "settings" static let ok = "ok" } /// social protocol social {} extension social 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 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 score property")) // same line above 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) } }
to use helper go skscene need call methods , implement protocol
class yourscene: skscene, social {....
now when facebook node/button pressed can call methods if part of scene itself.
openfacebook() // opens app or safari sharetofacebook() // opens share sheet textfield
all swift 2 , protocol extensions. cool bit want use helper in regular uikit app, have import uikit instead of spritekit
import uikit
and change protocol extension this
extension social self: uiviewcontroller {....
its quite nice , flexible think
hope helps.
Comments
Post a Comment