在本文中,我们将给您介绍关于使用Swift在iOS中发送短信的详细内容,并且为您解答swift消息发送的相关问题,此外,我们还将为您提供关于iOSApp–使用Swift在后台发送邮件[复制]、ios–
在本文中,我们将给您介绍关于使用Swift在iOS中发送短信的详细内容,并且为您解答swift 消息发送的相关问题,此外,我们还将为您提供关于iOS App – 使用Swift在后台发送邮件[复制]、ios – 使用swift读取短信、ios – 在Swift 2.3中使用swift 3框架、ios – 在Swift中使用Twilio发送短信的知识。
本文目录一览:- 使用Swift在iOS中发送短信(swift 消息发送)
- iOS App – 使用Swift在后台发送邮件[复制]
- ios – 使用swift读取短信
- ios – 在Swift 2.3中使用swift 3框架
- ios – 在Swift中使用Twilio发送短信
使用Swift在iOS中发送短信(swift 消息发送)
首先,我真的很惊讶这不是重复的,因为在Objective-C中有大量的堆栈溢出问题可以解决这个问题,但是我还没有看到使用Swift的好答案。
我正在寻找的是Swift中的代码段,该代码段将任意字符串作为文本消息的正文发送到给定的电话号码。从本质上讲,我希望像这样从苹果的官方文档,但是在斯威夫特,而不是Objective-
C的。
我认为这并不是太困难,因为可以在Android中只需几行代码即可完成。
编辑:我正在寻找的是5-20行Swift代码,我不同意这个范围太广。在Java(适用于Android)中,解决方案如下所示:
package com.company.appname;import android.app.Activity;import android.telephony.SmsManager;public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); public static final mPhoneNumber = "1111111111"; public static final mMessage = "hello phone"; SmsManager.getDefault().sendTextMessage(mPhoneNumber, null, mMessage, null, null); }}
现在这是android解决方案,只有11行。Java往往比Swift更冗长,因此我怀疑我的要求是“太宽泛了”,很可能是我不知道如何使用Objective-C
MessageComposer对象,因为我链接的文档关于Swift中的用法,上述内容尚不清楚。
答案1
小编典典不知道您是否真的得到了答案。我也遇到了类似的情况,遇到了这个解决方案并使它起作用。
import UIKitimport MessageUIclass ViewController: UIViewController, MFMessageComposeViewControllerDelegate { @IBOutlet weak var phoneNumber: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func sendText(sender: UIButton) { if (MFMessageComposeViewController.canSendText()) { let controller = MFMessageComposeViewController() controller.body = "Message Body" controller.recipients = [phoneNumber.text] controller.messageComposeDelegate = self self.presentViewController(controller, animated: true, completion: nil) } } func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { //... handle sms screen actions self.dismissViewControllerAnimated(true, completion: nil) } override func viewWillDisappear(animated: Bool) { self.navigationController?.navigationBarHidden = false }}
iOS App – 使用Swift在后台发送邮件[复制]
我有一个应用程序,我有一个简单的表单来收集数据,然后想要将包含该数据的电子邮件发送到特定的电子邮件地址.
理想情况下,我只是希望这对用户透明 – 他们只需按提交/发送,电子邮件将自动创建并在后台发送到该电子邮件地址,用户不知道.
我正在使用Objective-C获得这方面的教程,但我想使用Swift实现它.
是否有推荐的方式在后台发送电子邮件?
解决方法
ios – 使用swift读取短信
解决方法
即使您能够实现这一目标,Apple也不会允许它进入App Store.
ios – 在Swift 2.3中使用swift 3框架
解决方法
Swift 3 is the primary development language supported within Xcode 8
so there are a couple notes to consider if you chose to continue using
Swift 2.3. First,Swift 2.3 and Swift 3 are not binary compatible so
your app’s entire code base needs to pick one version of Swift.
Source and more informations on the Apple Swift Blog.
您必须在2.3和3.0之间进行选择.如果可以,我认为最好的想法是直接转向3.0(因为2.3只是一个“暂时”版本).
ios – 在Swift中使用Twilio发送短信
我的任务是使用Swift的Twilio API向一个号码发送短信.
我有一个Twilio.com帐户 – 那个正在运行.但是我如何以简单的方式在Swift代码中执行此操作.
Twilio确实提供了一个库 – 但这适用于C#而不是Swift(并且使用桥接头看起来太复杂了!)
这是C#示例,我需要一个简单的Swift示例.
// Download the twilio-csharp library from twilio.com/docs/csharp/install using System; using Twilio; class Example { static void Main(string[] args) { // Find your Account Sid and Auth Token at twilio.com/user/account string AccountSid = "AC5ef8732a3c49700934481addd5ce1659"; string AuthToken = "{{ auth_token }}"; var twilio = new TwilioRestClient(AccountSid,AuthToken); var message = twilio.SendMessage("+14158141829","+15558675309","Jenny please?! I love you <3",new string[] {"http://www.example.com/hearts.png"}); Console.WriteLine(message.Sid); } }
解决方法
要从Swift发送文本消息,您只需直接向Twilios REST API发出请求即可.也就是说,我不建议从iOS应用程序(或任何其他客户端应用程序)执行此操作,因为它要求您在应用程序中嵌入Twilio帐户凭据,这很危险.我建议从服务器端应用程序发送SMS.
如果您确实想从您的应用程序发送消息,我知道有几个Swift库可以简化HTTP请求:
> Alamofire – 来自AFNetworking的创建者Mattt Thompson – 在此示例中使用:https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html
> SwiftRequest – 来自Twilio的Ricky Robinett
要使用SwiftRequest发出请求,它看起来像这样:
var swiftRequest = SwiftRequest(); var data = [ "To" : "+15555555555","From" : "+15555556666","Body" : "Hello World" ]; swiftRequest.post("https://api.twilio.com/2010-04-01/Accounts/[YOUR_ACCOUNT_SID]/Messages",auth: ["username" : "[YOUR_ACCOUNT_SID]","password" : "YOUR_AUTH_TOKEN"] data: data,callback: {err,response,body in if err == nil { println("Success: \(response)") } else { println("Error: \(err)") } });
希望有所帮助.
今天关于使用Swift在iOS中发送短信和swift 消息发送的分享就到这里,希望大家有所收获,若想了解更多关于iOS App – 使用Swift在后台发送邮件[复制]、ios – 使用swift读取短信、ios – 在Swift 2.3中使用swift 3框架、ios – 在Swift中使用Twilio发送短信等相关知识,可以在本站进行查询。
本文标签: