Wednesday, December 26, 2012

How to check Multitasking capablity in iOS ?

- (BOOL) isMultitaskingCapable
{
 UIDevice* device = [UIDevice currentDevice];
 BOOL backgroundSupported = NO;
 if ([device respondsToSelector:@selector(isMultitaskingSupported)])
  backgroundSupported = device.multitaskingSupported;
 
 return backgroundSupported;
}

How to check Retina display available in phone ?

+ (BOOL) isRetinaDisplay
{
 int scale = 1.0;
 UIScreen *screen = [UIScreen mainScreen];
 if([screen respondsToSelector:@selector(scale)])
  scale = screen.scale;
 
 if(scale == 2.0f) return YES;
 else return NO;
}

Monday, December 10, 2012

Android JSON Parser Example



The first thing we will do is setup our JSON string, which we'll end up parsing.
 
String jsonStr = '{"menu": {' + 
            '"id": "file",' + 
            '"value": "File",' + 
            '"popup": {' + 
              '"menuitem": [' + 
                '{"value": "New", "onclick": "CreateNewDoc()"},' + 
                '{"value": "Open", "onclick": "OpenDoc()"},' + 
                '{"value": "Close", "onclick": "CloseDoc()"}' + 
              ']' + 
            '}' + 
          '}}'


import org.json.JSONObject;

JSONObject jsonObj = new JSONObject(jsonStr);

With that instantiated, we can do the following to retreive different pieces of data from the JSON string - you will also need the following import for JSONArray:import org.json.JSONArray; and import android.util.Log; for Log.



// grabbing the menu object 
JSONObject menu = jsonObj.getJSONObject("menu"); 
 
// these 2 are strings 
String id = menu.getString("id"); 
String value = menu.getString("value"); 
 
// the popop is another JSON object 
JSONObject popup = menu.getJSONObject("popup"); 
 
// using JSONArray to grab the menuitems from under popop 
JSONArray menuitemArr = popupObject.getJSONArray("menuitem");  
 
// lets loop through the JSONArray and get all the items 
for (int i = 0; i < menuitemArr.length(); i++) { 
    // printing the values to the logcat 
    Log.v(menuitemArr.getJSONObject(i).getString("value").toString()); 
    Log.v(menuitemArr.getJSONObject(i).getString("onclick").toString()); 
} 

Friday, December 07, 2012

Android: Enable and Disable WiFi Programmatically

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(enabled);

Internet Connection Check in Android


ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = mgr.getActiveNetworkInfo();
if (netInfo != null) {
      if (netInfo.isConnected()) {
              // do something
      } else {
            AlertDialog.Builder alertbuilder = new AlertDialog.Builder(MyActivity.this);
            alertbuilder.setTitle("Internet");
            alertbuilder.setMessage("Internet is not available");
            alertbuilder.setNeutralButton("Ok", okClickListener);
           alertbuilder.show();
     }
} else {
      AlertDialog.Builder alertbuilder = new AlertDialog.Builder(
      MyActivity.this);
      alertbuilder.setTitle("Internet");
      alertbuilder.setMessage("Internet is not available");
      alertbuilder.setNeutralButton("Ok", okClickListener);
      alertbuilder.show();
}

SD Card Present in Android


public static boolean isSdCardPresent()
{
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

Retrieve JSON from a REST web service in Android


String result = queryRESTurl("http://location/json.json");
try {
JSONObject json = new JSONObject(result);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
for (int i = 0; i < valArray.length(); i++)
Log.i(TAG, "\n" + nameArray.getString(i) + "\n\n" + "\n" + valArray.getString(i) + "\n");
}   
catch (JSONException e) {       
Log.e("JSON", "There was an error parsing the JSON", e);
public String queryRESTurl(String url) {  
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);   
HttpResponse response;        
try {       
response = httpclient.execute(httpget); 
Log.i(TAG, "Status:[" + response.getStatusLine().toString() + "]"); 
HttpEntity entity = response.getEntity();
if (entity != null) {  
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.i(TAG, "Result of converstion: [" + result + "]"); 
instream.close();            
return result;       
}    
} catch (ClientProtocolException e) {  
Log.e("REST", "There was a protocol based error", e); 
} catch (IOException e) {   
Log.e("REST", "There was an IO Stream related error", e); 
}       
return null;
}

Thursday, December 06, 2012

Converting NSStrings to uppercase, lowercase and capitalizing each word in a string

NSString *str = @"easy tips for programming";
 
// Convert string to uppercase
NSString *upperStr = [str uppercaseStringWithLocale:[NSLocale currentLocale]];
NSLog(@"Upper Case: %@", upperStr);
 
// Convert string to caps
NSString *capStr = [upperStr capitalizedStringWithLocale:[NSLocale currentLocale]];
NSLog(@"Capital: %@", capStr);
 
// Convert string to lowercase
NSString *lowerStr = [capStr lowercaseStringWithLocale:[NSLocale currentLocale]];
NSLog(@"Lower Case: %@", lowerStr);
 
output:
Upper Case: EASY TIPS FOR PROGRAMMING
Capital: Easy Tips For Programming
lower Case: easy tips for programming
   
  

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...