有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
You must add the MessageUI.framework to your Xcode project Include an #import <MessageUI/MessageUI.h> in your header file Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate In your IBAction method declare instance of MFMessageComposeViewController say messageInstance To check whether your device can send text use [MFMessageComposeViewController canSendText] in an if condition, it'll return Yes/No In the if condition do these: First set body for your messageInstance as: messageInstance.body = @"Hello from Shah"; Then decide the recipients for the message as: messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil]; Set a delegate to your messageInstance as: messageInstance.messageComposeDelegate = self; In the last line do this: [self presentModalViewController:messageInstance animated:YES];
其他回答
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");
});
这是所有。短信发送。
如果你想在你自己的应用程序中显示创建和发送消息,你需要使用MFMessageComposeViewController。
否则,您可以使用sharedApplication方法。
添加MessageUI。框架并使用下面的代码
#import <MessageUI/MessageUI.h>
然后:
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = @"Your Message here";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
以及委托方法-
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
}
您可以使用sms:[目标电话号码]URL打开sms应用程序,但没有指示如何用文本预填充sms正文。
用这个:
- (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];
}
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务