ios - Displaying UIActivityViewController in GameScene.swift -
i have developed little game consists of main menu, game , there game on label. problem have added programmatically in gamescene.swift file.
now can present uiactivityviewcontroller using lines of code:
class gamescene: skscene, skphysicscontactdelegate { var gameviewcontroller: gameviewcontroller!
in gameviewcontroller.swift put code in viewdidload() function:
scene.gameviewcontroller = self
and added following function gamescene.swift file:
func share() { if gameviewcontroller != nil { let mytext = "some text" let activityvc:uiactivityviewcontroller = uiactivityviewcontroller(activityitems: [mytext], applicationactivities: nil) gameviewcontroller.presentviewcontroller(activityvc, animated: true, completion: nil) } }
this function executed button added programmatically , works when launch app. when play game , hit replay button , gamescene gets reloaded following message when hit share button:
fatal error: unexpectedly found nil while unwrapping optional value
please me!
ok i've found answer after hours.
i didn't use gameviewcontroller variable have declared on top of gamescene.swift file. instead i've used following lines of code:
func share() { let vc = self.view?.window?.rootviewcontroller let mytext = "test" let activityvc:uiactivityviewcontroller = uiactivityviewcontroller(activityitems: [mytext], applicationactivities: nil) vc?.presentviewcontroller(activityvc, animated: true, completion: nil) }
the answer question simple , hope can solution.
Comments
Post a Comment