Phonegap iOS插件 - 如何在didFinishWithResult中访问回调

时间:2014-04-09 21:45:22

标签: ios phonegap-plugins

这是我的短信插件for phonegap的一些代码。我试图使回调正常工作:https://github.com/aharris88/phonegap-sms-plugin/issues/11

这是我正在处理的代码。您可以看到我在send方法的开头获得了回调函数,如下所示:

NSString* callback = command.callbackId;

然后我提出一个MFMessageComposeViewController,我需要在完成后调用该回调。所以我使用了messageComposeViewController:didFinishWithResult:,但是如何访问我需要调用的回调函数?

#import "Sms.h"
#import <Cordova/NSArray+Comparisons.h>

@implementation Sms

- (CDVPlugin *)initWithWebView:(UIWebView *)theWebView {
    self = (Sms *)[super initWithWebView:theWebView];
    return self;
}

- (void)send:(CDVInvokedUrlCommand*)command {

    NSString* callback = command.callbackId;

    if(![MFMessageComposeViewController canSendText]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice"
                                                        message:@"SMS Text not available."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }

    MFMessageComposeViewController* composeViewController = [[MFMessageComposeViewController alloc] init];
    composeViewController.messageComposeDelegate = self;

    NSString* body = [command.arguments objectAtIndex:1];
    if (body != nil) {
        [composeViewController setBody:body];
    }

    NSArray* recipients = [command.arguments objectAtIndex:0];
    if (recipients != nil) {
        [composeViewController setRecipients:recipients];
    }

    [self.viewController presentViewController:composeViewController animated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

#pragma mark - MFMessageComposeViewControllerDelegate Implementation
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    int webviewResult = 0;
    CDVPluginResult* pluginResult = nil;

    switch(result) {
        case MessageComposeResultCancelled:
            webviewResult = 0;
            break;
        case MessageComposeResultSent:
            webviewResult = 1;
            break;
        case MessageComposeResultFailed:
            webviewResult = 2;
            break;
        default:
            webviewResult = 3;
            break;
    }

    [self.viewController dismissViewControllerAnimated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

    if (webviewResult == 1) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:callback];
}

@end

1 个答案:

答案 0 :(得分:1)

您希望将回调ID存储为该类的属性。

@interface Sms

@property (nonatomic, strong) NSString *callbackId

@end

然后在你的发送方法中存储它。

- (void)send:(CDVInvokedUrlCommand*)command {

    self.callbackId = command.callbackId;

然后,您可以从委托方法再次访问它:

NSString *callbackId = self.callbackId;

你应该好好去。