有没有人知道,通过官方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];
其他回答
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *ui =resultimg.image;
pasteboard.image = ui;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"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];
}
快跑!
添加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];
}
你可以展示MFMessageComposeViewController,它可以发送短信,但是有用户提示(他点击发送按钮)。没有用户许可,不可能这样做。在iOS 11上,你可以做扩展,就像传入消息的过滤器,告诉iOS它是不是垃圾邮件。没有什么是短信不能做的
如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。
推荐文章
- 如何使用接口生成器创建的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
- 不区分大小写的比较