Showing posts with label IPAD. Show all posts
Showing posts with label IPAD. Show all posts

Thursday, September 27, 2012

IPhone: Pass data between view controllers

There are several ways to pass the data between the view controllers.
1.      Delegates & Protocols
2.      NSUserDefaults
3.      Database
4.      Singleton Object
Here I am going to explain using Delegates & Protocols.
First we will get an idea about the protocols and delegates.

Protocols

 Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:
·         To declare methods that others are expected to implement
·         To declare the interface to an object while concealing its class
·         To capture similarities among classes that are not hierarchically related
 Declaring a Protocol
You declare formal protocols with the @protocol directive:
@protocol ProtocolName
method declarations
@end
Optional Protocol Methods
Protocol methods can be marked as optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If you do not specify any keyword, the default is @required.
@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end

Delegates

iOS uses Objective-C delegates to implement the delegation pattern, in which one object passes work off to another. The object doing the work is the delegate of the first object. An object tells its delegate to do work by sending it messages after certain things happen.
Delegates are basically a way of extending the behavior of a class without needing to create subclasses. Applications in iOS often use delegates when one class calls back to another after an important action occurs.
For Java Developers:
Protocol is similar to Interface
Protocol
Interface
@protocol ProtocolName
-(void)methodName(NSString *):value;
@end
public interface ProtocolName {   
public void methodName(String value);
}
Declare protocol in .h file and import the .h file, wherever protocol has to be called
implements the interface and have a logic in a class

Sample Code : Here 
Screenshots:
View Controller Loaded
Entered Sample Text and Pressed Updated Button

Data transferred to Second View controller
 
Edited the Text and Pressed Back
Edited Text updated in first view controller
 

Thursday, July 26, 2012

IPhone: Solution for [[UIDevice currentDevice] uniqueIdentifier]]


[[UIDevice currentDevice] uniqueIdentifier] is deprecated in iOS5

APPLE DOCUMENTATION:
To create a unique identifier specific to your app, you can call the CFUUIDCreate function to create a UUID, and write it to the defaults database using the NSUserDefaults
/*
 * Generates UUID using CFUUIDCreate
 */
- (NSString *) generateUUID
{
    NSString *  result;
    CFUUIDRef   uuid;
    CFStringRef uuidStr;
    
    uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);
    
    uuidStr = CFUUIDCreateString(NULL, uuid);
    assert(uuidStr != NULL);
    
    result = [NSString stringWithFormat:@"%@", uuidStr];
    assert(result != nil);
    
    CFRelease(uuidStr);
    CFRelease(uuid);
    
    return result;
}

// Use the Below Code to save the UUID into the NSUserDefaults, so that we can use, anywhere in the application.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *fetchUUID = [defaults objectForKey:@"UUID"];
if (fetchedUUID == nil || [fetchedUUID isEqualToString:@""])
{
     NSLog(@"Checking in if");
     [defaults setObject:[self generateUUID] forKey:@"UUID"];
}    
fetchUUID = [defaults objectForKey:@"UUID"];

Use the above code to get UUID in iOS5.

Saturday, June 9, 2012

IPhone / IPAD : Custom Language Settings for NSString & UIImage

By Changing Language settings in Settings >General >International >Change Language. We can achieve the localization to the Phone and Application.

If we want to handle the localization only for the application but not to the Phone.
Then we can customize application as follows

We know how to use the localization by using NSLocalizedString, 
which returns a localized version of a string. by using following command
NSString *NSLocalizedString(NSString *key, NSString *comment) 

We have localization for Image as follows

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
self.image = [UIImage imageWithContentsOfFile:imageName];

But if you want to localize Image using your custom settings then we can try the following snippet code.

For Eg: If you’re App supports English and German.
But, If your Phone default language is using French and 
Your app should use as per your language settings.
 
How to achieve following use case?

Provide Language Settings in your app using settings.bundle 

By Using the above changes in your settings.bundle file, 
we can see as follows language settings in your app.
Now, How to get the selected language?
There is code snippet. one way, where we can achieve localization 
/*
 * languageSelectedStringForKey used to get the localized value using key.
 */
NSUserDefaults* defaults;

- (NSString *)getLocalizationPath
{
    NSString *path;
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults synchronize];
  
    NSString *defPhoneLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
  
    NSString *langCode = [defaults stringForKey:@"testLanguage"];
  
    if([langCode isEqualToString:@"0"])
    {
        if([defPhoneLanguage isEqualToString:@"de"] || [defPhoneLanguage isEqualToString:@"en"])
        {
            path = [[NSBundle mainBundle] pathForResource:defPhoneLanguage ofType:@"lproj"]; 
        }
        else
        {
            path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; 
        }
    }
    else if([langCode isEqualToString:@"German"] || [langCode isEqualToString:@"de"])
    {
        path = [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"]; 
    }
    else
    {
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];      
    }
    return path;
}

-(NSString*) getLocaleStringForKey:(NSString*) key
{
    NSString *path = [self getLocalizationPath];
  
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
  
    return str;
}

-(NSString*) getLocaleImageForKey:(NSString*) key
{
    NSString *path = [self getLocalizationPath];
  
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString *str = [[languageBundle resourcePath] stringByAppendingPathComponent:key];
  
    return str;
}

Custom Locale Image:
[UIImage imageWithContentsOfFile:[self getLocaleImageForKey:@"sample.png"]]


Have a nice day... Enjoy :)