lundi 19 juin 2017

How to achieve deep linking in Ruby on Rails

How can we achieve deep linking of ios applications on Rails server.

I am not sure how a deep linking works, But in my understanding it should work like this:

  1. There would be one URI which would be shared among users eg: http://ift.tt/2sJSNuQ
  2. When request would come from this URL rails will open this application in the device if the application is installed in device.
  3. When request would come from this URL rails will open App Store in browser if the application is not installed in device.

I came across similar code in PHP How can I achieve that in Rails:

   public function getShare(Request $request) {
    try {

        $app_name = $request->input('app_name', ‘URLSCHEME, Dev will give’);
        $shared_on = $request->input('shared_on', 'facebook');
        $shared_from = $request->input('shared_from', 'web');
        $shared_id = $request->input('shared_id', '0');
        $shared_by = $request->input('shared_by', '0');
        $app_store_id = '';
        $message = 'ok';


        $rules = array(
            // marketplace
            'app_name' => 'sometimes|string',
            // facebook, twitter
            'shared_on' => 'sometimes|string',
            // web , ios 
            'shared_from' => 'sometimes|string',
            //product_id' 
            'shared_id' => 'required|integer',
            // user_id
            'shared_by' => 'sometimes|integer',
        );
        $validation = Validator::make($request->all(), $rules);


        if ($validation->fails()) {
            $message = trans('messages.invalid_request');
        }
    } catch (Exception $e) {
        $message = trans('messages.exception_msg');
    }

    $deeplink = $app_name . "://applinks"
            . "?shared_on=" . $shared_on
            . "&shared_from=" . $shared_from
            . "&shared_id=" . $shared_id
            . "&shared_by=" . $shared_by
            . "&message=" . $message;

    $return_url = url('student/dashboard')
            . "?shared_on=" . $shared_on
            . "&shared_from=" . $shared_from
            . "&shared_id=" . $shared_id
            . "&shared_by=" . $shared_by
            . "&message=" . $message;



   $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
   if(stripos($ua,'android') !== false) { 

        $return_url = 'http://ift.tt/2tjG9zS';
    }
    else if(stripos($ua,'iPhone') !== false || stripos($ua,'iPad') !== false) { 
        $return_url = 'http://ift.tt/2sJYh91';
    }



    $html = '<html>
                <head>
                    <meta property="og:title" content="' . $app_name . '" />
                    <meta property="og:type" content="website" />
                    <meta property="al:android:package" content="' . $package . '" />
                    <meta property="al:android:url" content="' . $deeplink . '" />
                    <meta property="al:android:app_name" content="' . $app_name . '" />
                    <meta property="al:ios:url" content="' . $deeplink . '" />
                    <meta property="al:ios:app_store_id" content="' . $app_store_id . '" />
                    <meta property="al:ios:app_name" content="' . $app_name . '" />
                    <meta property="al:web:url" content="' . $return_url . '" />
                    <!-- Other headers -->';

    if(!$this->isMobileDevice())
    {
        $deeplink = $return_url;
    }

    $html = $html . "<script>
                    window.onload = function() {
                    <!-- Deep link URL for existing users with app already installed on their device -->
                        window.location = '" . $deeplink . "';
                    <!-- URL  for new users to download the app -->
                        setTimeout(\"window.location = '" . $return_url . "';\", 3000);
                    }
                </script>";

    $html = $html . '</head>
                <!-- Other HTML content -->';
    $html = $html . "Try opening <a id='deeplink' href='$deeplink'>app</a> ...";
    $html = $html . '</html>';
    echo $html;
    exit();
}

Aucun commentaire:

Enregistrer un commentaire