Monday, June 10, 2013

Execute HTTP GET in Android

Sample Code :

try {
       HttpClient client = new DefaultHttpClient();
       String targetUrl = "http://www.example.com";
       HttpGet httpGet = new HttpGet(targetUrl);
       HttpResponse response = null;       
       response = client.execute(httpGet);
       HttpEntity entity = response.getEntity();
       if(entity!=null){
             Log.v("GET RESPONSE", EntityUtils.toString(entity));
       }
}
catch(Exception e){
       e.printStackTrace();
}

Thursday, June 06, 2013

Get the frame of a view inside another view

Converts a rectangle from the coordinate system of another view to that of the receiver.
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view
Sample :
CGRect frame = [imageView convertRect:button.frame toView:self.view];


Tuesday, April 30, 2013

Atomic vs nonatomic properties


@property(nonatomic, retain)( NSString *userName;

@property(atomic, retain) NSString *userName;

@property(retain) NSString *userName;


The last two are identical; "atomic" is the default behavior (note that it is not actually a keyword; it is specified only by the absence of nonatomic -- atomic was added as a keyword in recent versions of llvm/clang).


Assuming that you are @synthesizing the method implementations, atomic vs. non-atomic changes the generated code. If you are writing your own setter/getters, atomic/nonatomic/retain/assign/copy are merely advisory. (Note: @synthesize is now the default behavior in recent versions of LLVM. There is also no need to declare instance variables; they will be synthesized automatically, too, and will have an _ prepended to their name to prevent accidental direct access).


With "atomic", the synthesized setter/getter will ensure that a whole value is always returned from the getter or set by the setter, regardless of setter activity on any other thread. That is, if thread A is in the middle of the getter while thread B calls the setter, an actual viable value -- an autoreleased object, most likely -- will be returned to the caller in A.
In nonatomic, no such guarantees are made. Thus, nonatomic is considerably faster than "atomic".


What "atomic" does not do is make any guarantees about thread safety. If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned -- the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell.

Ensuring data integrity -- one of the primary challenges of multi-threaded programming -- is achieved by other means.

Monday, April 29, 2013

How to get the available font from iOS Device ?

NSString *familyName;
NSString *fontName;
   
for(familyName in [UIFont familyNames])
    {
        NSLog(@"\nName of the Family: %@", familyName);
       
       
for(fontName in [UIFont fontNamesForFamilyName:familyName])
            NSLog(@"\tName of the Font: %@\n", fontName);
    }

Tuesday, March 12, 2013

Introduction to Core Data






Core Data: it is used to store data from your iPhone application into a Sqlite file which is present in the document directory of your application. Core Data is not a relational database, you can see Core data as a wrapper around Sqlite although core data is quite simpler as compared to Sqlite but it does not offer some of the functionality that Sqlite can offer and vice versa.



ManagedObject: Managed objects are the objects that are created by your application code to store data. A managed object can be thought of as a row or a record in a relational database table. For each new record to be added, a new managed object must be created to store the data. Similarly, retrieved data will be returned in the form of managed objects, one for each record matching the defined retrieval criteria. Managed objects are actually instances of the NSManagedObject class, or a subclass thereof. These objects are contained and maintained by the managed object context.



Persistence store coordinator: The persistent store coordinator is responsible for coordinating access to multiple persistent object stores. As an iPhone developer you will never directly interact with the persistence store coordinator and, in fact, will very rarely need to develop an application that requires more than one persistent object store. When multiple stores are required, the coordinator presents these stores to the upper layers of the Core Data stack as a single store.


Managed Object Context: Core Data based applications never interact directly with the persistent store. Instead, the application code interacts with the managed objects contained in the managed object context layer of the Core Data stack. The context maintains the status of the objects in relation to the underlying data store and manages the relationships between managed objects defined by the managed object model. All interactions with the underlying database are held temporarily in within the context until the context is instructed to save the changes, at which point the changes are passed down through the Core Data stack and written to the persistent store.

Source: http://qs4int.blogspot.in/2013/01/core-data.html
 

Thursday, March 07, 2013

Simple Example For Categories

NSDate+ComponentsExtractor.h:


 
typedef struct 
{
   NSInteger year;
   NSInteger month;
   NSInteger day;
} dateComponents;

@interface NSDate (ComponentsExtractor)
+ (dateComponents)componentsFromDate:(NSDate *)theDate;
@end
 
NSDate+ComponentsExtractor.m: 

#import "NSDate+ComponentsExtractor.h"

@implementation NSDate (ComponentsExtractor)

+ (dateComponents)componentsFromDate:(NSDate *)theDate
{
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:
                                            NSGregorianCalendar] autorelease];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | 
                                  NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags 
                                          fromDate:theDate];
    
    dateComponents theComponents;
    theComponents.year = [components year];
    theComponents.month = [components month];
    theComponents.day = [components day];
    
    return theComponents;
}

@end

Making use of the category:
NSDate *theDate = [NSDate date]; // use the current date ...
dateComponents components = [NSDate componentsFromDate:theDate];
NSLog(@"%i - %i - %i", components.year, components.month, components.day);
 
 

Friday, March 01, 2013

Shortening URLs in iOS


NSString *url = @"http://www.example.com";
NSString *apiUrl = [NSString stringWithFormat:@"http://api.tr.im/v1/trim_simple?url=%@",url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiUrl]
encoding:NSASCIIStringEncoding
error:nil];
NSLog(@"Long: %@ = Short: %@",url,shortURL);

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...