omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Testing if the app is running in the background

    Pythonista
    ios background threading
    2
    4
    3865
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Buzzerb
      Buzzerb last edited by

      I am trying to write a program which if at any point, is put into the iOS background, resets back to the beginning. I have only a basic level of python experience, but I suspect that the answer involves threading, and creating a new thread that constantly checks if it is in the background. If however there is a way a command can be run whenever the program is put in the background, I would be happy to know how

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        
        from objc_util import  *
        from ctypes import *
        import dialogs
        
        def willResignActive_(_self,_sel,_notification):
           print('entering background')
           #do stuff here
        
        	
        MyBGDetector=create_objc_class('MyBGDetector',methods=[willResignActive_])
        d=MyBGDetector.alloc().init()
        
        UIApplicationWillResignActiveNotification=ObjCInstance(c_void_p.in_dll(c,'UIApplicationWillResignActiveNotification'))
        NSNotificationCenter=ObjCClass('NSNotificationCenter')
        NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(d, sel('willResignActive:'), UIApplicationWillResignActiveNotification, None)
        
        
        Buzzerb 1 Reply Last reply Reply Quote 0
        • Buzzerb
          Buzzerb @JonB last edited by

          @JonB Thank you so much. If anyone, or Jon, would be able to explain exactly how this works, it would be greatly appreciated.

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            IOS applications can register to recieve "notifications" of certain system events.Since pythonista has a bridge to objectivec, it is possible to tap into those events, and register a python function to be called when the event occurs.

            This code probably needs to handle cleared globals, and multiple runs ("deregister" the object), etc but would work for the simple use case presented.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors