Guest User

Untitled

a guest
Oct 20th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. //
  2. // FacebookUpload.m
  3. // Masquerade
  4. //
  5. // Created by Paschenko Evgene on 3/3/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "FacebookUpload.h"
  10. #import "FBVideoUpload.h"
  11.  
  12.  
  13. #define APP_ID @"158828574171358"
  14. #define APP_KEY @"d2ae2f8e0f8b6ba5248df327b2c8af8a"
  15. #define APP_SECRET @"76a332e4caf59d2f9cb95655c67663e4"
  16.  
  17. @implementation FacebookUpload
  18.  
  19. @synthesize facebook;
  20. @synthesize txtvDescription;
  21. @synthesize txtfTitle;
  22. @synthesize methodRequest;
  23. @synthesize lbMessage;
  24. @synthesize btnLogIn,btnLogOut;
  25. @synthesize hud;
  26. @synthesize videoURL;
  27. @synthesize fileNameVideo;
  28. @synthesize mimeTypeVideo;
  29. @synthesize delegate = _delegate;
  30.  
  31. //--------------------------------------------------------------------------------------------------------------
  32. - (void)dealloc
  33. {
  34. [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
  35.  
  36. [txtfTitle release];
  37. [txtvDescription release];
  38. [facebook release];
  39. [methodRequest release];
  40. [lbMessage release];
  41. [btnLogIn release];
  42. [btnLogOut release];
  43. [hud release];
  44. [videoURL release];
  45. [fileNameVideo release];
  46. [mimeTypeVideo release];
  47.  
  48. [super dealloc];
  49. }
  50. //--------------------------------------------------------------------------------------------------------------
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54.  
  55. needAnimateForm = NO;
  56. isUpKeyboard = NO;
  57. isLogin = NO;
  58. lbMessage.text = @"You are not authorized";
  59. btnLogIn.hidden = NO;
  60. btnLogOut.hidden = YES;
  61.  
  62. facebook = [[Facebook alloc] initWithAppId:APP_ID];
  63.  
  64. // Observe keyboard hide and show notifications to resize the text view appropriately.
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  66. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  67.  
  68. hud = [[MBProgressHUD alloc] initWithView:self.view];
  69. [self.view addSubview:hud];
  70.  
  71. // login to facebook
  72. [self logIn];
  73. }
  74. //--------------------------------------------------------------------------------------------------------------
  75. - (void)viewDidUnload
  76. {
  77. [super viewDidUnload];
  78. }
  79. //--------------------------------------------------------------------------------------------------------------
  80. -(void)logIn
  81. {
  82. NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"video_upload", nil];
  83. [facebook authorize:permissions delegate:self];
  84. }
  85. //--------------------------------------------------------------------------------------------------------------
  86. -(void) uploadVideo
  87. {
  88. NSMutableDictionary * params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  89. ([txtfTitle.text length] > 0 ? txtfTitle.text : @"Masquerade Video"), @"title",
  90. [txtvDescription text], @"description",
  91. @"json", @"format",
  92. nil];
  93.  
  94. FBVideoUpload *faceBookVideoUpload = [FBVideoUpload new];
  95. faceBookVideoUpload.apiKey = APP_KEY;
  96. faceBookVideoUpload.appSecret = APP_SECRET;
  97. faceBookVideoUpload.accessToken = facebook.accessToken;
  98. [faceBookVideoUpload startUploadWithURL:videoURL params:params1 delegate:self];
  99. [faceBookVideoUpload release];
  100. methodRequest = @"facebook.video.upload";
  101. hud.labelText = @"Uploading...";
  102. [hud show:YES];
  103. }
  104. //--------------------------------------------------------------------------------------------------------------
  105. -(void)setVideo:(NSURL*)url fileName:(NSString*)name mimeType:(NSString*)type
  106. {
  107. self.videoURL = nil;
  108. self.videoURL = [url retain];
  109. self.fileNameVideo = name;
  110. self.mimeTypeVideo = type;
  111. }
  112. //--------------------------------------------------------------------------------------------------------------
  113. #pragma mark -
  114. #pragma mark FBSessionDelegate
  115. //--------------------------------------------------------------------------------------------------------------
  116. - (void)fbDidLogin
  117. {
  118. NSLog(@"fbDidLogin");
  119.  
  120. // request for name
  121. [facebook requestWithGraphPath:@"me" andDelegate:self];
  122. methodRequest = @"users.getInfo";
  123.  
  124. isLogin = YES;
  125.  
  126. btnLogOut.hidden = NO;
  127. btnLogIn.hidden = YES;
  128.  
  129. hud.labelText = @"Loading...";
  130. [hud show:YES];
  131. }
  132. //--------------------------------------------------------------------------------------------------------------
  133. - (void)fbDidLogout
  134. {
  135. NSLog(@"fbDidLogout");
  136.  
  137. isLogin = NO;
  138. btnLogIn.hidden = NO;
  139. btnLogOut.hidden = YES;
  140.  
  141. lbMessage.text = @"You are not authorized";
  142. }
  143. //--------------------------------------------------------------------------------------------------------------
  144. #pragma mark -
  145. #pragma mark FBRequestDelegate
  146. //--------------------------------------------------------------------------------------------------------------
  147. - (void)requestLoading:(FBRequest *)request
  148. {
  149. NSLog(@"requestLoading");
  150. }
  151. //--------------------------------------------------------------------------------------------------------------
  152. - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
  153. {
  154. NSLog(@"didReceiveResponse");
  155. }
  156. //--------------------------------------------------------------------------------------------------------------
  157. - (void)request:(FBRequest *)request didFailWithError:(NSError *)error
  158. {
  159. NSLog(@"didFailWithError");
  160. NSLog(@"%@", error);
  161.  
  162. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
  163. message:[error localizedDescription]
  164. delegate:nil
  165. cancelButtonTitle:@"OK"
  166. otherButtonTitles:nil];
  167.  
  168. [alert show];
  169. [alert release];
  170.  
  171. [hud hide:YES];
  172. }
  173. //--------------------------------------------------------------------------------------------------------------
  174. - (void)request:(FBRequest *)request didLoad:(id)result
  175. {
  176. NSLog(@"didLoad");
  177.  
  178. if ( [methodRequest isEqual:@"users.getInfo"] )
  179. {
  180. NSDictionary* usetInfo = result;
  181. NSString* name = [usetInfo objectForKey:@"name"];
  182. lbMessage.text = name;
  183. [hud hide:YES];
  184. }
  185. else if([methodRequest isEqual:@"facebook.video.upload"] )
  186. {
  187. [hud hide:YES];
  188.  
  189. id delegate = [self delegate];
  190. if ([delegate respondsToSelector:@selector(uploadToFacebookCompletedWithTitle:atLink:)]) {
  191.  
  192. NSDictionary* uploadInfo = result;
  193.  
  194. NSString* videoTitle = [uploadInfo objectForKey:@"title"];
  195. NSString* videoLink = [uploadInfo objectForKey:@"link"];
  196.  
  197. [delegate uploadToFacebookCompletedWithTitle:videoTitle atLink:videoLink];
  198. }
  199. }
  200. }
  201. //--------------------------------------------------------------------------------------------------------------
  202. - (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data
  203. {
  204. NSLog(@"didLoadRawResponse");
  205. }
  206. //-----------------------------------------------------------------------------------------------------------------------
  207. #pragma mark -
  208. #pragma mark UITextFieldDelegate
  209. //-----------------------------------------------------------------------------------------------------------------------
  210. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  211. {
  212. [textField resignFirstResponder];
  213. return YES;
  214. }
  215. //-----------------------------------------------------------------------------------------------------------------------
  216. #pragma mark -
  217. #pragma mark UITextViewDelegate
  218. //-----------------------------------------------------------------------------------------------------------------------
  219. - (BOOL)textViewShouldBeginEditing:(UITextView *) textView
  220. {
  221. needAnimateForm = YES;
  222.  
  223. NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"YouTubeDesc" owner:self options:nil];
  224. UIView* asc_view = [objects objectAtIndex:0];
  225. NSArray* sub = asc_view.subviews;
  226. UIButton* done = [sub objectAtIndex:0];
  227. [done addTarget:self action:@selector(doneInputDesc) forControlEvents:UIControlEventTouchUpInside];
  228.  
  229. txtvDescription.inputAccessoryView = asc_view;
  230.  
  231. if(isUpKeyboard)
  232. {
  233. double kbDuration = 0.5;
  234.  
  235. CGRect frame = self.view.frame;
  236. [UIView beginAnimations:nil context:nil];
  237. [UIView setAnimationDuration:kbDuration];
  238. frame.origin.y = -200;
  239. self.view.frame = frame;
  240. [UIView commitAnimations];
  241. }
  242.  
  243. return YES;
  244. }
  245. //-----------------------------------------------------------------------------------------------------------------------
  246. #pragma mark -
  247. #pragma mark Responding to keyboard events
  248. //-----------------------------------------------------------------------------------------------------------------------
  249. - (void)keyboardWillShow:(NSNotification *)notification
  250. {
  251. if(needAnimateForm)
  252. {
  253. NSDictionary* info = [notification userInfo];
  254. double kbDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  255.  
  256. CGRect frame = self.view.frame;
  257. [UIView beginAnimations:nil context:nil];
  258. [UIView setAnimationDuration:kbDuration];
  259. frame.origin.y = -200;
  260. self.view.frame = frame;
  261. [UIView commitAnimations];
  262. }
  263. needAnimateForm = NO;
  264. isUpKeyboard = YES;
  265. }
  266. //-----------------------------------------------------------------------------------------------------------------------
  267. - (void)keyboardWillHide:(NSNotification *)notification
  268. {
  269. NSDictionary* info = [notification userInfo];
  270. double kbDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  271.  
  272. CGRect frame = self.view.frame;
  273. [UIView beginAnimations:nil context:nil];
  274. [UIView setAnimationDuration:kbDuration];
  275. frame.origin.y = 0;
  276. self.view.frame = frame;
  277. [UIView commitAnimations];
  278. isUpKeyboard = NO;
  279. }
  280. //--------------------------------------------------------------------------------------------------------------
  281. #pragma mark -
  282. #pragma mark Actions
  283. //--------------------------------------------------------------------------------------------------------------
  284. -(IBAction)cancel
  285. {
  286. id delegate = [self delegate];
  287. if ([delegate respondsToSelector:@selector(uploadToFacebookCanceled)]) {
  288. [delegate uploadToFacebookCanceled];
  289. }
  290. }
  291. //--------------------------------------------------------------------------------------------------------------
  292. -(IBAction)upload
  293. {
  294. if(!isLogin)
  295. {
  296. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authorization"
  297. message:@"You are not authorized to upload videos"
  298. delegate:nil
  299. cancelButtonTitle:@"OK"
  300. otherButtonTitles:nil];
  301.  
  302. [alert show];
  303. [alert release];
  304. return;
  305. }
  306.  
  307. [self uploadVideo];
  308. }
  309. //--------------------------------------------------------------------------------------------------------------
  310. -(IBAction)doneInputDesc
  311. {
  312. [txtvDescription resignFirstResponder];
  313. }
  314. //--------------------------------------------------------------------------------------------------------------
  315. -(IBAction)logInOut
  316. {
  317. if (isLogin)
  318. {
  319. [facebook logout:self];
  320. }
  321. else
  322. {
  323. [self logIn];
  324. }
  325. }
  326. //--------------------------------------------------------------------------------------------------------------
  327.  
  328. @end
Add Comment
Please, Sign In to add comment