gpt4 book ai didi

facebook - 将分数发布到 Facebook

转载 作者:行者123 更新时间:2023-12-02 20:22:20 25 4
gpt4 key购买 nike

我在 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) {
//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"];
}

}

这样就成功了,它正确地调用了 facebook 类方法,但是当我输入 [self presentViewController:facebook animated:YES] 时在 setInitialText 括号之后,它给了我这个错误:没有选择器 '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:)
name:@"CreatePost"
object:nil];

接下来将此方法添加到您的 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];

}

在 SKScene 中的适当位置(赢得比赛、输掉比赛等...)添加以下代码:

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

上面的代码发送一个带有文本的 NSNotification,您的 View Controller 将拾取该文本并执行指定的方法。

关于facebook - 将分数发布到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23851178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com