Friday, September 11, 2015

BITCODE error/warning in iOS 9 Xcode 7

BITCODE error/warning in iOS 9 Xcode 7

Today I got the error of BITCODE in project.
After some searching I became to knew that yesterday I upgrade my Xcode from 6.4 to 7 beta 5.

detail of error is as below


ld: '/Users/Mrugesh/Documents/NewApp/NewApp/Fabric.framework/Fabric(Fabric.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The error says that i used Fabric framework which was not build with bitcode and new Xcode requires that.
So the solution is that either we have to use framework which build with bitcode or just disable bitcode from project setting for desired target like me.

Project setting > target > Build setting > Build Options > Enable Bitcode > NO

And problem solved.

Another problem I faced yesterday was that I was fetching data from API and for that I was using http URL instead of https.  And It stops send request.
For that I added dictionary to info.plist which is as below.

Wednesday, August 19, 2015

working with UIMenuController

working with UIMenuController

I am creating menu on longPressGesture of some view.
The code is as below.

- (void)showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        
        [self becomeFirstResponder];

        /*
         Set up th menu.
         */
        UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item 1" action:@selector(item1Selected:)];
        UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item 2" action:@selector(item2Selected:)];

        UIMenuController *menuController = [UIMenuController sharedMenuController];
        [menuController setMenuItems:@[menuItem1,menuItem2]];
        
        CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
        CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
        [menuController setTargetRect:menuLocation inView:[gestureRecognizer view]];
        
        [menuController setMenuVisible:YES animated:YES];
    }

}

- (void)item1Selected:(UIMenuController *)controller
{
    NSLog("Item 1 Selected");
}

- (void)item2Selected:(UIMenuController *)controller
{
    NSLog("Item 2 Selected");
}

// UIMenuController requires that we can become first responder or it won't display
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

Saturday, June 27, 2015

Get color from touch point

Get color from touch point

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self.view];
    self.pickedColor = [self colorOfPoint:loc];
    self.theColor.backgroundColor = self.pickedColor;
}

-(UIColor *) colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.view.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

Tuesday, June 16, 2015

Taking input from user via alert

Taking input from user via alert

UIAlertController * alert=   [UIAlertController alertControllerWithTitle:@"Text" message:@"Enter Text" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Do Some action here
                                                   UITextField *textField = alert.textFields.firstObject;
                                                   NSLog(@"%@",textField.text);
                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                                   }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                        textField.placeholder = @"Enter text here";
    }];
    

    [self presentViewController:alert animated:YES completion:nil];

Wednesday, April 22, 2015

Saving data in NSUserDefaults

Saving data in NSUserDefaults

If You want to save some details of user in device then you can use NSUserDefaults.
It just like SharedPreference or cookie.

To write data in NSUserDefaults use below code.

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"myKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

To read data from NSUserDefaults use below code.

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"myKey"];

Thursday, April 2, 2015

How to create Singleton in Objective c

How to create Singleton in Objective c

1)Add Class which inherits NSObject
2)Implement following in YourSingleton.h
                    
#import <foundation/Foundation.h>

@interface YourSingleton: NSObject {
    NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedInstance;

@end
 
 
3)In YourSingleton.m
 
#import "YourSingleton.h"

@implementation YourSingleton

@synthesize someProperty;

#pragma mark Singleton Methods

+ (id)sharedInstance{
    static YourSingleton*sharedInstance= nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance= [[self alloc] init];
    });
    return sharedInstance;
}

- (id)init {
  if (self = [super init]) {
      someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
  }
  return self;
}

- (void)dealloc {
  // Should never be called, but just here for clarity really.
}

@end
 
 
 
Its Done.....Now you can use it whenever you want. :)
 
 
 
Note : Do not user for creating UIkitcomponent because reference of instance of sigleton are never released.... 

Tuesday, March 24, 2015

Change font and size in watchkit

Change font and size in watchkit



  1. Create IBOutlet of your WKInterfaceLabel
  2. Create UIFont object

    UIFont
    * labelFont = [UIFont fontWithName:@"Courier-Bold" size:8];
  3. Create NSAttributedString object

    NSAttributedString *labelText = [[NSAttributedString alloc] initWithString : @"my Label string title" attributes : @{ NSKernAttributeName : @2.0, NSFontAttributeName : labelFont}];
  4. set attributedString to label

    [self.label setAttributedText:labelText];

Monday, February 16, 2015

Get User Current Location

Get User Current Location

  1. Import CoreLocation Framework to your Project
  2. #import <CoreLocation/CoreLocation.h> to your .h file
  3. Add CLLocationManagerDelegate to your .h file
  4. Now make object of CLLocationManager class
    e.g. 
    CLLocationManager *locationManager
  5. After this add following code to ViewDidLoad

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager requestWhenInUseAuthorization];
    [locationManager requestAlwaysAuthorization];
    locationManager.distanceFilter=kCLDistanceFilterNone;    
    [locationManager startUpdatingLocation];
  6. Add two Delegate method to your .m file

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"Latitude:%f Longitude:%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
        
    [locationManager stopUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        
    }
  7. Add Properties to your info.plist file

    NSLocationAlwaysUsageDescription = location required
    NSLocationWhenInUseUsageDescription = Location is required to find out where you are
I got failure to get location because of not adding Property in Info.plist file.
In new versions of Xcode and iOS this is mandatory.

If you are testing in simulator then you have to put breakPoint before startUpdatingLocation and at the execution time you have to select city manually through Xcode. You can also add .GPX file for same.

Saturday, February 14, 2015

Share via Whatsapp in ios

Share via Whatsapp in ios

There are two method to share via whatsapp
  1. URL scheme
  2. Document Interaction API
If you want to go with URL Scheme
then here is example.

/**Just open whatsapp*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://app"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

/** Send Hello World! to any contact/group*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

/** Send message to selected contact*/
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://abid?/*Address Book ID*/"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

And if you want to go with Document Interaction API then check whatsapp documentation.