Spritekit Facebook quick change button

Hi my app is almost ready for release, but I want to add a Facebook share button. The thing is, I have no idea how the communication between the scene and the view controller works. i did my research but only found code in obj-c like this

- (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 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];
}

      

my experience and knowledge is too small to carry over this too quickly, so I need help with this D:

thank

+3


source to share


2 answers


This is some code I made for twitter a while ago, which is still fast. I'll show you how to convert it to Facebook below. Put this in your 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
        })
}

      

To convert it to Facebook, just replace SLServiceTypeTwitter

with SLServiceTypeFacebook

and rename the variables for readability. If you want to call this method from SKScene, you need to tell the viewController somehow that you want it to call the method.

My preferred way is to use NSNotificationCenter so that I send an alert from the scene and receive a viewController so that it fires the method. It's also incredibly easy to set up. In this scene, you need to put this line of code wherever you want to trigger the Facebook popup:



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

      

Sends a notification with a name. Now in the viewController you need to subscribe to this warning by putting the following code in viewDidLoad, viewDidAppear or something similar.

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

      

The scene will now bind to the viewController and you can show the Facebook sheet. Don't forget to replace my lines with the ones that fit your project. Hope this helps - sorry it was such a long answer!

+13


source


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 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()
}

      



A quick conversion of Obj-C answer posted by a very helpful user ...... original post

+1


source







All Articles