02 9 / 2017

Given the minimalist node philosophy this doesn’t come with the package, and given my rails background it’s hard for me to imagine a world without migrations. In case you feel the same way, below is a quick start guide to get you up and running with migrations in a node app within two minutes:

Comments

10 6 / 2014

Soon after solving the First world problems, I am back to cross the bridge, as promised. This time around I am going to solve the same problem for the developing world aka Android. Yes you guessed it right - how to open a custom URL in the app if the app is installed, and open an alternate URL if the app is not available on the device, on an android browser. All for free ;)

As we all know, third (+ second) world problems are much harder to solve, in comparison to first world problems. This applies here as well =P. Given the fragmentation in android devices, it’s almost impossible to test it on all devices and browsers. So I would say that the solution below works for most of the  android browsers, and will list down known issues towards the end.

Below is the working javascript code snippet. To explain it briefly, there are 3 approaches that are at work here. One is the iFrame approach which works in older browsers (older android versions and default Android Browser), where the browser shows up a different page when a URL is not found. The other uses the Page visibility API, which some modern browsers have implemented. I use a combination of both, and some inception (timeout within timeout) to make it work on most android browsers. The third approach is based on android intents (which only works on Chrome).

It looks like chrome specifically introduced a page to show up for such scenarios, so that you couldn’t use the iframe approach (for version 25 and later). They did all this to force you to use a feature they created - android intents. In essence they wrote more code, so that they can force you to use the code they had already written in the first place. F#@k you chrome! F#@k you google! You made me work harder =P And there are specific scenarios which intents don’t handle. What if I don’t want the user to visit play store and I have the same content in my apps, as well as on my website. For users with app installed, I take them to the app, whereas others are taken to a web URL. Think Inc, Forbes, and all the magazine apps. I personally have a lot of respect for google, and this is one of the shittiest moves I have seen from you. I hope the project manager who forced this onto the world sees this, and expresses his ‘intent’ about 'Android Intents’ ;) You owning a popular browser, doesn’t mean you can force others to do whatever you want (or may be you can).

Please note that you will need to create an intent such that it can handle all your custom URL schemes. I am not an android dev myself, but I checked with my colleagues, and it is very much possible.

One known issue is that the visibility API seemingly doesn’t work on Firefox for android (ironically though looks like Mozilla proposed it in the first place), and therefore the alternate URL is opened even when the custom URL works (app installed). Not that bad ehhh…!

Looks like there is a reason why developers prefer iOS over android. As end users you may not feel the pinch, but we definitely do ;)

image

If you are elated by the solution, send me money. If you are pissed off by the analogies, or my stupid sense of humor, send me more money =P

PS: Initially I wanted to do this without intents, due to the reasons described above. I spent hours, if not days trying to work around the chrome stupidity, but to no avail. Eventually I found that link where it was mentioned that chrome doesn’t want you to do that. Looks like for now you will have to live with it. If you have suggestions to improve upon this, or better approaches to work around this problem, please drop a note in the comments section.

Comments

26 1 / 2014

The iOS custom URL scheme is broken. Have a link to a custom url for your app in the browser, and it works like a charm, if you have the app installed. If not, then you are doomed! Safari shows a super helpful alert that says - ‘Safari cannot open the page because the address is invalid.’ The other browsers are even more helpful, and they do nothing! Absolutely nothing. They don’t have the decency to redirect you to an alternate URL (or provide one), or prompt you to download the app.

May be the solution is somewhere out there, but I couldn’t find it. Or may be google has been lazy lately, and their focus shifted to google+ =P

There are start-ups built around solving this problem, and I am going to give this away for free. Remember, I wanted to change the world ;)

To start with, this isn’t rocket science, but it took me a lot of trial and error, and fine tuning to get this working on all the iOS browsers. In safari, it still shows that alert, but it goes away in a couple of seconds when the alternate URL is loaded. Here is how I went about solving this:

If it ended here, then life would have been so much fun. But it’s like saying the world would have been so much better if there was no internet explorer. Simply put, it isn’t!

The bad news is that the above code works only for safari, because they designed iOS and came up with 'pageshow’ and 'pagehide’ events, so they have them working well. The other browsers don’t care or give a shit. They don’t fire those events on tab change, or when the browser switches to background/foreground: http://stackoverflow.com/questions/21311478/pagehide-and-pageshow-events-dont-work-as-expected-on-ios-chrome

Sad, but true! Not a problem. Below is another javascript snippet that works well on all the browsers (I tested safari, chrome and mercury, but logically it should work on others as well).

I increased the timeout value to 2000ms because chrome sometimes takes a bit too long to launch the app (may be tries to do some post processing magic), and triggers the code in timeout block before the timers are cleared. Tweak it as per your need, or you can even have timeouts per browser. Also, don’t forget to put the 'source_url’ class on your link tag.

That’s it. This should work in android as well (may be there are other events available in android), but I’ll cross that bridge when I am there. You have the tools now.

Let me know if you need an explanation, see some failure scenarios, or if I can make this better, or you just want to say thank you. If this really changed your world, as mentioned in the misleading title, then send me money instead =P

Comments

16 5 / 2012

Since I already have wasted a lot of time, figuring it out, therefore it is better that I write it down for future reference, and may be someone else finds it useful, and can save a few hours!

As the title suggests, Safari and Chrome do not fire focus/blur events when the user uses the mouse to access checkboxes or radio buttons. It will work fine when you use the <Tab> key, but mouse click wouldn’t work as expected. It works perfectly in firefox. Here is an example:

http://jsfiddle.net/54y69/147/

Try it out on different browsers to experience the difference.

The fix is to associate the function with the click event. In my case we highlight the label when a form field is selected. So, I fixed it for checkboxes by binding to both click and focus events. Below is the pseudo code:

$('<selector>').bind('focus click', function(){

// Custom code

});

On a lighter note, how about banning all browsers except Firefox… ;)

In my opinion that is the easiest and quickest way to World Peace!

UPDATE: Mac version of firefox behaves similar to chrome. Thanks to Manoj for pointing that out. Filed a bug with Mozilla here: https://bugzilla.mozilla.org/show_bug.cgi?id=756028

Comments
blog comments powered by Disqus