您正在我的应用程序中使用swift2开发应用程序我希望每当用户成功登录时每5分钟跟踪一次位置我现在可以成功跟踪用户的位置我想跟踪应用程序处于后台的时候必须使用app委托,但我只在视图控制器中编写所有函数和方法,所以我如何调用视图控制器方法和函数到app委托。
viewController 中的代码:
class ThirdViewController: UIViewController{
In view did load:
{
////// i created timer scheduled with time internal /////
}
////// Here am fetched current locations and performed some function for timer to execute //////
}
在我的 appDelegate :
中func applicationDidEnterBackground(application: UIApplication) {
if UIApplication.sharedApplication().applicationState != .Active{
//here i have to call that timer function execute how to do that????
}
答案 0 :(得分:0)
您可以在应用转到后台时触发通知
func applicationDidEnterBackground(application: UIApplication) {
if UIApplication.sharedApplication().applicationState != .Active{
NSNotificationCenter.defaultCenter().postNotificationName("Notificationname", object: nil)
}
}
控制器视图中的确实加载了dd观察者通知
// add observer for notification.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.notificationReceived), name: "Notificationname", object: nil)
收到通知的方法处理程序
func notificationReceived() {
// do what you want to do here.
}
答案 1 :(得分:0)
您希望在其中创建一个包含您的函数的类,然后从您的app delegate中调用它,您可以这样做:
MyClass.swift
class MyClassName {
Add your function in here
}
然后你从appDelegate.swift
这样称呼它:
let myClassVar: MyClassName?
然后调用这样的函数 - > myClassVar.functionName
答案 2 :(得分:0)
像这样,
func applicationDidEnterBackground(application: UIApplication) {
if UIApplication.sharedApplication().applicationState != .Active{
let thirdViewController = ThirdViewController()
thirdViewController.functionYouWantToCall()
}
谢谢:)