Friday, 29 July 2016

Swipe To delete option in UITableView in iOS

Today we will learn how to give swipe to delete option in UITableView in iOS.

Steps:
1. Create a new Xcode Project (Lets say DefaultDeleteOptionTableView).
2. Now go to Your StoryBoard and take a tableView and a Label.
3. Set the delegate and dataSource for the tableView like this.




4. Your storyboard will look like this





5. Now go to Your ViewController.m and write the following Code.



#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end
@implementation ViewController
{
    NSMutableArray *tableDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableDataArray = [[NSMutableArray alloc]initWithObjects:@"Ram",@"Shyam",@"Mohan",@"nilesh", nil];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableView DataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [tableDataArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];
    return cell;
}




#pragma mark - UITableView Delegate Methods

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         [tableDataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       [self.tableView reloadData];
}
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;

}

 Now Run the code and see the desired output.

To download the whole source Code. Please  Click here.
Please have your valuable feedback.
Enjoy Coding :)

Tuesday, 26 July 2016

Twitter and Facebook sharing option in iOS

Today we will learn how to give share option (Facebook and twitter) in iOS using Social framework in iOS.

Steps:
1. Create a Xcode project Lets say (SocialFrameworkDemo).
2. Add Social Framework library in Your Project.
 3. Now go to StoryBoard and create two buttons and make Proper connection.

4.  You can add images in Your ProjectFolder if You want to share images also.
5. Now go to Your ViewController.m and add following code.
#import "ViewController.h"
#import <Social/Social.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)fbButtonAction:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
         SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [controller setInitialText:@"iostpoint - Everything about iOS"];
        [controller addImage:[UIImage imageNamed:@"facebookLogo"]];
        [controller addURL:[NSURL URLWithString:@"https://www.iostpoint.wordpress.com"]];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Facebook not installed or confogured" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
}

- (IBAction)twitterButtonAction:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
         [controller setInitialText:@"iostpoint - Everything about iOS"];
        [controller addImage:[UIImage imageNamed:@"twitterLogo"]];
          [controller addURL:[NSURL URLWithString:@"https://www.iostpoint.blogspot.com"]];
        [self presentViewController:controller animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Facebook not installed or confogured" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

6. Run the code and see the desired output. You can download the whole source code from here.
Please give your valuable feedback.
Enjoy Coding :) 

How to create a DatePicker in iOS using Objective C

The UIDatePicker class implements an object that uses multiple rotating wheels to allow users to select dates and times. iPhone examples of a date picker are the Timer and Alarm (Set Alarm) panes of the Clock application. You may also use a date picker as a countdown timer.
When properly configured, a UIDatePicker object sends an action message when a user finishes rotating one of the wheels to change the date or time; the associated control event is UIControlEventValueChanged. AUIDatePicker object presents the countdown timer but does not implement it; the application must set up anNSTimer object and update the seconds as they’re counted down.

Wednesday, 20 July 2016

How to send message in iOS Programatically

Today we will learn how to send message programmatically in iOS using MessageUI framework.

1. Create a new Xcode project (lets say MessageDemo).
2. Add MessageUI framework.

4. Add one button in Storyboard, your project will look like this.

5. import MessageUI framework and write the following code in your ViewController.m

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController ()<MFMessageComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
    switch (result) {
        case MessageComposeResultCancelled:
            break;
            
        case MessageComposeResultFailed:
        {
            
            UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Failed !" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *defaultAction  = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
            [controller addAction:defaultAction];
            [self presentViewController:controller animated:YES completion:nil];
            break;
        }
            
        case MessageComposeResultSent:
            break;
            
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sendMessage:(id)sender{
    
    if(![MFMessageComposeViewController canSendText]) {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Device not supported !" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *defaultAction  = [UIAlertAction actionWithTitle:@"Okay" style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:defaultAction];
        [self presentViewController:controller animated:YES completion:nil];
        return;
    }
    
    NSArray *recipents = @[@"767676767"];
    NSString *message = @"Check iostpoint.wordpress.com";
    
    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;
    [messageController setRecipients:recipents];
    [messageController setBody:message];
    
    // Present message view controller on screen
    [self presentViewController:messageController animated:YES completion:nil];
}
6. Now run the code.. Run on device because simulator will not support this.

Download the whole Xcode project from here.
Please share and give your valuable feedback if you like it.

Enjoy coding :)

Tuesday, 19 July 2016

How to send mail with attachment in iOS Programatically

The Message UI framework provides specialized view controllers for presenting standard composition interfaces for email and SMS (Short Messaging Service) text messages. Use these interfaces to add message delivery capabilities without requiring the user to leave your app.
To display a composition interface, present the corresponding view controller modally from your app. Once presented, the user has the option to customize the contents before sending or canceling the message. Your custom delegate object then handles the dismissal of the view controller based on the user’s action.

Monday, 18 July 2016

How to create, delete, edit, fetch events from the Calendar using EventKit framework in iOS

Today we will learn how to get access to iOS calendar and then access the events of it. You can read more about EventKit framework here.

Now we will write code for that.

Steps
 1. Create a new Xcode Project (Lets say CalendarDemo).
 2.  Now go to StoryBoard and design your Layout Like this.
3. Basically You have to take 5 buttons and a Label.
4. Make Proper connection for the Buttons.



5. Now import this in your ViewController.m  #import <EventKit/EventKit.h>

6.Now define Action For the Buttons

#import "ViewController.h"
#import <EventKit/EventKit.h>

@interface ViewController ()
@property(strong,nonatomic) NSMutableArray *eventDetailsArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.eventDetailsArray = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)getCalenderAccess:(id)sender {
    EKEventStore *eventStore = [[EKEventStore alloc]init];
    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,NSError* error){
            
            //Access not granted-------------
            if(!granted){
                NSString *message = @"Hey! I Can't access your Calendar... check your privacy settings to let me in!";
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                [controller addAction:defaultaction ];
                [self presentViewController:controller animated:YES completion:nil];
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                }
                               );
                
                //Access granted------------------
            }else{
                
                NSString *message = @"Hey! Thanks For that!";
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Alert" message:message preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *defaultaction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                [controller addAction:defaultaction ];
                [self presentViewController:controller animated:YES completion:nil];            }
        }];
    }
    
    //Device prior to iOS 6.0  *********************************************
    else{
        
        
    }
    
    
}

- (IBAction)createEventButtonAction:(id)sender {
    
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"iostpoint.wordpress.com";
        event.startDate = [NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour delay
        event.calendar = [store defaultCalendarForNewEvents];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        
    }];
}
- (IBAction)deleteEventButtonAction:(id)sender {
    
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:[NSDate date] endDate:[[NSDate date] dateByAddingTimeInterval:60*60] calendars:nil];
        self.eventDetailsArray = [NSMutableArray arrayWithArray:[store eventsMatchingPredicate:fetchCalendarEvents]];
        EKEvent *currentEvent = [self.eventDetailsArray objectAtIndex:0];
        EKEvent *event = [store eventWithIdentifier:currentEvent.eventIdentifier];
        event.calendar = [store defaultCalendarForNewEvents];
        [store removeEvent:event span:EKSpanThisEvent error:nil];
    }];

    
}
- (IBAction)editEventButtonAction:(id)sender {
    
    EKEventStore *store = [EKEventStore new];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:[NSDate date] endDate:[[NSDate date] dateByAddingTimeInterval:60*60] calendars:nil];
        self.eventDetailsArray = [NSMutableArray arrayWithArray:[store eventsMatchingPredicate:fetchCalendarEvents]];
        EKEvent *currentEvent = [self.eventDetailsArray objectAtIndex:0];
        EKEvent *event = [store eventWithIdentifier:currentEvent.eventIdentifier];
        event.title = @"iostpoint.blogspot.com";
        event.calendar = [store defaultCalendarForNewEvents];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        //save the event id if you want to access this later
    }];
    
}

- (IBAction)fetchAllEventButtonAction:(id)sender {
     EKEventStore *store = [[EKEventStore alloc]init];
    NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:[NSDate date] endDate:[[NSDate date] dateByAddingTimeInterval:60*60] calendars:nil];
    self.eventDetailsArray = [NSMutableArray arrayWithArray:[store eventsMatchingPredicate:fetchCalendarEvents]];
       for (int i =0; i< [self.eventDetailsArray count]; i++) {
            EKEvent *event = [self.eventDetailsArray objectAtIndex:i];
           NSLog(@"Event Title %@",event.title);
            }

}

7. Now run the code.
a. First Click the GetAccess Button.
b. Then Click on the Create Event button, it will create new event in your Calendar.

c. Click on edit button. It will change the title of your first event. You can change it according to Your need.

 d. Now click on delete Button, it will delete the first event. Change it according to your need.
If you like this post. Then do share and give your useful feedback.
You can download the whole source code from here.

Enjoy Coding :)

Ads Inside Post

Blogger Widget