Question

I am making casual game now.
I'd like to share the score with twitter.
My this work image is that below.

1.After game player plays the game and there is the score.
2.When the game goes to gameoverscene, they can push twitter button (using social.framework)
3. There is a text like "You got xx score !!" in the twitterdisplay(modal).
*I would like to change xx to the shared score.

Could you please give me some advices to do that ? I am a beginner of Objective - C. So, easy way is better for me. Little by little, I'd like to concern about security and scalability.

[Information] *MainScene and GameOverScene are made from SKScene

MainScene.m

@implementation MainScene {

//The score
NSInteger _score;

}

- (void)incrementScore
{
_score++;
}

GameOverScene.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

 if([node.name isEqualToString:@"twitterbutton"]){
    NSLog(@"self.delegate = %@",self.delegate);
    [self.delegate showShareScreen];

    //delegate to ViewController
    if (nil == self.delegate) NSLog(@"delegate is nil");
    }    
}

ViewController.m

-(void)showShareScreen
{
NSLog(@"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
    tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [tweetSheet setInitialText:@"You got xx score"];
     NSLog(@"self = %@",self);
    [self presentViewController:tweetSheet animated:YES completion:nil];

 }
 else {
     NSLog(@"not sls type twitter");
 }
 }
Was it helpful?

Solution

Change your line in delegate method in ViewController

[tweetSheet setInitialText:@"You got xx score"];

to as shown below; to include the score with a NSString's stringWithFormat: method

-(void)showShareScreenWithScore:(NSInteger) score {
NSLog(@"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
    tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [tweetSheet setInitialText:[NSString stringWithFormat:@"You got %d score",score]];
     NSLog(@"self = %@",self);
    [self presentViewController:tweetSheet animated:YES completion:nil];

 }
 else {
     NSLog(@"not sls type twitter");
 }
}

And from GameOverScene call this delegate method. Note: When you are creating Game over scene at that time after object instantiation you can set the property of GameOverScene. So just declare a property

@property (assign , nonatomic) NSInteger score;

and set its value Using NSURLSession that the game is getting over. And score value you can get from MainScene score property.


NSURLSession

The NSURLSession class and related classes provide an API for downloading content via HTTP. This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top