Posting score to facebook

谁说我不能喝 提交于 2019-12-07 12:32:10

问题


I created an app in SpriteKit with xcode where when the game is over, it shows you your score, and I want to add the feature to post your score to facebook. Pretty much all my code is in MyScene.m where it can't access presentViewController. Only my ViewController.m file can access that, so I tried calling a instance method in Viewcontroller from Myscene.m but I can't find a way to do that. The only way I have found calling methods from other files is using +(void) which is a class method I think.

Myscene.m:

    if (location.x < 315 && location.x > 261 && location.y < 404 && location.y > 361) {
 //if you click the post to facebook button (btw, location is a variable for where you tapped on the screen)

     [ViewController facebook];
                    }

ViewController.m:

+(void)facebook{

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
        facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [facebook setInitialText:@"initial text"];  
    }

    }

So that worked, and it called the facebook class method correctly, but when I put [self presentViewController:facebook animated:YES] after the setInitialText brackets, it gives me this error: No known class method for selector 'presentViewController:animated:'

By the way it lets me use presentViewController in a instance method but I can't call that method from inside the class method or from my Myscene file. Is there any way to either call an instance method from another file, or access presentViewController from a class method? Thanks


回答1:


You can either pass a reference for your View Controller to your SKScene or you can use NSNotificationCenter instead. I prefer to use the latter.

First make sure you have added the Social.framework to your project.

Import the social framework into your View Controller #import <Social/Social.h>

Then in your View Controller's viewDidLoad method add this code:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(createPost:)
                                         name:@"CreatePost"
                                       object:nil];

Next add this method to your View Controller:

-(void)createPost:(NSNotification *)notification
{
    NSDictionary *postData = [notification userInfo];
    NSString *postText = (NSString *)[postData objectForKey:@"postText"];
    NSLog(@"%@",postText);

    // build your tweet, facebook, etc...
    SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [self presentViewController:mySLComposerSheet animated:YES completion:nil];

}

At the appropriate location in your SKScene, (won game, lost game, etc...) add this code:

NSString *postText = @"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:postText forKey:@"postText"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreatePost" object:self userInfo:userInfo];

The above code sends a NSNotification, with text, which your View Controller will pick up and execute the specified method.



来源:https://stackoverflow.com/questions/23851178/posting-score-to-facebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!