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

时间:2015-02-12 14:20:05

标签: swift var

我想从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它中值将 nil

3 个答案:

答案 0 :(得分:1)

如果您确定您要访问的媒体资源实际上是可读的,那么您可以使用以下内容:

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

答案 1 :(得分:0)

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

要检查这一点,请从app delegate和视图控制器中打印出来,然后查看最先发生的事情。

至于如何解决这个问题,我会实现didFailToRegisterForRemoteNotificationsWithError,以便调用其中一个,然后我会在两种情况下(成功和失败)发送通知(通过NSNotificationCenter),并将结果存储在房产中。

在视图控制器中,我会订阅自定义通知,并显示进度视图,直到收到通知。但是在viewDidLoad中,我还会检查应用代理中设置的结果属性,以防注册过快而且已经完成。

答案 2 :(得分:0)

在app appate

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
        }

    }
}