Showing posts with label iOS. Show all posts
Showing posts with label iOS. 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.

Tuesday, May 1, 2012

Saturday, April 28, 2012

Java : Apple Push Notification Server Side Coding

JavaPNS is a Java library to send notifications through the Apple Push Notification Service to iOS devices. Using JavaPNS, we can send Push notification in a single line of code.

For further details, refer site : http://code.google.com/p/javapns/
The Apple Push Notification Service is a service created by Apple Inc. 
iOS apps can provide three types of notifications:
  • Sounds: Plays an audible alert
  • Alerts/Banners: Displays an alert or banner on the screen
  • Badges: Displays an image or number on the application icon
 Before sending a notification from server, we need to have the following.
  1. Device Token of the device (IPhone/IPod).
  2. Private Key created from certificate.
  3. Have a password of the private key created.
Note: If the private key is not proper, SSLHandshakeException occurs

For Eg:
Java Code looks like
Push.alert(message, keystore, password, production, devices)



Push.alert("Hello World!", "keystore.p12", "keystore_password", false, "Your token");
Parameters:
message : Content to pass
keystore : Private key
password : Private key password
producation : If true, production else sandbox
devices : one or more device token, device token length is 64 digits. The device token is analogous to a phone number; it contains information that enables APNs to locate the device on which the client application is installed.

To run the server code, required jar files list
bcprov-1.45.jar
commons-io-1.3.jar
commons-logging-1.1.1.jar
JavaPNS_2.2.jar
log4j-1.2.16.jar


Thanks for reading :).
Whether this post is helpful?
Please leave a comment.

Wednesday, March 14, 2012

Developer view : iOS View controller LifeCycle from Android Activity LifeCycle




Thanks for reading :)
If you have any other quick thoughts/hints that you think people will find useful, feel free to leave a comment.