I am having a lot of difficulty with trying to make a share button which allows users to share my game on social media sites. I am developing a game using Sprite Kit. From what I have seen, I am under the impression that the actual code to initiate the share viewController must be under my GameViewController class. I therefore have a button which is initialized in the gameScene class and when it is pressed runs a method that creates an instance of the gameViewController class then runs the shareGame method. Here is my code, I appreciate any help as I am really lost.
In my gameScene class (this method is run when the button is pressed)
-(IBAction)runShareGame:(id)sender{
GameViewController *myViewController = [[GameViewController alloc] init];
[myViewController shareGame];
}
And this code is in the gameViewController.m class (note that the shareGame method is included in the interface)
-(void)shareGame{
NSLog(@"shareGame!");
NSString *message = @"Hello World!";
UIImage *imageToShare = [UIImage imageNamed:@"blue"];
NSArray *postItems = @[message, imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:postItems
applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
When I run the code, I get the following message in the console:
shareGame!
2015-06-06 17:01:27.626 Dodge[96002:3651805] -[UIView setShowsFPS:]: unrecognized selector sent to instance 0x7a8c88b0
2015-06-06 17:01:27.630 Dodge[96002:3651805] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsFPS:]: unrecognized selector sent to instance 0x7a8c88b0'
First throw call stack:
(
0 CoreFoundation 0x00966746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x005efa97 objc_exception_throw + 44
2 CoreFoundation 0x0096e705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x008b5287 ___forwarding___ + 1047
4 CoreFoundation 0x008b4e4e _CF_forwarding_prep_0 + 14
5 Dodge 0x000fb48b -[GameViewController viewDidLoad] + 139
6 UIKit 0x0127cda4 -[UIViewController loadViewIfRequired] + 771
7 UIKit 0x0127d095 -[UIViewController view] + 35
8 UIKit 0x0125784d -[UIPresentationController __sizeClassPair] + 54
9 UIKit 0x0128a7a3 -[UIViewController _presentViewController:withAnimationController:completion:] + 2349
10 UIKit 0x0128d382 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
11 UIKit 0x0128d1d4 -[UIViewController presentViewController:animated:completion:] + 224
12 Dodge 0x000fb817 -[GameViewController shareGame] + 327
13 Dodge 0x00107871 -[GameScene runShareGame:] + 129
14 libobjc.A.dylib 0x006057cd -[NSObject performSelector:withObject:withObject:] + 84
15 UIKit 0x01119a90 -[UIApplication sendAction:to:from:forEvent:] + 99
16 UIKit 0x01119a22 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
17 UIKit 0x0125a18a -[UIControl sendAction:to:forEvent:] + 69
18 UIKit 0x0125a5a7 -[UIControl _sendActionsForEvents:withEvent:] + 598
19 UIKit 0x01259811 -[UIControl touchesEnded:withEvent:] + 660
20 UIKit 0x01171cfa -[UIWindow _sendTouchesForEvent:] + 874
21 UIKit 0x011727d6 -[UIWindow sendEvent:] + 792
22 UIKit 0x011306d1 -[UIApplication sendEvent:] + 242
23 UIKit 0x01140b08 _UIApplicationHandleEventFromQueueEvent + 21484
24 UIKit 0x01114337 _UIApplicationHandleEventQueue + 2300
25 CoreFoundation 0x0088806f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
26 CoreFoundation 0x0087db7d __CFRunLoopDoSources0 + 253
27 CoreFoundation 0x0087d0d8 __CFRunLoopRun + 952
28 CoreFoundation 0x0087ca5b CFRunLoopRunSpecific + 443
29 CoreFoundation 0x0087c88b CFRunLoopRunInMode + 123
30 GraphicsServices 0x03d3d2c9 GSEventRunModal + 192
31 GraphicsServices 0x03d3d106 GSEventRun + 104
32 UIKit 0x01118106 UIApplicationMain + 1526
33 Dodge 0x00107e7a main + 138
34 libdyld.dylib 0x030e5ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I believe the issue is that you are creating a new GameViewController and because you are doing that it is crashing. Looks like it has something to do with the fact it can't find a SKView because it isn't being loaded from a storyboard.
With that being said you don't want a new GameViewController you want to use the current one. There are a couple of ways to go about this. The simplest could be this...
However if you are using a navigation controller or something like that you may not get your GameViewController back and instead get a UINavigationController.
Best approach would be to create a delegate and assign it when you create your scene. This question has a bunch of different approaches to what you are trying to do. How do I present a UIViewController from SKScene? however your app is crashing because you are recreating the GameViewController which makes your question a bit different. One person does mention the delegate approach but you would be creating the protocol on Scene not the view controller.
Edit
https://stackoverflow.com/a/21917919/851041
Here is a good use of the delegate to comunicate back to your View Controller.
Hopefully that was helpful.