ios - How to call timer one time in application using swift? -
hi developing app using swift 2 in app updating current location every x minutes using nstimer able update location , works fine.in app(first view controller) when user logged in , moves second view controller in(second view controller) wrote function updating location using nstimer works fine when user moves third view controller , return second view controller again start updating location cant able stop timer twice invalidate timer 1 time other 1 keep on updating location after users logged out.so please me out solve problem.
my second view controller:
class secondviewcontroller:uiviewcontroller{
in view did load method:
var appdelegate:appdelegate = uiapplication.sharedapplication().delegate as! appdelegate appdelegate.timer = nstimer.scheduledtimerwithtimeinterval(10.0, target: self, selector: #selector(timetomoveon), userinfo: nil, repeats: true) //i declared var timer:nstimer! in appdelegate.swift , performed function using nstimer
my third view controller:
i created button action logout code is:
var appdelegate:appdelegate = (uiapplication.sharedapplication().delegate as? appdelegate)! appdelegate.timer.invalidate()
thanks in advance
hi thank support found answer question
i kept timer.invalidate() in 2 different methods according project
i suggest this:
this singleton wrap location.
sorry, swift3 have this:
class lm: nsobject, cllocationmanagerdelegate { //mark: public variable var lat = 0.0 var lon = 0.0 var cast = "location_cast" public static let : lm = { let instance = lm() return instance }() //mark: local variable fileprivate var locationmanager: cllocationmanager? //mark: init public override init() { super.init() locationmanager = cllocationmanager() locationmanager?.delegate = self } internal func start() { locationmanager?.startupdatinglocation() } internal func stop() { locationmanager?.startupdatinglocation() } func locationmanager(_ manager: cllocationmanager, didfailwitherror error: error) { //code print("\(error)") } func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { //code // print("\(locations)") if let loc = locations.last { //simple store retrieve later lat = loc.coordinate.latitude lon = loc.coordinate.longitude //cast here notification notificationcenter.default.post(name: nsnotification.name(rawvalue: cast), object: ["lat": lat, "lon": lon]) } } func locationmanager(_ manager: cllocationmanager, didchangeauthorization status: clauthorizationstatus) { switch status { case .notdetermined: manager.requestalwaysauthorization() break case .authorizedwheninuse: manager.startupdatinglocation() break case .authorizedalways: manager.startupdatinglocation() break case .restricted: // restricted e.g. parental controls. user can't enable location services break case .denied: // user denied app access location services, can grant access settings.app break } } }
to start , stop locationmanager use this:
@uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { //... var window: uiwindow? func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey : any]? = nil) -> bool { // override point customization after application launch. _ = lm.i return true } func applicationwillresignactive(_ application: uiapplication) { // sent when application move active inactive state. can occur types of temporary interruptions (such incoming phone call or sms message) or when user quits application , begins transition background state. lm.i.stop() } func applicationdidbecomeactive(_ application: uiapplication) { // restart tasks paused (or not yet started) while application inactive. if application in background, optionally refresh user interface. lm.i.start() } }
you can add inside appdelegate
custom timer casting variables each 1 second
let casttoviewcontroller = "casttoviewcontroller" notificationcenter.default.post(name: nsnotification.name(rawvalue: casttoviewcontroller), object: ["lat": lm.i.lat, "lon": lm.i.lon])
or simple
let casttoviewcontroller_ping = "casttoviewcontroller_ping" notificationcenter.default.post(name: nsnotification.name(rawvalue: casttoviewcontroller), object: nil)
to catch new data casted values use this:
https://stackoverflow.com/a/38615219/1979882
if not manage .start()
, .stop()
ios kick background timer application each in several minutes if press 'home' button.
Comments
Post a Comment