如何使用 Swift 从 ViewController 读取 AppDelegate 中的变量值

How to read a variable value in the AppDelegate from a ViewController using Swift

我想从 ViewController 中读取 AppDelegate 中包含的变量值。该变量包含用于启用 iOS 推送通知的 Device Token,我想在 UILabel.

中显示它

到目前为止,这是我的代码:

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    internal var deviceTokenToPass: String?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let pushNotificationsTypes: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let pushNotificationsSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: pushNotificationsTypes, categories: nil)

        application.registerUserNotificationSettings(pushNotificationsSettings)
        application.registerForRemoteNotifications()

        return true
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let chararacterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
        self.deviceTokenToPass = (deviceToken.description as NSString).stringByTrimmingCharactersInSet(chararacterSet).stringByReplacingOccurrencesOfString(" ", withString: "", options: nil, range: nil) as String
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet var deviceTokenLabel : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        var deviceToken = appDelegate.deviceTokenToPass

        println(deviceToken) /* nil */

        if deviceToken != nil {
            self.deviceTokenLabel.text = deviceToken
        } else {
            self.deviceTokenLabel.numberOfLines = 4
            self.deviceTokenLabel.text = "Cannot read your device token.\nYou must be using an iOS Simulator or you didn't allowed the application for push notifications"
        }
    }
}

问题是,如果我将 println(deviceToken) 代码放在 AppDelegate.swift 中,设备令牌会正确显示在控制台中,如果我将它放在 ViewController.swift 中,它的值将.

如果您确保要访问的 属性 实际上是可读的,那么您可以使用以下内容:

 let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
 let anAttribute = appDelegate.theAttribute

didRegisterForRemoteNotificationsWithDeviceToken 通知是异步的(在设备成功注册后调用),所以我认为正在发生的是通知发生在 视图控制器之后显示。

要检查这一点,请从应用委托和视图控制器中打印它,看看哪个先发生。

至于如何解决这个问题,我会实施 didFailToRegisterForRemoteNotificationsWithError,以便调用两者之一,然后我会在两种情况下(成功和失败)发送通知(通过 NSNotificationCenter) ),并将结果存储在 属性.

在视图控制器中,我会订阅自定义通知,并在收到通知之前显示进度视图。但在 viewDidLoad 中,我还会检查在应用程序委托中设置的结果 属性,以防万一注册太快并且已经完成。

在应用委托中

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    weak var label: UILabel? = nil

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let chararacterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
        let deviceTokenToPass = (deviceToken.description as NSString).stringByTrimmingCharactersInSet(chararacterSet).stringByReplacingOccurrencesOfString(" ", withString: "", options: nil, range: nil) as String
        if let l = label {
            l.text = deviceTokenToPass
        }
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let pushNotificationsTypes: UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
        let pushNotificationsSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: pushNotificationsTypes, categories: nil)

        application.registerUserNotificationSettings(pushNotificationsSettings)
        application.registerForRemoteNotifications()

        return true
    }
}

在ViewController

class ViewController: UIViewController {

    @IBOutlet weak var deviceTokenLabel : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
            appDelegate.label = deviceTokenLabel
        }

    }
}