Wednesday, April 22, 2015

JavaScript identify Android WebView

Hello,

Recently I was working on the cordova project where we have mobile web application hosted on server. With the same URL loaded in Android webview we created native app. Now we have some functionalities in app that should only be available if URL is loaded in Android WebView like camera capture. For that we have to identify webview with JavaScript. In this blog I will explain how to do this.

I added very simple logic. I set custom user agent for WebView from Native android app and just checked that in JavaScript and set global variable. Here is how you can do that.

First of all open res/values/string.xml and add following line.


<string name="user_agent_suffix">AppName/1.0</string>

Then we this Custom Agent from onCreate function of Android Activity.

this.appView.getSettings().setUserAgentString(
            this.appView.getSettings().getUserAgentString() 
            + " "
            + getString(R.string.user_agent_suffix)
         );

So we are setting custom user agent in Android WebView. Now in our app we just have to check this with UserAgent.

Var isNativeApp  = /AppName\/[0-9\.]+$/.test(navigator.userAgent) ? true : false//For checking native app.

That's it now you can check isNativeApp variable anywhere in app to set functions on if the app is running in Android WebView.


Mageto Cart API - Add Simple Product with Custom Options and Configurable Products

Hello,

Recently I was working with Magento SOAP API and I was using cart_product.add API of Magento. I had to support three types of products.

1) Simple Products
2) Simple Product with Custom Options
3) Configurable Products

If you try to add simple products with custom options without specifying options it will give you error. Same thing for configurable products. You have to specify product options. If you check their documentation, there is no information about adding options and attributes.  So here in this blog I am going to explain how to do that. check following code.


$client = new SoapClient($baseurl.'/api/soap/?wsdl');
        $session = $client->login(USER,PASS);
        $quote_id = $_POST['quote_id'];
        $arrProducts = null;
        if($_POST['has_options'] == "false"){
        $arrProducts = array(
array(
"product_id" => $_POST['product_id'],
"qty" => $_POST['qty']
)
);
        }else{
        $jsondata = $_POST['product_options'];
        $allOptions = json_decode($jsondata,true);
        $productType = $_POST['product_type'];
       
        if($productType == 'simple'){
        $arrProducts = array(
array(
"product_id" => $_POST['product_id'],
"qty" => $_POST['qty'],
'options' => $allOptions
                                               //array("option1"=>"value1","option2"=>"value2")
)
);
        }else if($productType == 'configurable'){
        $arrProducts = array(
array(
"product_id" => $_POST['product_id'],
"qty" => $_POST['qty'],
'super_attribute' => $allOptions
                                               //array("option1"=>"value1","option2"=>"value2")
)
);
        }
        }

As you can see above first we creating SOAP client and then creating $arrProducts. As you can see in above code we are passing product options as JSON encoded data and passing type of product. If product is simple product we set key as "options" and if product type is configurable product we need key as "super_attribute". I hope this helps you.

Friday, April 10, 2015

Cocoa OSX NSTextField Select All Text On Focus

Hello,

Recently I was working on MAC application where there was a requirement to select text inside NSTextField as soon as user focus it. In this blog I am going to explain how to do this.

First add a class to your project and name is CustomTextField. It will create two files CustomTextField.h and CustomTextField.m

Open CustomTextField.h file and add following code to it.

#import

@interface CustomTextField : NSTextField


@end

Now open CustomTextField.m file and add following code to it.

#import "CustomTextField.h"

@implementation CustomTextField

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (BOOL)becomeFirstResponder
{
    BOOL result = [super becomeFirstResponder];
    if(result){
        [self performSelector:@selector(selectText:) withObject:self afterDelay:0];
    }
    return result;
}
@end

As you can see we have added event handler for becomeFirstResponder event. This event is fired as soon as user focus the text field. In that function we are selecting text by calling selector selectText with zero second delay. This will immediately select the text as soon as user focus on it. Now go to your Storyboard or xib file and select the TextField in which you want this behavior. 

Go to identity inspector and select the class CustomTextField for your textfield.

That's it.You can also do additional stuff like changing background color on focus.

[self setBackgroundColor:[NSColor clearColor]];
[self setBackgroundColor:[NSColor redColor]];

Hope this helps you. 

Sencha Touch MessageBox Can Not Be Closed.

Recently in one of my Sencha Touch project, I had an issue with MessageBox, specifically in Android. The issue was when user taps on Ok button message box does not go away. Ideally it should hide, but instead of that it's body is still visible and due to that app was not usable. Issue was hide animation of message box. ActiveAnimation is blocking messagebox from closing properly, so the workaround is to force "end" function of that animation. it was not ended correctly, probably because of events.

Add following code to your application.

 Ext.override(Ext.MessageBox, {    
            hide:  function() {
                if (this.activeAnimation && this.activeAnimation._onEnd) {
                    this.activeAnimation._onEnd();
                }
                return this.callParent(arguments);
            }
});

This will solve your problem. Now message box will be hidden as soon as you tap on Ok button. Hope this helps you.