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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -