如何在iPhone中发送应用内短信

3

我正在开发一款信息传递应用。

我想要发送自动短信给我的客户,作为验证代码。用户可以使用该代码进行验证。

请问有谁能帮助我如何自动发送短信到我的客户端?

例如:

如果用户点击注册按钮,他/她将会从iPhone客户端自动收到确认短信。这就是我需要实现的功能。

谢谢。


你可以使用MFMessageComposeViewController并填充信息,如电话号码、短信内容等,或者如果你愿意,也可以发送后台短信,而不会实际通知用户正在使用某些短信服务发送短信。但我不知道苹果是否会批准具有这种短信功能的应用程序。 - stack2012
3个回答

3

在iOS上无法自动发送短信,但是可以显示短信视图并预先填写短信内容。

有关如何使用MFMessageComposeViewController的更多信息,请参见此处:http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

- (void) sendSMSCode:(NSString *)smsCode {
    MFMessageComposeViewController *viewController = [[MFMessageComposeViewController alloc] init];
    viewController.messageComposeDelegate = self;

    [viewController setBody:smsCode];
    // Replace with the number to send the SMS so
    [viewController setRecipients:[NSArray arrayWithObject:@"+123456789"]];
    [self presentModalViewController:viewController animated:YES];
    [viewController release], viewController = nil;
}

#pragma mark - MFMessageComposeViewController methods

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

    [controller dismissModalViewControllerAnimated:YES];

    if (result == MessageComposeResultFailed) {
             // handle the error.
    }
}

谢谢..我已经去过那里,但是没有找到任何示例..我需要一个参考示例..而且自动意味着它会弹出一个安全码进行验证...你有这样的代码吗? - Vivek2012
谢谢你提供的代码,但它仍然无法正常工作。你能帮我解决一下吗?我该如何对客户在注册表单中输入的数字进行编码和解码?请在这方面给我一些帮助... - Vivek2012
我想将手机号码编码为5位数字,然后在按钮点击事件中解码为相同的号码。你明白吗? - Vivek2012
看,我有一个10位数字..现在我想从中生成带有任何符号(*,#)的5位数字,然后我还想将该数字解码为原始的10位数字..你能帮我吗?现在你明白了吗? - Vivek2012
这与您的第一个问题和答案无关,请为此创建一个新问题。 这就是StackOverflow的工作方式。 - rckoenes

1
发送应用内短信与发送应用内邮件非常相似,但有一些小差别。使用MessageUI框架来发送应用内短信。
-(IBAction) sendInAppSMS:(id) sender
{
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello from Mugunth";
        controller.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil];
        controller.messageComposeDelegate = self;
        [self presentModalViewController:controller animated:YES];
    }
}

谢谢,但我已经尝试过这段代码了,但它显示错误并且无法工作。你知道如何在iPhone参数化地编码和解码数字(手机号码)吗?你有相关的代码吗? - Vivek2012
这个不起作用...我已经为此创建了另一个文件。我正在另一个文件中执行它...你能告诉我如何访问这个函数吗?我已经在Smsmanager.m中创建了这个函数,并在另一个文件的按钮单击事件中使用它...像[smsmanager sendInAppSMS],但它显示错误...我需要在sendInAppSMS后面写什么? - Vivek2012
抱歉回复晚了,您只需告诉我您想要完成的简单步骤,然后我就能够适当地回复您。 - user755278

1

这是关于编码/解码的扩展问题:

#import <Foundation/Foundation.h>


@interface Base64 : NSObject {

}

+ (void) initialize;

+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length;

+ (NSString*) encode:(NSData*) rawBytes;

+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength;

+ (NSData*) decode:(NSString*) string;

@end




#import "Base64.h"


@implementation Base64

#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))


static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[128];

+ (void) initialize {
    if (self == [Base64 class]) {
        memset(decodingTable, 0, ArrayLength(decodingTable));
        for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
            decodingTable[encodingTable[i]] = i;
        }
    }
}


+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];
        output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data
                                  encoding:NSASCIIStringEncoding] autorelease];
}


+ (NSString*) encode:(NSData*) rawBytes {
    return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}


+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength {
    if ((string == NULL) || (inputLength % 4 != 0)) {
        return nil;
    }

    while (inputLength > 0 && string[inputLength - 1] == '=') {
        inputLength--;
    }

    NSInteger outputLength = inputLength * 3 / 4;
    NSMutableData* data = [NSMutableData dataWithLength:outputLength];
    uint8_t* output = data.mutableBytes;

    NSInteger inputPoint = 0;
    NSInteger outputPoint = 0;
    while (inputPoint < inputLength) {
        char i0 = string[inputPoint++];
        char i1 = string[inputPoint++];
        char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
        char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';

        output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
        if (outputPoint < outputLength) {
            output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
        }
        if (outputPoint < outputLength) {
            output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
        }
    }

    return data;
}


+ (NSData*) decode:(NSString*) string {
    return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];
}

@end

希望这能对你有所帮助。


谢谢。现在关于使用SMS API发送iOS验证号码怎么办?我不知道如何编写代码,你能帮我吗? - Vivek2012

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接