Thursday, January 28, 2016

MFMessageComposeViewControllerDelegate not working

MFMessageComposeViewControllerDelegate not working

Steps to work with MFMessageComposeViewController.

1. Create object of MFMessageComposeViewController class.

let messageController = MFMessageComposeViewController()

2. Setting value to object

messageController.body = message // message is String object
messageController.recipients = recipients // recipients is Array of number

3. Setting delegate of object

messageController.messageComposeDelegate = self
//messageController.delegate = self
//Above commented statement is not correct. If you used that in your code then you will never be able to return in to your app

4. Present message controller

presentViewController(messageController, animated: true, completion: { () -> Void in
                    // completion Block
                })

5. Handle delegate

//MARK: - MFMessageComposeViewControllerDelegate
    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
switch (result) {
        case MessageComposeResultCancelled:
            print("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed:
            print("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent:
            print("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
}

It will work like charm.
Please use messageController.messageComposeDelegate = self for setting delegate and that will work perfect.

Wednesday, January 13, 2016

Remove notification observer and close DB connection in swift

Remove notification observer and close DB connection in swift

Which is the proper method to remove notification observer or close Database connection?

Earlier we was calling 'ViewDidUnload' method for this kind of purpose.

But now it is not possible.
According to Swift Book there is deinit block.
Which is going to call immediately before a class instance is deallocated.

Remember here, deinit is not method.


e.g.
deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

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"];