发布分数到Facebook [英] Posting score to facebook
问题描述
我在 SpriteKit
与xcode中创建了一个应用程序,当游戏结束时,它显示您的分数,我想添加功能将您的分数发布到Facebook 。几乎所有的代码都在 MyScene.m
中无法访问 presentViewController
。只有我的ViewController.m文件才能访问,所以我尝试从Myscene.m调用ViewController中的一个实例方法,但是找不到一个方法。我发现从其他文件调用方法的唯一方法是使用我认为的类方法 +(void)
。
Myscene.m:
if(location.x& 315&& location.x> 261&& location.y< 404&&& location.y> 361){
//如果你点击发布到Facebook按钮(btw,位置是您在屏幕上轻拍的位置的变量)
[ViewController facebook];
}
ViewController.m:
如果([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
SLComposeViewController * facebook = [[SLComposeViewController alloc] init ]。
facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebook setInitialText:@initial text];
}
}
Facebook类方法正确,但是当我在setInitialText括号后面放了 [self presentViewController:facebook animated:YES]
,它给了我这个错误:没有已知的类方法为选择器' presentViewController:animated:'
顺便说一下,我可以在实例方法中使用 presentViewController
,但是我不能从类方法或从我的Myscene文件中调用该方法。有没有办法从另一个文件调用实例方法,或从类方法访问 presentViewController
?谢谢
您可以将View Controller的参考资料传递给SKScene,也可以使用 NSNotificationCenter
我更喜欢使用后者。
首先确保您已将Social.framework添加到您的项目。
将社会框架导入您的View Controller #import< Social / Social.h>
然后在你的View Controller的viewDidLoad方法添加以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(createPost :)
名称:@CreatePost
对象:nil];
下一步将此方法添加到您的View Controller:
- (void)createPost:(NSNotification *)通知
{
NSDictionary * postData = [notification userInfo];
NSString * postText =(NSString *)[postData objectForKey:@postText];
NSLog(@%@,postText);
//构建您的推文,Facebook等...
SLComposeViewController * mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
在SKScene的适当位置,(赢得游戏,丢失的游戏等)添加以下代码:
NSString * postText = @我只是打败了最后一个级别。 ;
NSDictionary * userInfo = [NSDictionary dictionaryWithObject:postText forKey:@postText];
[[NSNotificationCenter defaultCenter] postNotificationName:@CreatePost对象:self userInfo:userInfo];
上述代码发送了一个NSNotification,带有文本,您的View Controller将会接收并执行指定的方法。
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
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.
这篇关于发布分数到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!