3

Hey there 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 viewcontroler works. i did my research but only found code in obj-c like this one

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

my experience and knowledge is too low to port this too swift so i need some help with this D:

Thanks

  • [Swift and Objective-C in the Same Project](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html), they made it easy to use existing Objective-C classes in Swift. – chazkii Aug 31 '14 at 19:57

2 Answers2

13

This is some code I did for twitter a while ago which still works in swift. I show you how to convert it to Facebook below. Put this 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, simply swap SLServiceTypeTwitter to SLServiceTypeFacebook and rename the variables for readability. If you want to call this method from the SKScene, you have to somehow alert the viewController that you want it to call a method.

My preferred way is to use NSNotificationCenter so that I post an alert from the scene and it is received by the viewController so that it fires a method. This is also incredibly easy to setup. In the scene, you need to put this line of code wherever you want to call the Facebook popup:

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

This sends out a notification with a name. Now, in the viewController you need to subscribe to this alert by putting the following code in either viewDidLoad, viewDidAppear or something similar.

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

Now the scene will communicate with the viewController and you will be able to show the Facebook sheet. Remember to replace my strings with ones relative to your project. Hope this helps - sorry it was such a long answer!

Jack Chorley
  • 2,309
  • 26
  • 28
  • am I allowed to use the facebook logo as the share button ? very helpfull learned all the things i needed thx – Basile Berckmoes Sep 02 '14 at 18:43
  • @BasileBerckmoes I looked it up, you can use the "f" logo in 4 (and only 4) scenarios. If you use it for something else, you break the rules. Make what you want of these: 1. To refer to your presence on Facebook, such as your Page, timeline, group, app or event 2. Imply your implementation of Facebook on your website/app 3. Your product’s integration with Facebook, such as ‘For use with Facebook' 4. Content that originates from Facebook – Jack Chorley Sep 02 '14 at 18:47
  • @BasileBerckmoes However, if you use more than one social media (such as twitter) and put the logos side by side, then you are permitted to do what you like with the icons - and the rules no longer apply. No idea why this is so, but personally I would take advantage of it and add in twitter too using the same code I posted - that way you won't break any copyright law. Hopefully that clears things up. Check out this website for information on pretty much any company you are likely to incorporate http://www.qualitylogoproducts.com/blog/social-media-logos-promotional-items/ – Jack Chorley Sep 02 '14 at 18:52
  • @Darvydas It somewhat works yes, there may be some Swift 2.0 tweaks which I shall look into, and Facebook have also provided some new limitations - you can no longer predefine text. In other words, for Facebook, setInitialText() now longer makes no change. Otherwise it should function as expected. – Jack Chorley Nov 23 '15 at 18:09
  • 1
    @JackC well everything is OK but i preffer ` self.view?.window?.rootViewController?.presentViewController(tweetSheet, animated: false, completion: { //Optional completion statement })` :) – Darvas Nov 24 '15 at 07:10
1
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()
}

Swift Conversion of Obj-C answer posted by a very helpful user...... original post

Community
  • 1
  • 1
upallnight
  • 89
  • 12