Monday, June 04, 2012

Play a video in full screen

1. Include MediaPlayer framework in the project
2. Import header file in the view controller
3. Declare the player instance in the controller header file:
MPMoviePlayerViewController *playerViewController;

Sample Code : 
----------------
- (void) playVideo:(NSString *)fileName
{
    NSString *url = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:[playerViewController moviePlayer]];
    [self.view addSubview:playerViewController.view];
   
    //play movie
    MPMoviePlayerController *player = [playerViewController moviePlayer];
    [player play];        
}

// The call back
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];
    player.initialPlaybackTime = -1;
    [player stop];
    [player.view removeFromSuperview];   
    [player release];   
}

Scaling an image in iOS

The UIImage class can be used to represent an image. The following snippet scales an image according to a size which is provided with a CGSize parameter.

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

Tuesday, May 29, 2012

How to Create ,Rename and Delete a file from Documents Directory

Documents Directory

// For error information
NSError *error;
 
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
 
// Document directory
NSString *documentsDirectory = [NSHomeDirectory() 
         stringByAppendingPathComponent:@"Documents"];
 
 
 
Creating a File
// File we want to create in the documents directory 
// Result is: /Documents/file1.txt
NSString *filePath = [documentsDirectory 
         stringByAppendingPathComponent:@"file1.txt"];
 
// String to write
NSString *str = @"Maheswaran.cm";
 
// Write the file
[str writeToFile:filePath atomically:YES 
         encoding:NSUTF8StringEncoding error:&error];
 
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
                           documentsDirectory error:&error]);
 
Renaming a File
 
// Rename the file, by moving the file
NSString *filePath2 = [documentsDirectory 
             stringByAppendingPathComponent:@"file2.txt"];
 
// Attempt the move
if ([fileMgr moveItemAtPath:filePath toPath:filePath2 
                                              error:&error] != YES)
  NSLog(@"Unable to move file: %@", [error localizedDescription]);
 
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
                        documentsDirectory error:&error]);
 
Deleting a File
 
// Attempt to delete the file at filePath2
if ([fileMgr removeItemAtPath:filePath2 error:&error] != YES)
  NSLog(@"Unable to delete file: %@", [error localizedDescription]);
 
// Show contents of Documents directory
NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath:
                                   documentsDirectory error:&error]); 

Wednesday, May 16, 2012

How to get Suck Effect in iOS

[UIView beginAnimations:@"suck" context:NULL];
[UIView setAnimationTransition:103 
                 forView:myViewContainer cache:YES];
[UIView setAnimationPosition:CGPointMake(12, 345)];
[myView removeFromSuperview];
[UIView commitAnimations]; 
 

 /*setAnimationTransition:103, it invokes suck effect.*/

For reference:
http://www.iphonedevwiki.net/index.php?title=UIViewAnimationState

Wednesday, April 04, 2012

How to Create a Universally Unique Identifier (UUID)

- (NSString *)CreateUUID
{
    CFUUIDRef uuidRef = CFUUIDCreate(NULL);
    CFStringRef uuidStringRef = CFUUIDCreateString(NULL, uuidRef);
    CFRelease(uuidRef);
    NSString *uuid = [NSString stringWithString:(NSString *)uuidStringRef];
    CFRelease(uuidStringRef);
    return uuid;
}

How to remind users to Restart App

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
   
    // Current date
    NSDate *date = [NSDate date];
   
    // Add interval to the current time
    NSDate *dateToOpen = [date dateByAddingTimeInterval:timeInterval];
   
    // Set the fire date/time
    [localNotification setFireDate:dateToOpen];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];   
   
    // Setup alert notification
    [localNotification setAlertBody:@"Tap to return to MyApp" ];
    [localNotification setAlertAction:@"Open MyApp"];
    [localNotification setHasAction:YES];
   
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

How to Parse an NSURL Object in iOS

NSURL *url = [NSURL URLWithString:
 @"http://maheswarancm.com:999/2012/April;URLParsing?
                    url=testURL&purpose=testing"];
 
NSLog(@"URL Scheme: %@", [url scheme]); 
NSLog(@"URL Host: %@", [url host]); 
NSLog(@"URL Port: %@", [url port]);     
NSLog(@"URL Path: %@", [url path]);     
NSLog(@"URL Relative path: %@", [url relativePath]);
NSLog(@"URL Path components as array: %@", [url pathComponents]);        
NSLog(@"URL Parameter string: %@", [url parameterString]);   
NSLog(@"URL Query: %@", [url query]);       
NSLog(@"URL Fragment: %@", [url fragment]);
 
output:
URL Scheme: http
URL Host: maheswarancm.com
URL Port: 999
URL Path: /2012/April
URL Relative path: /2012/April
URL Path components as array: (
    "/",
    2012,
    April
)
URL Parameter string: URLParsing
URL Query: url=testURL&purpose=testing
URL Fragment: (null) 

Create a list in SwiftUI with sticky section headers

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