有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?


当前回答

您可以使用sms:[目标电话号码]URL打开sms应用程序,但没有指示如何用文本预填充sms正文。

其他回答

遵循以下步骤

1 .添加消息i。项目框架

2。在.h文件中导入# Import <MessageUI/MessageUI.h>。

3.复制此代码以发送消息

 if ([MFMessageComposeViewController canSendText]) {
    MFMessageComposeViewController *messageComposer =
    [[MFMessageComposeViewController alloc] init];
    NSString *message = @"Message!!!";
    [messageComposer setBody:message];
    messageComposer.messageComposeDelegate = self;
    [self presentViewController:messageComposer animated:YES completion:nil];
}

4所示。实现委托方法,如果你想。

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


   ///your stuff here 

    [self dismissViewControllerAnimated:YES completion:nil];
}

快跑!

用这个:

- (void)showSMSPicker
{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));

    if (messageClass != nil) {          
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
           [self displaySMSComposerSheet];
        }   
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{       
    //feedbackMsg.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MessageComposeResultCancelled:
        {   
            UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert1 show];
            [alert1 release];
        }   

        // feedbackMsg.text = @"Result: SMS sending canceled";
        break;

        case MessageComposeResultSent:
        {
            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert2 show];
            [alert2 release];
        }   

        // feedbackMsg.text = @"Result: SMS sent";
        break;

        case MessageComposeResultFailed:
        {   
            UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert3 show];
            [alert3 release];
        }   

        // feedbackMsg.text = @"Result: SMS sending failed";
        break;

        default:
        {   
            UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert4 show];
            [alert4 release];
        }   

        // feedbackMsg.text = @"Result: SMS not sent";
        break;
    }

    [self dismissModalViewControllerAnimated: YES];
}

如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。

如果你想在你自己的应用程序中显示创建和发送消息,你需要使用MFMessageComposeViewController。

否则,您可以使用sharedApplication方法。

MacOS中进程间通信的系统之一是XPC。该系统层是基于libSystem和launchd的plist结构传输而开发的进程间通信层。事实上,它是一个允许通过交换字典等结构来管理进程的接口。由于遗传,iOS 5也拥有这种机制。

你可能已经明白我的介绍是什么意思。是的,iOS中的系统服务包括用于XPC通信的工具。我想用一个用于SMS发送的守护进程来举例说明工作。但是,应该提到的是,这个能力在iOS 6中是固定的,但与iOS 5.0-5.1.1相关。越狱、私有框架和其他非法工具都不需要使用它。只需要目录/usr/include/xp /*中的头文件集。

iOS中用于发送SMS的元素之一是系统服务com.apple。Chatkit,其任务包括生成、管理和发送短文本消息。为了便于控制,它具有公开可用的通信端口com.apple.chatkit.clientcomposeserver.xpc。使用XPC子系统,您可以在不需要用户批准的情况下生成和发送消息。

好吧,让我们试着建立联系。

xpc_connection_t myConnection;

dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT);

myConnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);

现在我们已经将XPC连接myConnection设置为SMS发送服务。但是,XPC配置提供了创建挂起连接的功能——我们需要再采取一步来激活。

xpc_connection_set_event_handler(myConnection, ^(xpc_object_t event){
xpc_type_t xtype = xpc_get_type(event);
if(XPC_TYPE_ERROR == xtype)
{
NSLog(@"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
}
// Always set an event handler. More on this later.

NSLog(@"Received a message event!");

});

xpc_connection_resume(myConnection);

连接已激活。此时此刻,iOS 6将在电话日志中显示一条消息,禁止这种类型的通信。现在,我们需要生成一个类似于xpc_dictionary的字典,其中包含消息发送所需的数据。

NSArray *recipient = [NSArray arrayWithObjects:@"+7 (90*) 000-00-00", nil];

NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient format:200 options:0 error:NULL];

xpc_object_t mydict = xpc_dictionary_create(0, 0, 0);
xpc_dictionary_set_int64(mydict, "message-type", 0);
xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]);
xpc_dictionary_set_string(mydict, "text", "hello from your application!");

剩下的不多了:将消息发送到XPC端口,并确保它被交付。

xpc_connection_send_message(myConnection, mydict);
xpc_connection_send_barrier(myConnection, ^{
NSLog(@"The message has been successfully delivered");
});

这是所有。短信发送。