Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FacebookUpload.m
- // Masquerade
- //
- // Created by Paschenko Evgene on 3/3/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "FacebookUpload.h"
- #import "FBVideoUpload.h"
- #define APP_ID @"158828574171358"
- #define APP_KEY @"d2ae2f8e0f8b6ba5248df327b2c8af8a"
- #define APP_SECRET @"76a332e4caf59d2f9cb95655c67663e4"
- @implementation FacebookUpload
- @synthesize facebook;
- @synthesize txtvDescription;
- @synthesize txtfTitle;
- @synthesize methodRequest;
- @synthesize lbMessage;
- @synthesize btnLogIn,btnLogOut;
- @synthesize hud;
- @synthesize videoURL;
- @synthesize fileNameVideo;
- @synthesize mimeTypeVideo;
- @synthesize delegate = _delegate;
- //--------------------------------------------------------------------------------------------------------------
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
- [txtfTitle release];
- [txtvDescription release];
- [facebook release];
- [methodRequest release];
- [lbMessage release];
- [btnLogIn release];
- [btnLogOut release];
- [hud release];
- [videoURL release];
- [fileNameVideo release];
- [mimeTypeVideo release];
- [super dealloc];
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- needAnimateForm = NO;
- isUpKeyboard = NO;
- isLogin = NO;
- lbMessage.text = @"You are not authorized";
- btnLogIn.hidden = NO;
- btnLogOut.hidden = YES;
- facebook = [[Facebook alloc] initWithAppId:APP_ID];
- // Observe keyboard hide and show notifications to resize the text view appropriately.
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- hud = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:hud];
- // login to facebook
- [self logIn];
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- }
- //--------------------------------------------------------------------------------------------------------------
- -(void)logIn
- {
- NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"video_upload", nil];
- [facebook authorize:permissions delegate:self];
- }
- //--------------------------------------------------------------------------------------------------------------
- -(void) uploadVideo
- {
- NSMutableDictionary * params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- ([txtfTitle.text length] > 0 ? txtfTitle.text : @"Masquerade Video"), @"title",
- [txtvDescription text], @"description",
- @"json", @"format",
- nil];
- FBVideoUpload *faceBookVideoUpload = [FBVideoUpload new];
- faceBookVideoUpload.apiKey = APP_KEY;
- faceBookVideoUpload.appSecret = APP_SECRET;
- faceBookVideoUpload.accessToken = facebook.accessToken;
- [faceBookVideoUpload startUploadWithURL:videoURL params:params1 delegate:self];
- [faceBookVideoUpload release];
- methodRequest = @"facebook.video.upload";
- hud.labelText = @"Uploading...";
- [hud show:YES];
- }
- //--------------------------------------------------------------------------------------------------------------
- -(void)setVideo:(NSURL*)url fileName:(NSString*)name mimeType:(NSString*)type
- {
- self.videoURL = nil;
- self.videoURL = [url retain];
- self.fileNameVideo = name;
- self.mimeTypeVideo = type;
- }
- //--------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark FBSessionDelegate
- //--------------------------------------------------------------------------------------------------------------
- - (void)fbDidLogin
- {
- NSLog(@"fbDidLogin");
- // request for name
- [facebook requestWithGraphPath:@"me" andDelegate:self];
- methodRequest = @"users.getInfo";
- isLogin = YES;
- btnLogOut.hidden = NO;
- btnLogIn.hidden = YES;
- hud.labelText = @"Loading...";
- [hud show:YES];
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)fbDidLogout
- {
- NSLog(@"fbDidLogout");
- isLogin = NO;
- btnLogIn.hidden = NO;
- btnLogOut.hidden = YES;
- lbMessage.text = @"You are not authorized";
- }
- //--------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark FBRequestDelegate
- //--------------------------------------------------------------------------------------------------------------
- - (void)requestLoading:(FBRequest *)request
- {
- NSLog(@"requestLoading");
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
- {
- NSLog(@"didReceiveResponse");
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)request:(FBRequest *)request didFailWithError:(NSError *)error
- {
- NSLog(@"didFailWithError");
- NSLog(@"%@", error);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
- message:[error localizedDescription]
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- [hud hide:YES];
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)request:(FBRequest *)request didLoad:(id)result
- {
- NSLog(@"didLoad");
- if ( [methodRequest isEqual:@"users.getInfo"] )
- {
- NSDictionary* usetInfo = result;
- NSString* name = [usetInfo objectForKey:@"name"];
- lbMessage.text = name;
- [hud hide:YES];
- }
- else if([methodRequest isEqual:@"facebook.video.upload"] )
- {
- [hud hide:YES];
- id delegate = [self delegate];
- if ([delegate respondsToSelector:@selector(uploadToFacebookCompletedWithTitle:atLink:)]) {
- NSDictionary* uploadInfo = result;
- NSString* videoTitle = [uploadInfo objectForKey:@"title"];
- NSString* videoLink = [uploadInfo objectForKey:@"link"];
- [delegate uploadToFacebookCompletedWithTitle:videoTitle atLink:videoLink];
- }
- }
- }
- //--------------------------------------------------------------------------------------------------------------
- - (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
- {
- NSLog(@"didLoadRawResponse");
- }
- //-----------------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark UITextFieldDelegate
- //-----------------------------------------------------------------------------------------------------------------------
- -(BOOL)textFieldShouldReturn:(UITextField *)textField
- {
- [textField resignFirstResponder];
- return YES;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark UITextViewDelegate
- //-----------------------------------------------------------------------------------------------------------------------
- - (BOOL)textViewShouldBeginEditing:(UITextView *) textView
- {
- needAnimateForm = YES;
- NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"YouTubeDesc" owner:self options:nil];
- UIView* asc_view = [objects objectAtIndex:0];
- NSArray* sub = asc_view.subviews;
- UIButton* done = [sub objectAtIndex:0];
- [done addTarget:self action:@selector(doneInputDesc) forControlEvents:UIControlEventTouchUpInside];
- txtvDescription.inputAccessoryView = asc_view;
- if(isUpKeyboard)
- {
- double kbDuration = 0.5;
- CGRect frame = self.view.frame;
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kbDuration];
- frame.origin.y = -200;
- self.view.frame = frame;
- [UIView commitAnimations];
- }
- return YES;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark Responding to keyboard events
- //-----------------------------------------------------------------------------------------------------------------------
- - (void)keyboardWillShow:(NSNotification *)notification
- {
- if(needAnimateForm)
- {
- NSDictionary* info = [notification userInfo];
- double kbDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- CGRect frame = self.view.frame;
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kbDuration];
- frame.origin.y = -200;
- self.view.frame = frame;
- [UIView commitAnimations];
- }
- needAnimateForm = NO;
- isUpKeyboard = YES;
- }
- //-----------------------------------------------------------------------------------------------------------------------
- - (void)keyboardWillHide:(NSNotification *)notification
- {
- NSDictionary* info = [notification userInfo];
- double kbDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- CGRect frame = self.view.frame;
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:kbDuration];
- frame.origin.y = 0;
- self.view.frame = frame;
- [UIView commitAnimations];
- isUpKeyboard = NO;
- }
- //--------------------------------------------------------------------------------------------------------------
- #pragma mark -
- #pragma mark Actions
- //--------------------------------------------------------------------------------------------------------------
- -(IBAction)cancel
- {
- id delegate = [self delegate];
- if ([delegate respondsToSelector:@selector(uploadToFacebookCanceled)]) {
- [delegate uploadToFacebookCanceled];
- }
- }
- //--------------------------------------------------------------------------------------------------------------
- -(IBAction)upload
- {
- if(!isLogin)
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authorization"
- message:@"You are not authorized to upload videos"
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- return;
- }
- [self uploadVideo];
- }
- //--------------------------------------------------------------------------------------------------------------
- -(IBAction)doneInputDesc
- {
- [txtvDescription resignFirstResponder];
- }
- //--------------------------------------------------------------------------------------------------------------
- -(IBAction)logInOut
- {
- if (isLogin)
- {
- [facebook logout:self];
- }
- else
- {
- [self logIn];
- }
- }
- //--------------------------------------------------------------------------------------------------------------
- @end
Add Comment
Please, Sign In to add comment