Wednesday, 30 July 2014

How to set view for particular orientation in any view forcefully?



Here using this bellow code you can set any orientation forcefully to any view...


[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")

                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

after put this code you get warning that "PerformSelector may cause a leak because its selector is unknown"

for remove this code you can use this above code with bellow solution...

First define the bellow code in your .m file

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

after use that above code with bellow method...

SuppressPerformSelectorLeakWarning(
         [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
    );

you can use this code in viewDidLoad method or anywhere in which you want to display whole view in particular orientation forcefully

Friday, 25 July 2014

Set All Orientation for only one UIViewController in your whole application.

In AppDelegate class just use this bellow method which is available in iOS 6 and later....

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];// this topViewController is my custom method which i post bellow...

    if ([currentViewController isKindOfClass:[YourClassName class]]) {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;
}

This is custom methods which is used to find out top viewController

- (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }

}

Also For do some changes in your that class in which you want to available different orientation then in that class's use bellow method...

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape Orientation Called");
    }
    else{
        NSLog(@"Potrait Orientation Called");
    }
}  

If Any suggestion then please post comment...

Wednesday, 9 July 2014

Set Landscape Oriantation forcefull with bellow code.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    
    float   angle = M_PI/2//rotate 180°, or 1 π radians

    self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

Thursday, 24 April 2014

Rotate UIView or UIImageView with this bellow code.

 For use this code import QuartzCore framework like bellow

#import <QuartzCore/QuartzCore.h>   

and then use bellow code

   CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    theAnimation.duration=1.0; // Animation duration
    theAnimation.repeatCount=1; // no of times you want to do animation
    theAnimation.autoreverses=YES; // reverses the animation
    theAnimation.fromValue=[NSNumber numberWithFloat:0.0]; // initial stage of animation
    theAnimation.toValue=[NSNumber numberWithFloat:3.0]; // rotation angle

    [yourImageView.layer addAnimation:theAnimation forKey:@"animateRotation"]; // add animation to thelayer of a view for which you want animation.

Thursday, 17 April 2014

open mapview with any address name from iphone application to apple map application

with bellow code you can open any location with its name to apple's mapview application with bellow code..


//    [[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];// you can open user current location with this 1 line  

    Class mapItemClass = [MKMapItem class];
    if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
    {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:yourAddressText
                     completionHandler:^(NSArray *placemarks, NSError *error) {
                         
                         // Convert the CLPlacemark to an MKPlacemark
                         // Note: There's no error checking for a failed geocode
                         CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc]
                                                   initWithCoordinate:geocodedPlacemark.location.coordinate
                                                   addressDictionary:geocodedPlacemark.addressDictionary];
                         
                         // Create a map item for the geocoded address to pass to Maps app
                         MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
                         [mapItem setName:geocodedPlacemark.name];
                         [mapItem openInMapsWithLaunchOptions:nil];

    }

Load URL in UIWebView with special character , append some string with bellow code.

Here you can add any code or string in your local file url with bellow code.

if your url like user/.../filename.htm and if you want to add some hash code of word after that url like user/.../filename.htm#green then you can use my bellow function.

-(void)loadWebView{
    NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"filename" ofType:@"html"];//here write your file name and its extention
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURL *fullURL = [NSURL URLWithString:@"#green" relativeToURL:url];//pass any code or string with append in url baseurl like this
    [yourWebView loadRequest:[NSURLRequest requestWithURL:fullURL]];
    [yourWebView setBackgroundColor:[UIColor clearColor]];
    [yourWebView setOpaque:NO];

}

Tuesday, 4 March 2014

Set and get Date in NSString format or NSDate format with different date format with my bellow method

You can get date with string return type and also set date format in which you want to get date For Ex: dd-MMM-yyyy (i.e. 05-Mar-2014)

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    
    NSDate *date = [dateFormatter dateFromString:stringDate];
    [dateFormatter setDateFormat:getwithFormat];
    
    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);
    return convertedString;
}

NSString  *stringVar  = [self changeDateFormat:yourDateString dateFormat:@"yyyy-MM-dd" getwithFormat:@"dd MMM yyyy"];// set your date string and its date format and after set your required new date format

You can get date with NSDate return type and also set date format in which you want to get date For Ex: dd-MMM-yyyy (i.e. 05-Mar-2014) same like above but here you get return date in NSDate.

-(NSDate *)convertStringToDate:(NSString *)date dateFormat:(NSString *)dateFormat getWithFormat:(NSString *)getWithFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate *nowDate = [[NSDate alloc] init];
    [formatter setDateFormat:dateFormat];// set format here which format in string date
    date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
    nowDate = [formatter dateFromString:date];
    [formatter setDateFormat:getWithFormat];
    
    NSString *convertedString = [formatter stringFromDate:nowDate];
    nowDate = [formatter dateFromString:convertedString];
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
    return nowDate;

}