【发布时间】:2015-07-02 08:47:48
【问题描述】:
我在 AppDelegate.swift 中有这个委托,一旦另一个应用使用 url 方案打开我的应用,它就会触发。
AppDelegate.swift
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return true
}
当另一个应用程序使用 url 方案打开我的应用程序时它会触发,但是当这个函数触发时,我想通知某个 ViewController。我想我可以使用自定义委托来做到这一点,并让 AppDelegate 通知曾经实现我的委托的人有人打开了应用程序。
MyDelegate.swift
protocol MyDelegate {
func opened(hasBeenOpened: Bool!)
}
然后我的 ViewController 实现了这个 Delegate
LoginViewController.swift
import UIKit
/* Obviously this class has more code and other functions,
but for the illustration of the problem, I removed all the other unrelated things.*/
class LoginViewController: UIViewController, MyDelegate {
func opened(hasBeenOpened: Bool!) {
print(hasBeenOpened)
}
}
到目前为止一切顺利,但让我们回到 AppDelegate.swift 中的 openURL() 函数并尝试调用 MyDelegate.opened()。这是我完全迷失的地方。
AppDelegate.swift
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
print("openURL delegate running")
if var delegate = MyDelegate?() {
delegate.opened(true)
}
return true
}
控制台打印“openUrl delegate running”,所以它正在运行,但我的委托变量变为nil。我缺少一些初始化吗?
我似乎无法弄清楚如何从 AppDelegate 调用我自己的自定义委托。有没有另一种方法来通知 ViewControllers 这已经发生了?或者这总体上是一个坏主意,还有另一种被认为更好的方法吗?
提前谢谢大家,非常感谢您的帮助。
【问题讨论】:
-
我不确定代表是实现这一目标的适当方式。问题是您需要在 AppDelegate 中有一个委托变量,然后在 LoginViewController 中设置它。但是在您的 openURL 方法中,您可能还没有实例化的 LoginViewController。你只想显示 LoginViewController 吗?
-
我基本上想在 LoginViewController 上调用 openURL 方法的函数。但是从 AppDelegate 调用 ViewController 上的函数似乎很奇怪。我想我宁愿注册一个委托,这样曾经实现过委托的人就可以监听事件。
-
不只是delegate,你的VC对象也会是
nil,请问你想做什么,我是说通知VC做什么? -
@VinayJain 我正在使用第三方银行应用程序对我的用户进行身份验证,当身份验证完成后,银行应用程序再次使用 url 方案打开我的应用程序。发生这种情况时,AppDelegate 中的 openURL 正在运行。我只是想通知我的 LoginViewController 用户已尝试使用银行应用程序进行身份验证,因此我可以从银行 Web 服务收集登录信息并查看它是否成功。