سؤال

I just started programming Apps and so on. So my first App is a "simple" Game with a timer function. My problem is this: I want to give the user a function with it he can share his points on Twitter and Facebook, but the output of the game points are always 0.. I want to fix this and need your help for this.

Screenshots

http://techstern.de/app/screenshot1.png
http://techstern.de/app/screenshot3.png


*Sorry, I´m from german, so here's the translation:

"KlickMich" -> "TapMe"
"Zeit" -> "Time"
"Punktestand" -> "Points"
"Die Zeit ist um" -> "Time is up"
"Du hast 11 Punkte erzielt" -> "You scored 11 Points"
"Auf Facebook teilen" -> "Share on Facebook"
"Auf Twitter teilen" -> "Share on Twitter"
"Nochmal spielen" -> "Play again"
"Habe gerade 0 Punkte in KlickMich erreicht..." -> "Just scored 0 points in TapMe..."!


Looking forward to your reactions :)

Yours faithfully
Robin


Code: ViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.m"

@interface ViewController : UIViewController<UIAlertViewDelegate> {
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;

NSInteger count;
NSInteger seconds;
NSTimer *timer;

int time;
}

- (IBAction) buttonPressed;
- (void)setupGame;
- (void)subtractTime;
- (IBAction)postToTwitter;
- (IBAction)postToFacebook;

@end

Code: ViewController.m

#import "ViewController.h"
#import <UIKit/UIAlertView.h>
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)buttonPressed{
count++;

scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

}

- (void)setupGame{
seconds = 15;
count = 0;

timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];
scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                         target:self
                                       selector:@selector(subtractTime)
                                       userInfo:nil
                                        repeats:YES];

}

- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];

if (seconds == 0) {
    [timer invalidate];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Die Zeit ist um"
                                                    message:[NSString       stringWithFormat:@"Du hast %li Punkte erzielt",(long)count]
                                                   delegate:self
                                          cancelButtonTitle:@"Nochmal spielen"
                                          otherButtonTitles:@"Auf Facebook teilen", @"Auf Twitter teilen", nil];
    
    [alert show];
}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

[self setupGame];

if (buttonIndex == 1)
{
    [self postToTwitter];
}
else if (buttonIndex == 2)
{
    [self postToFacebook];
}
}

- (IBAction)postToTwitter{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}
} 

- (IBAction)postToFacebook{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
    [controller setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [self presentViewController:controller animated:YES completion:Nil];
}
}

@end
هل كانت مفيدة؟

المحلول

I review your code. You reset your timer first, then you are send score to share on Twitter.

First share on Twitter and then you have to call setupGame method like this :

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
    if (buttonIndex == 1)
    {
       [self postToFacebook]; 
    }  
    else if (buttonIndex == 2)
    {
       [self postToTwitter];
    }

   [self setupGame]; // after share you can reset
 }

And call this method like -(void)postToTwitter not like - (IBAction)postToTwitter. because there is no connection to that method. you are calling that method not connect with a button action.

نصائح أخرى

this is your required answer. Please go through this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0)
  {
    [self setupGame];
    NSLog(@"Cancel..");
  }
  else if (buttonIndex == 1)
  {
    NSLog(@"Facebook..");
    [self postToFacebook];
  }
  else if (buttonIndex == 2)
  {
    NSLog(@"Twitter..");
    [self postToTwitter];
  }
}
- (void)postToTwitter
{
   SLComposeViewController *tweetSheet=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//     if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
//     {
     SLComposeViewControllerCompletionHandler __block    completionHandler=^(SLComposeViewControllerResult result){

      [tweetSheet dismissViewControllerAnimated:YES completion:nil];

        switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
            {
                NSLog(@"Cancel");
                [self setupGame];
            }
                break;
            case SLComposeViewControllerResultDone:
            {
                NSLog(@"Post");
                [self setupGame];
            }
                break;
        }
    };

    [tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [tweetSheet setCompletionHandler:completionHandler];
    [self presentViewController:tweetSheet animated:YES completion:nil];
  //}

 }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top