gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:04:03 26 4
gpt4 key购买 nike

这是我的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

最佳答案

您需要将回调 ID 存储为类的属性。

@interface Sms

@property (nonatomic, strong) NSString *callbackId

@end

然后在您使用发送方法时存储它。

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

self.callbackId = command.callbackId;

然后您可以从您的委托(delegate)方法再次访问它:

NSString *callbackId = self.callbackId;

你应该可以开始了。

关于ios - Phonegap iOS 插件-如何访问 didFinishWithResult 中的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com