swift spritekit Facebook 分享按钮

Posted

技术标签:

【中文标题】swift spritekit Facebook 分享按钮【英文标题】:swift spritekit Facebook share button 【发布时间】:2014-08-31 19:39:21 【问题描述】:

嘿,我的应用几乎可以发布了,但我想添加一个 Facebook 分享按钮。问题是我不知道场景和视图控制器之间的通信是如何工作的。我做了我的研究,但只在 obj-c 中找到了像这样的代码

- (void)lkFaceBookShare 
    NSString *serviceType = SLServiceTypeFacebook;
    if (![SLComposeViewController isAvailableForServiceType:serviceType])
    
        [self showUnavailableAlertForServiceType:serviceType];
    
    else
    
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        CGRect rect = [keyWindow bounds];
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.5f);
        [self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [composeViewController addImage:viewImage];
        NSString *initalTextString = [NSString stringWithFormat:@"Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8"];
        [composeViewController setInitialText:initalTextString];
        UIViewController *vc = self.view.window.rootViewController;
        [vc presentViewController:composeViewController animated:YES completion:nil];
    


- (void)showUnavailableAlertForServiceType:(NSString *)serviceType

    NSString *serviceName = @"";

    if (serviceType == SLServiceTypeFacebook)
    
        serviceName = @"Facebook";
    
    else if (serviceType == SLServiceTypeSinaWeibo)
    
        serviceName = @"Sina Weibo";
    
    else if (serviceType == SLServiceTypeTwitter)
    
        serviceName = @"Twitter";
    

    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Account"
                              message:[NSString stringWithFormat:@"Please go to the device settings and add a %@ account in order to share through that service", serviceName]
                              delegate:nil
                              cancelButtonTitle:@"Dismiss"
                              otherButtonTitles:nil];
    [alertView show];

我的经验和知识太少,无法快速移植,所以我需要一些关于这个 D 的帮助:

谢谢

【问题讨论】:

Swift and Objective-C in the Same Project,他们让在 Swift 中使用现有的 Objective-C 类变得很容易。 【参考方案1】:

这是我不久前为 twitter 编写的一些代码,它仍然可以在 swift 中运行。我在下面向您展示如何将其转换为 Facebook。把它放在你的 viewController 中:

func showTweetSheet() 
    let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
    tweetSheet.completionHandler = 
        result in
        switch result 
        case SLComposeViewControllerResult.Cancelled:
            //Add code to deal with it being cancelled
            break

        case SLComposeViewControllerResult.Done:
            //Add code here to deal with it being completed
            //Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
            break
        
    

    tweetSheet.setInitialText("Test Twitter") //The default text in the tweet 
    tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
    tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on

    self.presentViewController(tweetSheet, animated: false, completion: 
        //Optional completion statement
        )

要将其转换为 Facebook,只需将 SLServiceTypeTwitter 交换为 SLServiceTypeFacebook 并重命名变量以提高可读性。如果你想从 SKScene 调用这个方法,你必须以某种方式提醒 viewController 你希望它调用一个方法。

我的首选方法是使用 NSNotificationCenter,以便我从场景中发布警报,并由 viewController 接收,以便它触发一个方法。这也非常容易设置。在场景中,您需要将这行代码放在您想要调用 Facebook 弹出窗口的任何位置:

NSNotificationCenter.defaultCenter().postNotificationName("WhateverYouWantToCallTheNotification", object: nil)

这会发出一个带有名称的通知。现在,在 viewController 中,您需要通过将以下代码放入 viewDidLoad、viewDidAppear 或类似的东西中来订阅此警报。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "ThisIsTheMethodName", name: "WhateverYouCalledTheAlertInTheOtherLineOfCode", object: nil)

现在场景将与 viewController 通信,您将能够显示 Facebook 表单。请记住将我的字符串替换为与您的项目相关的字符串。希望这会有所帮助 - 抱歉,答案太长了!

【讨论】:

我可以使用 facebook 标志作为分享按钮吗?非常有帮助学习了我需要的所有东西 thx @BasileBerckmoes 我查了一下,你可以在 4 个(并且只有 4 个)场景中使用“f”标志。如果您将其用于其他用途,则违反了规则。做你想要的: 1. 提及你在 Facebook 上的存在,例如你的主页、时间线、群组、应用程序或活动 2. 暗示你在你的网站/应用程序上实施 Facebook 3. 你的产品与 Facebook 的集成,例如4. 源自 Facebook 的内容 @BasileBerckmoes 但是,如果您使用多个社交媒体(例如 twitter)并将徽标并排放置,那么您可以使用图标做您喜欢的事情 - 并且规则不允许再申请。不知道为什么会这样,但我个人会利用它并使用我发布的相同代码添加到 Twitter - 这样你就不会违反任何版权法。希望这可以解决问题。查看此网站,了解您可能合并的几乎所有公司的信息qualitylogoproducts.com/blog/… @Darvydas 有点用,是的,我可能会研究一些 Swift 2.0 的调整,Facebook 也提供了一些新的限制——你不能再预定义文本。换句话说,对于 Facebook,setInitialText() 现在不再进行任何更改。否则它应该按预期运行。 @JackC 一切都很好,但我更喜欢 `self.view?.window?.rootViewController?.presentViewController(tweetSheet, animated: false, completion: //可选的完成语句 )` :) 【参考方案2】:
func lkFaceBookShare() 
    var serviceType: String = SLServiceTypeFacebook
    if !SLComposeViewController.isAvailableForServiceType(serviceType) 
        self.showUnavailableAlertForServiceType(serviceType)
    
    else 
        var composeViewController: SLComposeViewController = SLComposeViewController.composeViewControllerForServiceType(serviceType)
        var keyWindow: UIWindow = UIApplication.sharedApplication().keyWindow
        var rect: CGRect = keyWindow.bounds
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0.5)
        self.view!.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
        var viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        composeViewController.addImage(viewImage)
        var initalTextString: String = String(format: "Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8")
        composeViewController.initialText = initalTextString
        var vc: UIViewController = self.view.window.rootViewController
        vc.presentViewController(composeViewController, animated: true, completion:  _ in )
    


func showUnavailableAlertForServiceType(serviceType: String) 
    var serviceName: String = ""
    if serviceType == SLServiceTypeFacebook 
        serviceName = "Facebook"
    
    else if serviceType == SLServiceTypeSinaWeibo 
        serviceName = "Sina Weibo"
    
    else if serviceType == SLServiceTypeTwitter 
        serviceName = "Twitter"
    

    var alertView: UIAlertView = UIAlertView(title: "Account", message: "Please go to the device settings and add a \(serviceName) account in order to share through that service", delegate: nil, cancelButtonTitle: "Dismiss", otherButtonTitles: "")
    alertView.show()

一位非常有帮助的用户发布的 Obj-C 答案的 Swift 转换......original post

【讨论】:

以上是关于swift spritekit Facebook 分享按钮的主要内容,如果未能解决你的问题,请参考以下文章

Swift / SpriteKit:如何创建水滴效果?

Swift,SpriteKit:如何保存场景的整个进度

将 SKSpriteNode 移动 Y - Swift 2 + SpriteKit

SpriteKit 设备方向改变导致 SKLabelNode 变形 Swift

这个带有 SpriteKit 代码的 Swift 没有达到预期的效果

SpriteKit,Swift - 滚动视图不显示