Question

I'm probably not explaining this logically, as I'm new to Objective-C, but here I go...

I am writing an application in Objective-C that interacts with a WebView. Part of the app involves sharing an image via NSSharingService that is currently displayed in the WebView. Consequently, I have a method like this defined in my AppDelegate.m file:

#import "CCAppDelegate.h"
#import <WebKit/WebKit.h>
#import <AppKit/AppKit.h>

@implementation CCAppDelegate

    -(void)shareFromMenu:(id)sender shareType:(NSString *)type{
        NSString *string = [NSString stringWithFormat: @"window.function('%@')", type];
        [self.webView stringByEvaluatingJavaScriptFromString: string];
    }

@end

I then have a subclass of NSMenu, defined in CCShareMenu.m, which creates a menu of available sharing options:

#import "CCShareMenu.h"

@implementation CCShareMenu

- (void)awakeFromNib{
    [self setDelegate:self];
}

- (IBAction)shareFromService:(id)sender {
    NSLog(@"%@", [sender title]);
    // [CCAppDelegate shareFromMenu]; 
}

- (void)menuWillOpen:(NSMenu *)menu{
    [self removeAllItems];
    NSArray *shareServicesForItems = @[
        [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage],
        [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail],
        [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnFacebook],
        [NSSharingService sharingServiceNamed:NSSharingServiceNamePostOnTwitter]
    ];
    for (NSSharingService *service in shareServicesForItems) {
        NSMenuItem *item = [[NSMenuItem alloc] init];
        [item setRepresentedObject:service];
        [item setImage:[service image]];
        [item setTitle:[service title]];
        [item setTarget:self];
        [item setAction:@selector(shareFromService:)];
        [self addItem:item];
    }
}

@end

These methods both work fine on their own, except I need to call the shareFromMenu method from within the shareFromService IBAction.

I attempted moving the IBAction method to AppDelegate.m, then realized that made zero sense as the menuWillOpen-created selectors would never find the correct methods. Similarly, I tried following the instructions posted here, but:

[CCAppDelegate shareFromMenu];

Also responded with an error saying that the method was not found.

I realize I'm doing something fundamentally wrong here, so guidance would be appreciated.

Was it helpful?

Solution

-[CCAppDelegate shareFromMenu]

is different from

-[CCAppDelegate shareFromMenu:shareType:]

I would try adding the following to CCAppDelegate.h between @interface and @end:

-(void)shareFromMenu:(id)sender shareType:(NSString *)type

Then change your shareFromService: method to something like:

- (IBAction)shareFromService:(id)sender
{
    NSString *shareType = @"Set your share type string here.";

    CCAppDelegate *appDelegate = (CCAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate shareFromMenu:sender shareType:shareType];
}

OTHER TIPS

-(void)shareFromMenu is a member method, but

[CCAppDelegate shareFromMenu]

is calling a class function which is not the correct way to call a member function.

You may try to get the CCAppDelegate instance and then call the function like this

CCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate shareFromMenu];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top