Sunday, 2 February 2014

some Useful Links

Facebook and twitter post

ADD Framework:Social,Accounts

ViewController.h

#import <Social/Social.h>
#import <Accounts/Accounts.h>
@interface ViewController : UIViewController
-(IBAction)twitterPost:(id)sender;

-(IBAction)facebookPost:(id)sender;

Viewcontroller.M

-(IBAction)facebookPost:(id)sender{
    
    SLComposeViewController *controller = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeFacebook];
    SLComposeViewControllerCompletionHandler myBlock =
    ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"Cancelled");
        }
        else
        {
            NSLog(@"Done");
        }
        [controller dismissViewControllerAnimated:YES completion:nil];
    };
    controller.completionHandler =myBlock;
    //Adding the Text to the facebook post value from iOS
    [controller setInitialText:@"My test post"];
    //Adding the URL to the facebook post value from iOS
    [controller addURL:[NSURL URLWithString:@"http://www.test.com"]];
    //Adding the Text to the facebook post value from iOS
    [self presentViewController:controller animated:YES completion:nil];
}

-(IBAction)twitterPost:(id)sender{
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"My test tweet"];
    [self presentModalViewController:tweetSheet animated:YES];
}

Sending Mail

Add framework :MessageUI

Viewcontroller.h
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>{
     MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

ViewController.M
-(IBAction)sendMail:(id)sender{
    mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Test mail"];
    [mailComposer setMessageBody:@"Testing messagefor the test mail" isHTML:NO];
     [self presentModalViewController:mailComposer animated:YES];
     }
     
-(void)mailComposeController:(MFMailComposeViewController *)controller
             didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
                 if (result) {
                     NSLog(@"Result : %d",result);
                 }
                 if (error) {
                     NSLog(@"Error : %@",error);
                 }
                 [self dismissModalViewControllerAnimated:YES];
                 
             }

Adjust size of the image programmatically-Taj

 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"taj.jpg"]];
    imageView.frame = CGRectMake(50, 100, 217  ,217);

    self.view.center=imageView.center;

Move button through coding

viewcontroller.h
 IBOutlet UIButton *button;

viewController.m

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[[event allTouches]anyObject];
    button.center=[touch locationInView:self.view];

}

Counting Timer

ViewController.h

@interface ViewController : UIViewController{
    NSTimer *timer;
    int MainInt;
     IBOutlet UIButton *drag;
}

@property (weak, nonatomic) IBOutlet UILabel *l1;
- (IBAction)click:(id)sender;
-(void)countup;
- (IBAction)stopTimer:(id)sender;

- (IBAction)reset:(id)sender;

viewcontroller.M

-(void)countup {
    MainInt += 1;
    l1.text = [NSString stringWithFormat:@"%i", MainInt];
    
    
}

- (IBAction)click:(id)sender {
    //MainInt = 0;
    if (timer==nil)
    {
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self selector:@selector(countup)userInfo:nil repeats:YES];
        
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[[event allTouches]anyObject];
    drag.center=[touch locationInView:self.view];
    
}
- (IBAction)stopTimer:(id)sender
{
    if (timer != nil)
    {
        [timer invalidate];
        timer = nil;
    }
}

- (IBAction)reset:(id)sender {
    MainInt=0;
    
    
}

Audio and Video

1. ADD AVFoundation and Mediaplayer  Framworks
2.Import header file <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

3.ViewController.h
@interface ViewController : UIViewController{
AVAudioPlayer *audioPlayer;
    MPMoviePlayerViewController *moviePlayer;

}
-(IBAction)playAudio:(id)sender;

-(IBAction)playVideo:(id)sender;

4.ViewController.M
-(IBAction)playAudio:(id)sender{
    NSString *path = [[NSBundle mainBundle]
                      pathForResource:@"song" ofType:@"mp3"];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:
                   [NSURL fileURLWithPath:path] error:NULL];
    [audioPlayer play];
}
-(IBAction)playVideo:(id)sender{
    NSString *path = [[NSBundle mainBundle]pathForResource:
                      @"video" ofType:@"mov"];
    moviePlayer = [[MPMoviePlayerViewController
                    alloc]initWithContentURL:[NSURL fileURLWithPath:path]];

    [self presentModalViewController:moviePlayer animated:YES];
}