Tuesday, 18 June 2013

Force start iOS application in Landscape Mode OR Portrait Mode.


If you want your application to be in Landscape mode.

STEP 1: Click on "MyProject.xcodeproj" file 
STEP 2: Select the TARGET Goto Project summary.
STEP 3: Choose supported interface orientations as landscape.



#import <objc/message.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Force start in Landscape Mode
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];


 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight);
        }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    NSLog(@"orientation:%d",[[UIDevice currentDevice]orientation]);

    return YES;
}


You can use the steps for portrait mode, just change the orientation to portrait mode in supported orientation. And in STEP 3: 
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortraitRight];



If you support all the orientations, then you need to update your configurations in 
Info.p-list.

Either
Using Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</plist>

OR

Just add "Supported interface orientations" and values in the array starting with
For Landscape:
  "UIInterfaceOrientationLandscapeRight" or "UIInterfaceOrientationLandscapeLeft"
For Portrait: 
   "UIInterfaceOrientationPortrait" or "UIInterfaceOrientationPortraitUpsideDown"

Read JSON from NSBundle file iOS.


-(void)fetchJSONDict
{
//  Goto the filePath

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"JsonFile" ofType:@"txt"];
    NSString *jsonContent = [NSString stringWithContentsOfFile:filePath usedEncoding:nil error:nil];
    NSLog(@"content Json is:%@",jsonContent);

    NSData *data = [jsonContent dataUsingEncoding:NSUTF8StringEncoding];
    id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; //Check out other options NSJSONReadingOptions available. 


//  Check the class type of the object fetched.


    if([object isKindOfClass:[NSDictionary class]]) {
        _dataRXDict = [[NSDictionary alloc]initWithDictionary:object];
        NSLog(@"Json Object recieved is dictionary");
        NSLog(@"Json Object Test:%@",[object valueForKey:@"myKey"]);
    }
    else if([object isKindOfClass:[NSArray class]]) {
        _dataRXArray = [[NSArray alloc]initWithArray:object];

        NSLog(@"Json Object recieved is array");
        NSLog(@"Json Object Test:%@",[object objectAtIndex:0]);
    }
    else if([object isKindOfClass:[NSString class]]) {
        _dataRXString = [[NSDictionary alloc]initWithDictionary:object];

        NSLog(@"Json Object recieved is string");
        NSLog(@"Json Object Test:%@",object);
    }
}


Monday, 17 June 2013

iOS Image Gallery Viewer


Making an app for showing an image gallery in iPhone and iPad?
Have a look at various sample available:
Three20 and EGOPhotoViewer developed by enormego.
MWPhotoBrowser which is a better option.

MPMusicPlayerController with AirPlay


When creating an audio app which uses iPod Music Player, 
I need Airplay functionality. I  used RoutePlayButton in MPVolumeView slider. I don't have apple TV to test if it will work or not.
1.) To add Airplay functionality to MPMusicPlayerController
MPVolumeView *myVolumeView;
    [myVolumeView setShowsRouteButton:YES];
2.) To test airplay without Apple T.V.
AirServer is a paid app. I tried the trial version, it was working fine on Mac-Mini. Please let me know if anyone come across any free app. As @Prince suggested.

You can also try AirPlayer 0.28 beta application and transform your Mac into anAirPlay-compatible display. Change Sharing in System Preferences, Tick internet Sharing by making WiFi On. Use same WIFI in device also.

Merge two UIImage to make single UIImage.


- (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
{
 // get size of the first image
 CGImageRef firstImageRef = first.CGImage;
 CGFloat firstWidth = CGImageGetWidth(firstImageRef);
 CGFloat firstHeight = CGImageGetHeight(firstImageRef);

 // get size of the second image
 CGImageRef secondImageRef = second.CGImage;
 CGFloat secondWidth = CGImageGetWidth(secondImageRef);
 CGFloat secondHeight = CGImageGetHeight(secondImageRef);

 // build merged size
 CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight, secondHeight));

 // capture image context ref
 UIGraphicsBeginImageContext(mergedSize);

 //Draw images onto the context
 [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
 [second drawInRect:CGRectMake(0, 0, secondWidth, secondHeight)]; 

 // assign context to new UIImage
 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

 // end context
 UIGraphicsEndImageContext();

 return newImage;
}