小编典典

swift spritekit Facebook分享按钮

swift

嘿,我的应用程序几乎可以发布了,但是我想添加一个Facebook分享按钮。事情是我不知道场景和ViewController之间的通信是如何工作的。我做了我的研究,但只在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相关的帮助:

谢谢


阅读 350

收藏
2020-07-07

共1个答案

小编典典

这是我不久前为twitter做的一些代码,现在仍然可以迅速使用。我在下面向您展示如何将其转换为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的,只是换SLServiceTypeTwitterSLServiceTypeFacebook并重命名变量可读性。如果要从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工作表。请记住用与您的项目相关的字符串替换我的字符串。希望这会有所帮助-
很抱歉,回答这么长!

2020-07-07