Search code examples
http-redirectmobiletitaniumappceleratortitanium-alloy

Get back to mobile application after leaving it with Appcelerator Titanium


I was wondering if there were any ways to get back into my mobile application after leaving it.

For example, if i redirect the user into a webpage, how can i redirect the user into the application once the page's request has been finished? Moreover, i'd like to redirect the user to the app conserving some parameters given by the current webpage..

Second example, i guess facebook does sthg like that when a user uses the facebook signin button as he's directly redirected to the facebook web page and then goes back to the app with the facebook answer parameters.

I'm currently using Titanium with Alloy framework and Appcelerator Studio.

Hoping anyone of you has an answer,

Best regards,

Quentin


Solution

  • I'm not sure it that's possible since you technically leave your application. I used a WebView to render a HTML page which fires an event(with a certain payload) when a certain action is completed. The listener in my controller then catches this event and handles the payload from there.

    Create a new file in your app->assets folder and name it for example yoursite.html.

    Paste the following code in it:

    <html>
      <head>
        <title>Test</title>
        <script type="text/javascript">
          window.onload = function() 
          {
            Ti.App.fireEvent('your_event', { 'message': 'Hello World!' });            
          };
        </script>
      </head>
      <body>
      </body>
    </html>
    

    Then add the following code to your controller:

    var win = Ti.UI.createWindow();         
    
    Ti.App.addEventListener('your_event', function(e)
    {
        alert(e.message);
    });
    
    win.add(Ti.UI.createWebView({ url: '/yoursite.html' }));
    
    win.open();
    

    Tip: Global event listeners are not good for the performance of your app. If the user has the see the webview only once(single action like a login) then I suggest you using the following code:

    var win = Ti.UI.createWindow();         
    
    var setWebViewEventHandler = function(e)
    {
        this.removeEventListener('your_event', setWebViewEventHandler);
    
        alert(e.message);
    }   
    
    Ti.App.addEventListener('your_event', setWebViewEventHandler);
    
    win.add(Ti.UI.createWebView({ url: '/yoursite.html' }));
    
    win.open();