Reputation: 23
I have the variable I created in appDelegate. how do I use this variable in ViewController. I've tried a few methods, but. I always got an error. my code is as follows. I am swifte new Please help?
AppDelegate.swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// refreshedToken is variable. I use it in viewcontroller.
if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
}
ViewController.swift
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let purl = URL(string: "")!
var request = URLRequest(url: purl)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "GET"
let postString = "ajax=token&"+"token="+refreshedToken// Use of unresolved identifier 'refreshedToken'
print("postString: \(postString)")
request.httpBody = postString.data(using: .utf8)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
how do I use the refreshedToken in ViewController ?
Upvotes: 2
Views: 2688
Reputation: 927
You can handle this with Singleton Model Object which will hold your data and has global access of data from anywhere of your project.
First create a new Model File that will have code written below:
class SingletonDataModel: NSObject {
var token:String?
static let sharedInstance = SingletonDataModel()
private override init() {
super.init()
}
}
Secondly save data on this upper model from ApppDelegate
class like below:
var refreshedToken : String?
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// refreshedToken is variable. I use it in viewcontroller.
if let token = InstanceID.instanceID().token() {
refreshedToken = token
SingletonDataModel.sharedInstance.token = token
print("InstanceID token: \(refreshedToken)")
}
}
Now your data model singleton is initialized and token value is stored on that object
Finally access that value from your viewDidLoad
method of the ViewController
Class
filelike below:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let token = SingletonDataModel.sharedInstance.token
//Do your work
}
if token
is nil
, this mean your AppDelegate
-> didRegisterForRemoteNotificationsWithDeviceToken
method is not called before the initialization of your ViewController
. You must ensure that your didRegisterForRemoteNotificationsWithDeviceToken
is called before the initialization of your ViewController
. Otherwise you have to handle this in different way
Upvotes: 0
Reputation: 52088
You need to understand in what scope you declare a variable and how it is visible outside that scope. Now your variable is only visible inside that func, to make it visible to your view controller it needs to be a class property in AppDelegate.
So define a property like (somewhat simplified code here)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var refreshedToken = InstanceID.instanceID().token()
...
}
And then in you can access it in your view controller
let token = appDelegate.refreshedToken
Upvotes: 1
Reputation: 732
You can save the refresh token in UserDefaults
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// refreshedToken is variable. I use it in viewcontroller.
if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
UserDefaults.standard.set(refreshedToken, forKey: "preferenceName")
}
}
and Later can retrieve the value in ViewController using
let postString = "ajax=token&"+"token="+ UserDefaults.standard.string(forKey: "Key")
Upvotes: 0
Reputation: 5823
Just create your refreshedToken globally in appdelegate class and update code as follows:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var refreshedToken : String?
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// refreshedToken is variable. I use it in viewcontroller.
if let token = InstanceID.instanceID().token() {
self.refreshedToken = token
print("InstanceID token: \(refreshedToken)")
}
}
}
From above code, refreshedToken available if didRegisterForRemoteNotificationsWithDeviceToken
method called otherwise it will be nil.
If you want it global available in application without dependent on any method write following code just after AppDelegate class implementation.
let refreshedToken = InstanceID.instanceID().token()
Upvotes: 0