Wednesday, August 15, 2012

iOS, iPhone, iPad, XCode resize view on rotation

Hello,

This is quick blog on how to resize view in iPhone, iPad application. Normally when we use story board all the views we add are of portrait mode and if your app supports rotation, in landscape mode you need to resize your views. So how to do that.

Go to your ViewController file and find the following function.


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Normally function will look as following.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

That means it will only support Portrait orientation. So to allow rotation in your app change this function as follow.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

Now it will allow rotation in application. Now add one more function to your view controller file

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (fromInterfaceOrientation == UIInterfaceOrientationPortrait) {
           //
    }
    else{
    }
}

This function will be invoked automatically when orientation of phone changes. fromInterfaceOrientation will give you the last orientation. That means if you are in porttrait mode and you are now changing to landscape mode, value of fromInterfaceOrientation would be UIInterfaceOrientationPortrait. So in above function you need to reize your view in if else loop.

Hope this helps you.




Friday, August 10, 2012

Sencha Touch Get Yahoo News RSS using JsonP request

Hello,

Recently I was working on project where requirement was to display rss feeds from yahoo news in sencha touch application. For that we have to use JsonP request as it will be cross domain request. If we use simple Ajax request, we will get error of cross domain access and if we use JsonP request, we can not parse the response as normally RSS feed response is in XML format. So what is the solution?

Solution is to use YQL (Yahoo Query Language). YQL is the is the SQL like language that lets you search, filter data across yahoo services. We can write a select statement just like we use select statement for a table in database. We can also use joins to combine more than one results. Checkout following example.


Ext.data.JsonP.request({
            url: 'http://query.yahooapis.com/v1/public/yql',
            params: {
                q: 'select * from rss where url ="http://news.search.yahoo.com/rss?ei=UTF-8&p=USD"',
                format: 'json'
            },
            success: function (response) {
                console.log(response);
            },
            failure: function () {

            }
});

That's it and you have rss feeds in json format using cross domain jsonp request for sencha touch. Above example will return all the news related to USD currency published on yahoo news. This way you can query for any other topic.

Please note that yahoo will not return results if you try to access yql frequently from single IP. This is for security reasons. If you need uninterrupted results, you need to sign up for api and get authentication key and pass it along with the your request. Above example if of JsonP request. Same way you can use it in Ext.data.Store along with JsonP proxy. See the following code.


 Ext.define('MyApp.store.MyStore',{
       extend: 'Ext.data.Store',
       config:{
           model:'MyApp.model.MyModel',
           autoLoad: true,
            proxy : {
                type : 'jsonp',
               url: 'http://query.yahooapis.com/v1/public/yql',
                 reader: {
                  type: 'json',
                  rootProperty: 'query.results.item',
                }
            }
       }
});


Ext.getStore('MyStore').getProxy().setExtraParams({
                   q:'select * from rss where url ="http://news.search.yahoo.com/rss?ei=UTF-8&p=USD",          
                   format: 'json'
              });
Ext.getStore('MyStore').load();


This is the example of dynamic loading of store.


Hope this help you.