iOS Timer in the background

后端 未结 4 1045
夕颜
夕颜 2020-12-11 17:56

I want in my iOS application to start a timer when the app is running in the background and when the app is closed. The timer must check every 30 minutes of there a new noti

4条回答
  •  离开以前
    2020-12-11 18:45

    Write this both methos in your Appdelegate.m file
    
    //For Objective c
        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
              [self TimerMethod];
        }
        -(void)TimerMethod
        {
            //Every 30 minute call the functions
            _timer=[NSTimer scheduledTimerWithTimeInterval:1800.0f target:self selector:@selector(updateMethod:) userInfo:nil repeats:YES];
        }
    
        - (void)updateMethod:(NSTimer *)theTimer
        {
            NSLog(@"Timer set now");
        }
    
    //For Swift
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool 
    {
         self.TimerMethod()
    }
    
    func TimerMethod()
    {
          var timer = NSTimer.scheduledTimerWithTimeInterval(1800, target: self, selector: "updateMethod", userInfo: nil, repeats: true)
    }
    
    func updateMethod()
    {
        print("set timer now")
    }
    

提交回复
热议问题