有没有人知道,通过官方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。有一些发送短信的方法。