2014-04-09 1 views
0

이것은 phonegap에 대한 내 SMS 플러그인의 일부 코드입니다. 콜백이 올바르게 작동하도록하려고합니다. https://github.com/aharris88/phonegap-sms-plugin/issues/11Phonegap iOS 플러그인 - didFinishWithResult에서 콜백에 액세스하는 방법

다음 코드는 제가 작업하고 있습니다. 당신은 내가 이런 송신 방법의 시작 부분에 콜백 함수를 얻을 볼 수 있습니다

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 
+0

대리자 메서드에 중단 점을 넣었습니까? 메시지보기 컨트롤러가 완료되면 호출됩니까? – Joshua

+0

예, 자동으로 호출됩니다. 변수 NSString * 콜백에 액세스 할 수 없습니다. – aharris88

답변

1

클래스의 속성으로 콜백 ID를 저장하려고합니다.

@interface Sms 

@property (nonatomic, strong) NSString *callbackId 

@end 

송신 방법에있을 때 저장하십시오.

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

    self.callbackId = command.callbackId; 

그런 다음 대리자 메서드에서 다시 액세스 할 수 있습니다

NSString *callbackId = self.callbackId; 

당신은 갈 수 있어야한다.

+0

GitHub에서 당기기 요청을 추가하고 hide/show 상태 표시 줄 메서드도 제거했습니다. 행운을 빈다 : https://github.com/aharris88/phonegap-sms-plugin/pull/15 – Joshua