kivy-ios memo kivy v1.8.0 URL linkage (MAC)

Deprecated this article (Reference to New article)

I investigated how to do URL linkage on iOS. Since there was a part of cooperation with mail APP in kivy-ios, it can be implemented by referring to it.

1. Call

Import / preprocessing


try:
    from urllib.parse import quote
except ImportError:
    from urllib import quote
from pyobjus import autoclass, objc_str
from pyobjus.dylib_manager import load_framework

load_framework('/System/Library/Frameworks/UIKit.framework')

NSURL = autoclass('NSURL')
NSString = autoclass('NSString')
UIApplication = autoclass('UIApplication')

Call (email)


uri = "mailto:[email protected]?subject=text&body=body"
nsurl = NSURL.alloc().initWithString_(objc_str(uri))
UIApplication.sharedApplication().openURL_(nsurl)

2. Called

Define urlscheme in XCode by referring to Reference site. Now it will be called by other applications, but most applications will want to take arguments. However, kivy-ios does not seem to accept arguments by default, so it needs to be received by some mechanism. kivy now receives iOS openUrl events when generating drop file events. However, this cannot be received because the event has not been transmitted to the application. It is easier to divert this than to add a new event, so divert it. The fix location is under tmp in the kivy-ios directory.

-Add a drop file interface to the SDL event definition for Cython in kivy.

kivy/kivy/core/window/sdl.pyx


        .
        .
        .
    ctypedef struct SDL_TouchFingerEvent:
        long touchId
        long fingerId
        unsigned char state
        unsigned short x
        unsigned short y
        short dx
        short dy
        unsigned short pressure

    # add start
    ctypedef struct SDL_DropEvent:
        unsigned int timestamp
        char *file
    # add end

    ctypedef struct SDL_Event:
        unsigned int type
        SDL_MouseMotionEvent motion
        SDL_MouseButtonEvent button
        SDL_WindowEvent window
        SDL_KeyboardEvent key
        SDL_TextInputEvent text
        SDL_TouchFingerEvent tfinger
    # add start
        SDL_DropEvent drop
    # add end 

    # Events
    int SDL_QUIT
    int SDL_WINDOWEVENT
    int SDL_SYSWMEVENT
    int SDL_KEYDOWN
    int SDL_KEYUP
    int SDL_MOUSEMOTION
    int SDL_MOUSEBUTTONDOWN
    int SDL_MOUSEBUTTONUP
    int SDL_TEXTINPUT
    int SDL_FINGERDOWN
    int SDL_FINGERUP
    int SDL_FINGERMOTION
    # add start
    int SDL_DROPFILE
    # add end                
        .
        .
        .
    elif event.type == SDL_TEXTINPUT:
        s = PyUnicode_FromString(<char *>event.text.text)
        return ('textinput', s)
    # add start
    elif event.type == SDL_DROPFILE:
        s = PyUnicode_FromString(<char *>event.drop.file)
        action = ('dropfile', s)
        return action
    # add end
    else:
        if __debug__:
            print('receive unknown sdl event', event.type)
        pass
        .
        .
        .

-Allow the above event to be caught in the wrapper part of SDL.

kivy/kivy/core/window/window_sdl.py


        .
        .
        .
    def _mainloop(self):
        EventLoop.idle()

        while True:
            event = sdl.poll()
        .
        .
        .
            elif action == 'textinput':
                key = args[0][0]
                # XXX on IOS, keydown/up don't send unicode anymore.
                # With latest sdl, the text is sent over textinput
                # Right now, redo keydown/up, but we need to seperate both call
                # too. (and adapt on_key_* API.)
                self.dispatch('on_key_down', key, None, args[0],
                              self.modifiers)
                self.dispatch('on_keyboard', None, None, args[0],
                              self.modifiers)
                self.dispatch('on_key_up', key, None, args[0],
                              self.modifiers)
            # add start
            elif action == 'dropfile':
                self.dispatch('on_dropfile', args[0])
            # add end

        #    # video resize
        #    elif event.type == pygame.VIDEORESIZE:
        .
        .
        .
       # XXX FIXME wait for sdl resume
        while True:
            event = sdl.poll()
            if event is False:
                continue
            if event is None:
                continue

            action, args = event[0], event[1:]
            if action == 'quit':
                EventLoop.quit = True
                self.close()
                break
            elif action == 'windowrestored':
                break
            # add start
            elif action == 'dropfile':
                app.dispatch('on_dropfile', args[0])
            # add end
        .
        .
        .

-Add an event interface for the drop file so that it can be received by the main function.

kivy/kivy/app.py


        .
        .
        .
    # Return the current running App instance
    _running_app = None

    # modify start ( add on_dropfile )
    __events__ = ('on_start', 'on_stop', 'on_pause', 'on_resume', 'on_dropfile')
    # modify end

    def __init__(self, **kwargs):
        App._running_app = self
        self._app_directory = None
        self._app_name = None
        .
        .
        .
    def on_resume(self):
        '''Event handler called when your application is resuming from
        the Pause mode.

        .. versionadded:: 1.1.0

        .. warning::

            When resuming, the OpenGL Context might have been damaged / freed.
            This is where you can reconstruct some of your OpenGL state
            e.g. FBO content.
        '''
        pass

    # add start
    def on_dropfile(self, filename):
        '''Event called when a file is dropped on the application.

        .. warning::

            This event is currently used only on MacOSX with a patched version
            of pygame, but is left in place for further evolution (ios,
            android etc.)

        .. versionadded:: 1.2.0
        '''
        pass
    # add end

    @staticmethod
    def get_running_app():
        .
        .
        .

-After executing Tools / Build-all.sh, if you define def on_dropfile (self, filename): in your main function, you can get it as an event. As a caveat, since it is URL-encoded, it is necessary to perform URL decoding after acquisition (URL decoding reference).

main.py


        .
        .
        .
    def on_dropfile(self, filename):     <----Here, it becomes possible to acquire the argument called by URL linkage.
        print self.mydecode(filename)

    def mydecode(self, value):
        if isinstance(value, unicode):
            value = value.encode('raw_unicode_escape')
        value = urllib.unquote(value)
        return value

if __name__ == '__main__':
    TestApp().run()

Recommended Posts

kivy-ios memo kivy v1.8.0 URL linkage (MAC)
kivy-ios memo kivy v1.9.0-dev URL linkage (MAC)
kivy-ios memo kivy v1.8.0 (MAC)
kivy-ios memo kivy v1.8.0 for new toolchain (MAC)
[Tentative] How to convert a character string to Shift_jis with kivy-ios Memo kivy v1.8.0
Kivy-Android Memo (MAC)
[My memo] python -v / python -V