Monday 3 February 2014

File Management in iOS

File Management in iOS


File Management using UserName Save Process:

NSString *file = @"/Login.txt";
        NSString *savedusername = aText.text; //saved text File Value
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];
        NSString *filePath = [docDirectory stringByAppendingString:file];
        [savedusername writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];



File Management using UserName Retrive Process:

  NSString *file = @"/Login.txt";
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];
        NSString *filePath = [docDirectory stringByAppendingString:file];
        NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",filePath);
NSLog(@"%@", fileContents);  //show the contents of file



File Management using UserName Update Process:

Step 1 :  Retrive process to retrive the file contents
Step 2 :  Nsstring to save the file contents
Step 3 :  Nsstring to add the new content &Old content
Step 4 :  Save process to save the new added strings



Example - File Management using UserName Retrive Process with Validation:

NSString *file = @"/Login.txt";
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDirectory = [arrayPaths objectAtIndex:0];
        NSString *filePath = [docDirectory stringByAppendingString:file];
        NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",filePath);
NSLog(@"%@", fileContents);  //show the contents of file
     
//Validation Process
        if(fileContents!=nil)
        {
            if ([fileContents isEqualToString:@"|data missing|"])
            {
                UIAlertView *objAlert = [[UIAlertView alloc] initWithTitle:@"Message!" message:@"Unable to connect. Please try again" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Try Again",nil];
                [objAlert show];
                [objAlert release];
             
                UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                               initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(logP)];
                self.navigationItem.rightBarButtonItem = saveButton;
                [saveButton release];
            }
         
            else if ([fileContents isEqualToString:@"|InvalidUdid |"])
            {
                UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                               initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(logP)];
                self.navigationItem.rightBarButtonItem = saveButton;
                [saveButton release];
            }
         
            else if ([fileContents isEqualToString:@"|Deactivated|"])
            {
                UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                               initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(logP)];
                self.navigationItem.rightBarButtonItem = saveButton;
                [saveButton release];
            }
         
            else if ([fileContents isEqualToString:@""])
            {
                UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                               initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(logP)];
                self.navigationItem.rightBarButtonItem = saveButton;
                [saveButton release];
            }
         
            else if ([fileContents length]>=20)
            {
                UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                               initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                               style:UIBarButtonItemStylePlain
                                               target:self
                                               action:@selector(logP)];
                self.navigationItem.rightBarButtonItem = saveButton;
                [saveButton release];
            }
         
            else
            {
                self.username = fileContents;
                NSLog(@"%@",username);
            }
}
else
        {
            UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                           initWithTitle:NSLocalizedString(@"Login", @"Save - for button to save changes")
                                           style:UIBarButtonItemStylePlain
                                           target:self
                                           action:@selector(logP)];
            self.navigationItem.rightBarButtonItem = saveButton;
            [saveButton release];
        }







Orientation Lock Code for Specified Pages in iOS App

Orientation Lock Code for Specified Pages in iOS Apps



For 5.0 & Below 5.0


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return NO;
}



For 6.0 & Above 6.0

Code implemented in (void)didrotate & (void)viewwillappear

- (void)viewWillAppear:(BOOL)animated
{
    if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
    {
        [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
    }
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
    {
        [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
    }
}


For 7.0, 8.0 Above 7.0, 8.0


Lock a Orientation for App in XCode 6.1  

In Project Settings should change into support orientation to Unlock(Tick-Mark) the all items.

The code used to one Time Orientation Lock. 
When run the (Portrait-Lock)code in your app, The app is support Autorotate Function. If you want to Autorotate, You can run the (Portrait-UnLock)code in your app. 

If want use in Single Page, Use the (Portrait-Lock)code in viewWillAppear (or) viewDidAppear and (Portrait-UnLock)code used in viewWillDisappear (or) Navigate Button (or) Back Button. 


AppDelegate.h File

@property (nonatomic) BOOL screenIsPortraitOnly;


AppDelegate.m File

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
    if (self.screenIsPortraitOnly)
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    else
    {
        if(self.window.rootViewController)
        {
            UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
            orientations = [presentedViewController supportedInterfaceOrientations];
        }
        return orientations;
    }
}



ViewController.m File

//Orientation UnLock Code
-(void)viewWillAppear:(BOOL)animated
{
    [self portraitUnLock];
}

(or)

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:true];
    [self portraitUnLock];
}

//Orientation Lock Code
-(void)viewWillDisappear:(BOOL)animated
{
    [self portraitLock];
}

(or)

//Button Code
- (IBAction)Back_Clicked:(id)sender
{
    [self portraitLock];
[self.navigationController popViewControllerAnimated:YES];
}


-(void)portraitLock
{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = true;
}


-(void)portraitUnLock
{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.screenIsPortraitOnly = false;
}

Orientation Code for iOS Apps

Orientation Code for iOS Apps



Code implemented in (void)didrotate & (void)viewwillappear

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if((orientation==UIDeviceOrientationLandscapeLeft)||(orientation==UIDeviceOrientationLandscapeRight))
{
// Code
}
else if((orientation == UIDeviceOrientationPortrait) || (orientation == UIDeviceOrientationPortraitUpsideDown) || (orientation == 0))
    {
// Code
}
}


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super viewWillAppear:animated];

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if((orientation==UIDeviceOrientationLandscapeLeft)||(orientation==UIDeviceOrientationLandscapeRight))
{
// Code
}
else if((orientation == UIDeviceOrientationPortrait) || (orientation == UIDeviceOrientationPortraitUpsideDown) || (orientation == 0))
    {
// Code
}
}

AdWhirl in iOS

iAd Framework Did Fail AdMob Framework Load

iAd Framework Did Fail AdMob Framework Load



Sample App : 
https://github.com/chrisjp/CJPAdController/archive/master.zip


Framework Files : 
CJPAdController/Demo - CJPAdController.h & CJPAdController.m

.h File :
We have set the prefered ad:
// Set which ads should be initially displayed and preferred ("iAd" or "AdMob")

static NSString * const kDefaultAds = @"iAd";

We have set the prefered location of ad: 
// Where to position the ad on screen ("top" or "bottom")

static NSString * const kAdPosition = @"top";


Single iAd runs on Whole Application

Single iAd runs on Whole Application



Sample App : 
https://developer.apple.com/library/ios/samplecode/iAdSuite/iAdSuite.zip


Framework Files : 
ContainerBanner --> BannerViewController.h & BannerViewController.m



iAd Framework in iOS

iAd Framework in Xcode


Add the Framework :   iAd.framework

.h File:

#import <iAd/iAd.h>
@interface ViewController : UIViewController<ADBannerViewDelegate>
{
            IBOutlet ADBannerView *adView;
BOOL bannerIsVisible;
}
@property(nonatomic,retain)IBOutlet ADBannerView *adView;
@end;



.m File:

@synthesize adView;

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    adView.hidden=NO;
    NSLog(@"adView had ads. Showing.");
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    adView.hidden=YES;
    NSLog(@"adView has no ads. Hiding.");
}

- (void)dealloc
{
    adView.delegate=nil;
    [adView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
adView = [[ADBannerView alloc] 
  initWithFrame:CGRectMake(0,0,280,320)];
adView.requiredContentSizeIdentifiers = 
[NSSet setWithObjects: 
ADBannerContentSizeIdentifierPortrait,
ADBannerContentSizeIdentifierLandscape, nil];
self.adView = adView;
adView.delegate = self;
[self.view addSubview:adView];
if ([[[UIDevice currentDevice] systemVersion] compare:@"5.0.1" options:NSNumericSearch] == NSOrderedAscending) {
        
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
        
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        
        NSLog(@"iOS version is below 5.0.1");
        
    } 
    else 
   {
        
     adView.requiredContentSizeIdentifiers = [NSSet     setWithObject:ADBannerContentSizeIdentifierPortrait];
        
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        
        NSLog(@"iOS version is 5.0.1 or above");
        
    }
}