Friday, 6 June 2014

Check wifi is available on large file upload/download in iOS


Check wifi is available on large file upload/download in iOS

The handleEventsForBackgroundURLSession Method

The purpose of the background transfer service is to allow large downloads or uploads to take place even when the associated application is placed in the background. In the situation where a transfer is in progress and the application is placed in the background, the handleEventsForBackgroundURLSession: delegate method of the application’s delegate class will be called and passed a reference to a completion handler which must be called by the application when the transfer is complete. The next step in this tutorial, therefore, is to implement this method and set up a property in which to store a reference to the completion handler. Begin by selecting the BackgroundTransferAppDelegate.h file adding a property for the completion handler reference as follows:
#import <UIKit/UIKit.h>

@interface BackgroundTransferAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (copy) void (^sessionCompletionHandler)();
@end
Next, edit the BackgroundTransferAppDelegate.m file and add the delegate method so that the completion handler reference is stored:
#import "BackgroundTransferAppDelegate.h"

@implementation BackgroundTransferAppDelegate

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
  completionHandler:(void (^)())completionHandler
{
        self.sessionCompletionHandler = completionHandler;
}
.
.
.
@end

Designing the User Interface

Select the Main.storyboard  file and drag and drop an Image View object onto the canvas, resizing it to fill the entire view. With the image view selected in the canvas, select the Xcode Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints menu option. This will add constraints attaching the image view to each side of the superview.
With the Image View object still selected, display the Attributes Inspector and change the Mode property to Aspect Fit. Display the Assistant Editor and ensure that it is displaying the content of the BackgroundTransferViewController.h file. Ctrl-click on the Image View in the canvas and drag the resulting line to a position just beneath the @interface line in the Assistant Editor panel. Release the line and establish an outlet for the image view named imageView.
With the BackgroundTransferViewController.h file still displayed, further modify the file to add properties to store the NSURLSession and NSURLSessionDownloadTask instances and to declare that the class implements a number of delegate protocols:
#import <UIKit/UIKit.h>

@interface BackgroundTransferViewController : UIViewController
<NSURLSessionDelegate, NSURLSessionTaskDelegate, 
                     NSURLSessionDownloadDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic) NSURLSession *session;
@property (nonatomic) NSURLSessionDownloadTask *downloadTask;
@end

Configuring the View Controller

The next step is to begin to implement the functionality in the view controller class. First, edit the file and import the App Delegate interface file (access to the app delegate instance will be needed later to access the reference to the completion handler) and declare the URL to the image file that is to be downloaded:
#import "BackgroundTransferViewController.h"
#import "BackgroundTransferAppDelegate.h"

static NSString *DownloadURLString = 
               @"http://www.ebookfrenzy.com/code/LargeImage.jpg";

@interface BackgroundTransferViewController ()

@end

@implementation BackgroundTransferViewController
.
.
.
@end
Next, edit the viewDidLoad method in the BackgroundTransferViewController.m file to begin the initialization process:
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionConfiguration *configuration = 
        [NSURLSessionConfiguration   
          backgroundSessionConfiguration:@"com.ebookfrenzy.transfer"];
    configuration.allowsCellularAccess = YES;

    _session = [NSURLSession sessionWithConfiguration:configuration 
         delegate:self delegateQueue:nil];

    NSURL *downloadURL = [NSURL URLWithString:DownloadURLString];

    NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
    
    _downloadTask = [self.session downloadTaskWithRequest:request];

    [_downloadTask resume];
}
The above code begins by creating a new NSURLSessionConfiguration instance, configured to perform a background transfer and specifying a unique identifier for the session (in this case the package name of the application):
NSURLSessionConfiguration *configuration = 
    [NSURLSessionConfiguration   
       backgroundSessionConfiguration:@"com.ebookfrenzy.transfer"];
configuration.allowsCellularAccess = YES;
By calling the backgroundSessionConfiguration method in the above code we are indicating that the session is required to perform file transfer related tasks. An option is also set to allow the transfer to use the cellular connection on the device if WiFi is not available. For large transfers, this should be set to NO, or the user given the opportunity to configure this setting to avoid incurring additional costs on their cellular data plan.
An NSURLSession instance is then created using the configuration object and designating the current class as the delegate for the session:
_session = [NSURLSession sessionWithConfiguration:configuration 
         delegate:self delegateQueue:nil];
Next, an NSURL object is created containing the URL of the image file to be downloaded and an NSURLRequest object initialized with this URL:
NSURL *downloadURL = [NSURL URLWithString:DownloadURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
Finally, the download task request is created and resumed (tasks do not start at the point of creation and will not run until the resume method is called):
_downloadTask = [self.session downloadTaskWithRequest:request];
[_downloadTask resume];

All that remains is to implement the delegate methods.

Thursday, 5 June 2014

Kiip Integration in iOS

Kiip Integration in iOS

Initialize

Once you register an iPhone app on our application dashboard, we’ll automatically generate an App Key and App Secret–keep them handy when you start the initialization process. In general, you should initialize within AppDelegates. You’ll need to import the Kiip library whenever you want to initialize.
The method to initialize the Kiip SDK takes the following parameters:
  • @param appKey (NSString): The applications key, provided by Kiip.
  • @param appSecret (NSString): The applications secret, provided by Kiip.
Kiip *kiip = [[Kiip alloc] initWithAppKey:@"app_key" andSecret:@"app_secret"];

Lifecycle

When you initialize your application, the KiipDelegate parameter added in your .h file will take care of managing moments. Something like below is all you’ll need:
@interface MyAppDelegate : UIResponder <UIApplicationDelegate, KiipDelegate>

Moments

When you save a moment with the SDK, it’s automatically registered with us on the dashboard. You will need to make sure the completionHandler is up and running, to handle error. There are two ways to save a moment.

Save a moment

  • @param momentId (NSString): The unique identifier of the moment. This will dynamically create a moment for tracking in the Kiip system. This is an arbitrary name chosen by you.
  • @withCompletionHandler: A callback that must be created to show the poptart and handle errors.
[[Kiip sharedInstance] saveMoment:@"Beat Level One" withCompletionHandler:^(KPPoptart *poptart, NSError *error) {
    if (error){
        NSLog( @"something's wrong");
    }
    if (poptart){
        [poptart show];
    }
}];

Save a moment with a value

  • @param momentId (NSString): The unique identifier of the moment. This will dynamically create a moment for tracking in the Kiip system. This is an arbitrary name chosen by you.
  • @param value (double): This is checked against a threshold (high or low) set in the Kiip dashboard. A user will only be rewarded if they reach the threshold, or fall below it if you’ve selected inverse.
  • @withCompletionHandler: A callback that must be created to show the poptart and handle errors.
[[Kiip sharedInstance] saveMoment:@"Test High Score" value:5.0 withCompletionHandler:^(KPPoptart *poptart, NSError *error) {
    if (error){
        NSLog( @"something's wrong");
    }
    if (poptart){
        [poptart show];
    }
}];

Object Properties

The Kiip object allows you to attach user information automatically without user intervention. For example, if you already have a user’s e-mail, you can pre-populate a reward unit when you reward your users.

Object

Set the following properties on the Kiip sharedInstance.
Kiip *kiip = [Kiip sharedInstance];

Email

  • @property (NSString) *email Set’s a user’s email address so it auto-populate’s in the reward.
kiip.email = @"support@kiip.me"

Gender

  • @property (NSString) *gender Set’s a user’s gender to help with targeting relevant rewards.
kiip.gender = @"Male"

Birthday

  • @property (NSDate) *birthday Set’s a user’s birthday to help with targeting relevant rewards.
kiip.birthday = @"03/19/1990"

Poptart Overview

Poptart is the name of the entire reward workflow Kiip manages for you. By calling [poptart show]when you save a moment, Kiip enables the following routine to run:
saveMoment called > Poptart onShow > Modal onShow > Modal onDismiss > Poptart onDismiss

Event Listeners

Kiip’s SDK can notify you of events that occur, such as showing and hiding notifications. While saving a moment, on the [Kiip sharedInstance] class, you can set listeners for:
  • poptarts
  • notifications
  • modals
  • virtual currency

Poptarts : KPPoptartDelegate

KPPoptartDelegate is the overarching modal and notification system. The delegate can be accessed in the completion handler of a saveMoment call by doing:
[[Kiip sharedInstance] saveMoment: momentId withCompletionHandler:^(KPPoptart *poptart){
   
   // setting the delegate before showing poptart
   poptart.delegate = self; 
   
   // show poptart
   [poptart show]
   
}];
Within the same file, you’ll have to then implement the listener. Something like the following will get you set up:
- (void)willPresentPoptart: (KPPoptart *) poptart {
    NSLog( @"Can't wait for my poptart!");
}
The following methods can be implemented for the poptart delegate.

willPresentPoptart

Called before the poptart is presented to the user.

didDismissPoptart

Called after the poptart was dismissed.

Modals : KPModalDelegate

The KPModal is the fullscreen view of a Kiip reward. The delegate can be accessed in the completion handler of a saveMoment call by doing:
[[Kiip sharedInstance] saveMoment: momentId withCompletionHandler:^(KPPoptart *poptart){
    
    // set notification delegate. 
    KPModal *modal = poptart.modal;
    modal.delegate = self;
    
    // show poptart
    [poptart show]
}];
Like the poptart listeners, you’ll have to implement the modal listeners in the same file, something along the lines of:
- (void)willPresentModal: (KPModal *) modal {
    NSLog( @"Here comes a reward!");
}
The following methods can be implemented for the Modal delegate:

didDismissModal

Called after the modal was dismissed, either by being closed or a reward being redeemed.

willPresentModal

Called before the modal is presented to the user.

Video

Starting with SDK 2.0.8, we’ve included video rewards to present to your user. In order to optimize the Kiip experience, we’ll notify you before a video is about to play, and when the video has ended (or if the user has chosen to close the video manually).

kiipVideoPlaybackDidBegin

Called right before a video will play. Usually used to mute volume in your app.

kiipVideoPlaybackDidEnd

Called right after a video is finished, or a user closes a video before it finishes. Resume background noise.

Virtual Currency Listener

This listener is called when a user has been rewarded virtual currency.
  • @param kiip: The Kiip instance.
  • @param content (NSString): The identifier of the content that should be awarded to the user.
  • @param quantity (int): The amount of content that should be awarded to the user.
  • @param transactionID (NSString): The unique identifier of this transaction.
  • @param signature (NSString): The signature that can be checked against the Kiip API to validate this transaction.
- (void) kiip:(Kiip *)kiip didReceiveContent:(NSString *)contentId quantity:(int)quantity transactionId:(NSString *)transactionId signature:(NSString *)signature {
    // Give the currency to the user.
}

Custom Notifications

The Kiip SDK allows you to attach your own assets when you show a notification view. This promotes a higher level of immersion for your app’s UX. Step one is to ensure you are saving moments correctly. Once that’s taken care of, navigate to your saveMoment callback.
After proper error checking, look at the following section within the withCompletionHandler:
    // currently inside the withCompletionHandler
    if (poptart){
    
    // Set your rect coordinates and properties as you wish.
    KPNotificationView *notificationView = [[KPNotificationView alloc] initWithFrame:CGRectMake(0,0,320,50)];
    notificationView.backgroundColor = [[UIColor yourChoiceOfColor]];
    
    // note, this kiip var comes from the Kiip sharedInstance you declared earlier.
    kiip.notificationView = notificationView  
    
    // Now that the view is constructed, let's allocate for the notification itself 
    // and assign it to poptart's notification to override it.  
    KPNotification *notification = [[KPNotification alloc] init]
    poptart.notification = notification;
    
    // Programmatically create your UIView
    // customView ...
    
    // Attach to notificationView
    [notificationView addSubview: customView]
    
    // Let's start poptart with our new notification! 
    notificationView.poptart = poptart;
    [poptart show];
}

App is portrait mode but play video in landscape in iOS



App is portrait mode but play video in landscape in iOS


You need to first change in appdelegate:

Property synthesize   allowRotation in appdelegate.
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}
Register Notifications for the full screen control in your viewController.m :
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
                                             name:MPMoviePlayerWillEnterFullscreenNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
                                             name:MPMoviePlayerWillExitFullscreenNotification
                                           object:nil];


//Write these two methods in viewController.m
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
    self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
    self.allowRotation = NO;
}

More detail you can see following link:
See this link
Second link:

Friday, 11 April 2014

Display size and icon size of iOS devices


                                      Display size and icon size of iOS devices




Resolutions & Display Specifications
Resolutions
Devices
Portrait
Landscape
iPhone 5
iPhone 5, 5S, 5C
640x1136 px
1136x640 px
iPhone 4/4S
640x960 px
960x640 px
iPhone & iPod Touch
1st, 2nd and 3rd Generation
320x480 px
480x320 px
Retina iPad
iPad 3, iPad 4, iPad Air
1536x2048 px
2048x1536 px
iPad Mini
768x1024 px
1024x768 px
iPad Mini Retina
1536x2048 px
2048x1536 px
iPad
1st and 2nd Generation
768x1024 px
1024x768 px
Displays
Devices
PPI
Color Mode
Color Temperature
iPhone 5
iPhone 5, 5S, 5C
326
8bit RGB
Warm
iPhone 4/4S
326
8bit RGB
Cool
iPhone & iPod Touch
1st, 2nd and 3rd Generation
163
8bit RGB
Warm
Retina iPad
iPad 3, iPad 4, iPad Air
264
8bit RGB
Warm
iPad Mini
163
8bit RGB
Warm
iPad Mini Retina
326
8bit RGB
Warm
iPad
1st and 2nd Generation
132
8bit RGB
Warm
App Icons
One of the biggest changes in iOS 7 is the new dimensions and the visual language used for app icons. Apple introduced a grid system, increased the general size of icons on your home screen and also masked icons with a different shape.
Dimensions

Device
App Icon
AppStore Icon
Spotlight Icon
Settings Icon
iPhone 5
iPhone 5, 5S, 5C
120x120 px
1024x1024 px
80x80 px
58x58 px
iPhone 4/4S
120x120 px
1024x1024 px
80x80 px
58x58 px
Retina iPad
iPad 3, 4, Air, Mini Retina
152x152 px
1024x1024 px
80x80 px
58x58 px
iPad Mini
76x76 px
512x512 px
40x40 px
29x29 px
iPad
1st and 2nd Generation
76x76 px
512x512 px
40x40 px
29x29 px

Monday, 14 October 2013

NSArray in objective-C

An array is an object that contains collections of other objects. Array objects in Objective-C are handled using the Foundation Framework NSArray class. The NSArray class contains a number of methods specifically designed to ease the creation and manipulation of arrays within Objective-C programs. Unlike some object oriented programming languages (C# being one example), the objects contained in an array do not all have to be of the same type.

  • 1 Mutable and Immutable Arrays
  • 2 Creating an Array Object
  • 3 Finding out the Number of Elements in an Array
  • 4 Accessing the Elements of an Array Object
  • 5 Accessing Array Elements using Fast Enumeration
  • 6 Adding Elements to an Array Object
  • 7 Inserting Elements into an Array
  • 8 Deleting Elements from an Array Object
  • 9 Sorting Array Objects
Mutable and Immutable Arrays
Array objects in Objective-C come in mutable and immutable forms. The contents of immutable arrays cannot be changed after the array has been initialized. Immutable arrays are instantiated from the NSArray class. Mutable arrays are created using the NSMutableArray class (a subclass of NSArray) and can be modified after they have been created and initialized.
Creating an Array Object
The NSArray class contains a class method named arrayWithObjects that can be called upon to create a new array object and initialize it with elements. For example:
NSArray *myColors;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
The above code creates a new array object called myColors and initializes it with four constant string objects containing the strings "Red", "Green", "Blue" and "Yellow". A nil entry is required so that the methods called upon to work on the array know where the end of the array is located. Failure to include this entry may result in the application crashing, particularly during sort operations.
Because we used the NSArray class in the above example the contents of the array object cannot be changed subsequent to initialization. To create a mutable array that will allow the array contents to be modified, we need to use the NSMutableArray class:
NSMutableArray *myColors;

myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
Finding out the Number of Elements in an Array
The number of objects in an array (referred to as elements) can be identified using the count instance method of the NSArray class:
NSArray *myColors;
      
myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

NSLog (@"Number of elements in array = %lu", [myColors count]);
When executed, the above code will output the following:
Number of elements in array = 4
Accessing the Elements of an Array Object
The objects contained in an array are given index positions beginning at position zero. Each element may be accessed by passing the required index position through as an argument to the NSArray objectAtIndex instance method. We can, therefore, now extend our array example to display each element, using the count method to identify in advance how many elements there are to display:
NSArray *myColors;
int i;
int count;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
count = [myColors count];

for (i = 0; i < count; i++)
        NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
When run, the above code will display each element in the array object:
Element 0 = Red
Element 1 = Green
Element 2 = Blue
Element 3 = Yellow

Accessing Array Elements using Fast Enumeration
The technique for accessing all the array elements using a for loop as described in the previous section is a little ungainly. Another, easier mechanism for accessing element in an array involves something called fast enumeration. Fast enumeration simply requires that a variable be declared to hold each array element, and then referenced in the for loop:
NSArray *myColors;
NSString *color;

myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

for (color in myColors)
        NSLog (@"Element = %@", color);
Adding Elements to an Array Object
New elements may be added to a mutable array object using the addObject instance method of the NSMutableArray class. For example, to declare and initialize an array, and then later add new element object the following code might be used:
NSMutableArray *myColors;

myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors addObject: @"Indigo"];
[myColors addObject: @"Violet"];
Inserting Elements into an Array
The previous method appends new objects onto the end of an array. It is also possible to insert new objects at specific index points in an array object using the insertObject instance method. This method accepts as arguments the object to be inserted and the index position at which the insertion is to take place:
NSMutableArray *myColors;
int i;
int count;

myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors insertObject: @"Indigo" atIndex: 1];
[myColors insertObject: @"Violet" atIndex: 3];

count = [myColors count]; 

for (i = 0; i < count; i++)
        NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
When we compile and run the code, the following output confirms that the new objects were indeed inserted at the specified index positions:
Element 0 = Red
Element 1 = Indigo
Element 2 = Green
Element 3 = Violet
Element 4 = Blue
Element 5 = Yellow
Deleting Elements from an Array Object
The NSMutableArray class provides a number of instance methods designed specifically to remove one or more elements from an array object. A sample of some of the more commonly used methods is provided below. This list is not exhaustive so refer to the Foundation Framework documentation for the NSMutableArray class for a full listing.
To remove an element at a specific index location, use the removeObjectAtIndex method:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors removeObjectAtIndex: 0];
To remove the first instance of a specific object from an array use removeObject:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors removeObject: @"Red"];
To remove all instances of a specific object in an array, use removeObjectIdenticalTo:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", @"Red", @"Red", nil];

[myColors removeObjectIdenticalTo: @"Red"];
To remove all objects from an array, use removeAllObjects:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors removeAllObjects];
To remove the last object in the array, use the removeLastObject method:
myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

[myColors removeLastObject];
Sorting Array Objects
The Foundation Framework NSArray class, and the subclasses thereof, provide a number of mechanisms for sorting the elements of an array into a specific order. The simplest way to achieve this is to use the sortedArrayUsingSelector instance method. For example, to perform a sort on our example array using this method, we could use the following code:
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil];

NSArray *sortedArray;


sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];