Thursday, 26 May 2016

Get boolean value from JSON, in Obj C

myObject.MyBoolValue = [[myDictionary objectForKey:@"TheNameOfTheJsonValue"]boolValue];

Friday, 13 May 2016

Android Xamarin ViewPager cacheing - unable to renew contents

I just 'wasted' a couple of hours trying to resolve a problem with a ViewPager. I have a fragment that hosts a ViewPager of 7-10 fragments, all of which needed to be swapped out if the user selects a different value/setting. This was working fine in some cases, but when navigating away from the parent fragment and then returning, I seemed unable to get the ViewPager to update properly, showing either old or blank content.

I spent too long stepping through my code trying to work out if I'd missed something, but came up with nothing, and figured that there must be some kind of cacheing going on.

After some reading I learnt that I should be using FragmentStatePagerAdapter and not FragmentPagerAdapter. The difference is explained here.

This, combined with the below override in my adapter, finally solved my problem.
public override int GetItemPosition(Object objectValue)
            return PositionNone;

As ever, I remained indebted to StackOverflow, and this is the answer that resolved it for me.

Thursday, 12 May 2016

Friday, 22 April 2016

Invoke C# from Javascript in Android using Export attribute

I've been happily invoking C# from a webview in my Xamarin Android app for some while now, but this time I needed to send a parameter from my js. Using the basic Run() was no longer an option and so I turned to a solution using the Export attribute, which I first read about here.
Instead of:
public void Run()
        //do your stuff
you do this:
public void CustomInvoke(string myParameter)
       //do your stuff

Tuesday, 29 March 2016

autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"

How to stop an HTML input field thinking it knows best on mobile:
autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"

Monday, 28 March 2016

Capturing touch events in iOS/Android webviews

I have some HTML content that is shared between a Xamarin Android app and an ObjC/Swift native iOS app. In the HTML are a few buttons that need to trigger events in the native code of each app. The techniques for capturing these events are well documented, but one has to remember that a touch event might actually be a scroll or a swipe, and the use may not be trying to 'click' the element in question. I overcame this problem by using a boolean flag to see if we're moving:
var moved;
$('.myElement').on('touchend', function(e){
    if(moved != true){
        //do your thing
}).on('touchmove', function(e){
    moved = true;
}).on('touchstart', function(){
    moved = false;