我是 swift 的新手,我创建了一个今日小部件,并向其添加了一个按钮。在该按钮的操作中,我想在 safari 中启动一个 url,但“sharedApplication()”不起作用,我在普通 View Controller 中使用此代码:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().openURL(NSURL(string: "facetime:mahdi@gmail.com")!)
// Do any additional setup after loading the view, typically from a nib.
}
但它在 Today App Extension 中不起作用。
最佳答案
您不能在应用扩展中使用 sharedApplication
。
如今,小部件可以使用 NSExtensionContext
上的 openUrl:completionHandler:
方法打开 url,因此您可以:
extensionContext?.openURL(NSURL(string: "facetime:mahdi@gmail.com")!, completionHandler: nil)
不过,您应该在按钮的点击处理程序中执行此操作,而不是在 viewDidLoad
中执行此操作。
但请注意,苹果文档指出:
IMPORTANT
Apple allows any Today widget to use the openURL:completionHandler: method to open the widget’s own containing app.
If you employ this method to open other apps from your Today widget, your App Store submission might entail additional review to ensure compliance with the intent of Today widgets.
To learn more, read App Store Review Guidelines and iOS Human Interface Guidelines, linked to from Apple’s App Review Support page
关于ios - sharedApplication() 在 Today Extension 中无法快速工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187584/