swift - 如何以编程方式在 Watch 上撰写消息?

标签 swift watchkit

如何打开消息应用到撰写屏幕,消息正文预加载了特定文本?

最佳答案

Benjy's answer is almost correct ,但有一个问题。

由于 urlSafeBody 未展开,字符串插值产生

sms:&body=Optional("Hello%20World!")

这导致 NSURL 初始化返回 nil,因为 URL 字符串格式错误。

这是一个有条件地解包可选值的工作示例。这消除了与 nil 可选值被强制展开相关的任何崩溃的可能性。

let messageBody = "Hello World!"
let urlSafeBody = messageBody.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
if let urlSafeBody = urlSafeBody, url = NSURL(string: "sms:&body=\(urlSafeBody)") {
    WKExtension.sharedExtension().openSystemURL(url)
}

关于swift - 如何以编程方式在 Watch 上撰写消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361531/

相关文章:

ios - UINavigationBar 功能中的 UISwitch

swift - Firestore : How to set security on object types with users as field names

swift - 设置 WatchKit 计时器的倒计时间隔

ios - containerURLForSecurityApplicationGroupIdentifier : gives different results on iPhone and Watch simulator

ios - 我如何从 watch 应用程序检查用户是否登录到电话应用程序

ios - 应用程序 :handleIntent:completionHandler: on WatchOS 的替代方案

ios - UICollectionView 中具有序列的小单元格大小和大单元格大小

ios - AVFoundation Swift 错误

ios - Watchkit 应用程序在安装期间显示错误 - WatchKit 应用程序具有无效的 stub 可执行文件

ios - 将图像添加到 UICollectionView 中的 AVPlayerViewController