George
George

Reputation: 522

Can't get a Share Button to work in my iOS App?

I can't get a Share button to work in my current app, I successfully had one in my previous app but I can't get it to work in my new app. I think this may be due to my first app having only a single ViewController whereas this app has multiple. I am trying to do this in a different view controller, not the default main one. Not sure what I'm doing wrong.

//  MoreMenuViewController.h

#import <UIKit/UIKit.h>

@interface MoreMenuViewController : UIViewController

- (IBAction)tellFriend:(id)sender;


//  MoreMenuViewController.m

#import "MoreMenuViewController.h"

@implementation MoreMenuViewController

- (IBAction)tellFriend:(id)sender 
{   
    NSString *shareText = [NSString stringWithFormat:@"Check out Stories With Friends, a new word game for iPhone!"]; // Share message
    NSArray *itemsToShare = @[shareText];
    UIActivityViewController *activityVC =
    [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    activityVC.excludedActivityTypes = @[];

    [MoreMenuViewController:activityVC animated:YES completion:nil];
}

For the last line of code I get an error: No known class method for selector 'animated:completion:.'

Help would be much apperciated, thanks!

Upvotes: 0

Views: 747

Answers (2)

james075
james075

Reputation: 1278

You need to call this method from self instead of MoreMenuViewController:

[self presentViewController:activityVC
                   animated:YES
                 completion:nil];

You also don't need to call this setter if you don't need to excluded types:

activityVC.excludedActivityTypes = @[];

UPDATE:

Despite now being able to click the button the app crashes: -[MoreMenuViewController tellAFriend:]

It's because the name of your method is tellFriend: and NOT tellAFriend: you must had renamed your method from the code without refactoring so the linked IBAction has no clue of the change. What you need to do is remove this link made from your Storyboard to your implementation file.

Click on your button then click on the cross where the action is linked to tellAFriend:

enter image description here

Upvotes: 3

davbryn
davbryn

Reputation: 7176

Try [self presentViewController:activityVC animated:YES completion:nil];

instead

Upvotes: 0

Related Questions