Creating Link sharing feature in Sketchware

 Requirements:

  • A webpage
  • APK Editor plus or alternatives for editing Android Manifest.

Welcome to Sketchub Blog, in this blog I will try to explain how to create post/profile link sharing option just like Sketchub. (There is no special need of Firebase Dynamics for this).

For example you have a webpage "https://project.sketchub.in". Then in index file, you will need to add a button with id "btn_open_project" which will be used for opening the app:

 <h4 id="btn_open_project">Open Project</h4>
And add javascript code in but end of body tag:
 <script>
var url_string = window.location.href;
var url = new URL(url_string);
var a = document.getElementById('link_main');
a.href = "project.sketchub://" + url;
</script>
Here, link_main is the id of button or text which will be used to open the project through the page. We will explain, why we added "project.sketchub://" before the URL, in end of this post.

Now lets move to the Sketchware project.
In MainActivity, preferably in OnCreate , add this code for obtaining the link when app is opened via link:
 try{
   Intent intent = getIntent();
   Uri data = intent.getData();
   shared_data = data.toString();
} catch (Exception e) {
   // The app is not opened via link
}
If your app is opened via link, it will get the link in the string shared_data. The value of shared_data will be "project.sketchub://project.sketchub.in/?id=123" (why project.sketchub:// , we will explain later).
Now, in the link, "id" is URL parameter, with value of "123". And we can add multiple URL parameters like: "project.sketchub.in/?id=123&userID=456&other=789" (here userID and other is another parameters with value 456 & 789 respectively).

In Sketchware, we will need to get value of "id" from the string "shared_data" by using this code:

 Uri uri = Uri.parse(shared_data);
String server = uri.getAuthority();
String path = uri.getPath();
String protocol = uri.getScheme();
Set<String> args = uri.getQueryParameterNames();

shared_key = uri.getQueryParameter("id");
Here, "shared_key" is the value of URL parameter "id" i.e. "123".

Now, you will need to implement your own method to find the post with id=123 ;)

Now run the project, and edit APK's android manifest using APK Editor Plus or alternative apps.
Add this code after any </activity> in the existing code.

 <activity android:label="@string/app_name" android:name=".MainActivity">
   <intent-filter>
       <action android:name="android.intent.action.VIEW" />
       <action android:name="com.yourapppackage.LAUNCH" />
       <category android:name="android.intent.category.DEFAULT" />
       <category android:name="android.intent.category.BROWSABLE" />
       <data android:scheme="project.sketchub" />
   </intent-filter>
</activity>

Here android:scheme="project.sketchub" means that it will only pick those links which starts from "project.sketchub://...". (This way also forces user to move to webpage first, you may edit it if you don't want so, but again, it is useful to send user to webpage.)

Build the APK and test it, it will work ;)

6 comments:

  1. Great. It's helpful... Very nice😍

    ReplyDelete
  2. Can you make video on this and share

    ReplyDelete
  3. Canot copy code when i select code its selecting whole web pls fix add button to copy code

    ReplyDelete
  4. How to create like dislike button in Sketchware studio like youtube

    ReplyDelete
  5. Pls give me video tutorial!

    ReplyDelete

Note: Only a member of this blog may post a comment.

Powered by Blogger.