有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
遵循以下步骤
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];
}
快跑!
其他回答
你可以展示MFMessageComposeViewController,它可以发送短信,但是有用户提示(他点击发送按钮)。没有用户许可,不可能这样做。在iOS 11上,你可以做扩展,就像传入消息的过滤器,告诉iOS它是不是垃圾邮件。没有什么是短信不能做的
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");
});
这是所有。短信发送。
你可以使用这种方法:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:MobileNumber"]]
iOS会自动从你的应用程序导航到消息应用程序的消息合成页面。由于URL的方案是从sms:开始的,这被标识为消息应用程序可以识别的类型,并启动它。
//Add the Framework in .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
//Set the delegate methods
UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
//add the below code in .m file
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= @"Hello";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
@"", nil];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:
(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(@"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(@"Failed");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
下面是在iOS中发送短信的Swift版本代码。请注意,它只在实际设备中工作。在iOS 7+中测试的代码。你可以在这里阅读更多。
1)创建一个继承MFMessageComposeViewControllerDelegate和NSObject的新类:
import Foundation
import MessageUI
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(textMessageRecipients:[String] ,textBody body:String) -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = body
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
2)如何使用这个类:
func openMessageComposerHelper(sender:AnyObject ,withIndexPath indexPath: NSIndexPath) {
var recipients = [String]()
//modify your recipients here
if (messageComposer.canSendText()) {
println("can send text")
// Obtain a configured MFMessageComposeViewController
let body = Utility.createInvitationMessageText()
let messageComposeVC = messageComposer.configuredMessageComposeViewController(recipients, textBody: body)
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
self.displayAlerViewWithTitle("Cannot Send Text Message", andMessage: "Your device is not able to send text messages.")
}
}
推荐文章
- 如何使用接口生成器创建的nib文件加载UIView
- iOS如何设置应用程序图标和启动图像
- 更改UITextField和UITextView光标/插入符颜色
- 'Project Name'是通过优化编译的——步进可能会表现得很奇怪;变量可能不可用
- 如何设置回退按钮文本在Swift
- 模拟器慢动作动画现在打开了吗?
- 如何为TableView创建NSIndexPath
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)
- 如何比较两个nsdate:哪个是最近的?
- 使UINavigationBar透明
- 如何改变推和弹出动画在一个基于导航的应用程序
- 删除/重置核心数据中的所有条目?
- Swift to Objective-C头未在Xcode 6中创建
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded
- 不区分大小写的比较