ShareFacebook.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /****************************************************************************
  2. Copyright (c) 2014 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #import "ShareFacebook.h"
  21. #import "ShareWrapper.h"
  22. #import <FacebookSDK/FacebookSDK.h>
  23. #import "ParseUtils.h"
  24. #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__);
  25. @implementation ShareFacebook
  26. @synthesize mShareInfo;
  27. @synthesize debug = __debug;
  28. /**
  29. * A function for parsing URL parameters.
  30. */
  31. - (NSDictionary*)parseURLParams:(NSString *)query {
  32. NSArray *pairs = [query componentsSeparatedByString:@"&"];
  33. NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  34. for (NSString *pair in pairs) {
  35. NSArray *kv = [pair componentsSeparatedByString:@"="];
  36. NSString *val =
  37. [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  38. NSString *key = [kv[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  39. params[key] = val;
  40. }
  41. return params;
  42. }
  43. /**
  44. * shareInfo parameters support both AnySDK style and facebook style
  45. * 1. AnySDK style
  46. * - title
  47. * - site
  48. * - siteUrl
  49. * - text
  50. * - imageUrl
  51. * - imagePath
  52. *
  53. * 2. Facebook style
  54. * - caption
  55. * - name
  56. * - link
  57. * - description
  58. * - picture
  59. */
  60. - (void)convertParamsToFBParams:(NSMutableDictionary*) shareInfo {
  61. // Link type share info
  62. NSString *link = [shareInfo objectForKey:@"siteUrl"];
  63. if (!link) {
  64. link = [shareInfo objectForKey:@"link"];
  65. }
  66. else {
  67. [shareInfo setObject:link forKey:@"link"];
  68. }
  69. // Photo type share info
  70. NSString *photo = [shareInfo objectForKey:@"imageUrl"];
  71. if (!photo) {
  72. photo = [shareInfo objectForKey:@"imagePath"];
  73. }
  74. if (!photo) {
  75. photo = [shareInfo objectForKey:@"photo"];
  76. }
  77. else {
  78. [shareInfo setObject:photo forKey:@"photo"];
  79. [shareInfo setObject:photo forKey:@"picture"];
  80. }
  81. // Title
  82. NSString *caption = [shareInfo objectForKey:@"title"];
  83. if (!caption) {
  84. link = [shareInfo objectForKey:@"caption"];
  85. }
  86. else {
  87. [shareInfo setObject:caption forKey:@"caption"];
  88. }
  89. // Site name
  90. NSString *name = [shareInfo objectForKey:@"site"];
  91. if (!name) {
  92. link = [shareInfo objectForKey:@"name"];
  93. }
  94. else {
  95. [shareInfo setObject:name forKey:@"name"];
  96. }
  97. // Description
  98. NSString *desc = [shareInfo objectForKey:@"text"];
  99. if (!desc) {
  100. link = [shareInfo objectForKey:@"description"];
  101. }
  102. else {
  103. [shareInfo setObject:desc forKey:@"description"];
  104. }
  105. }
  106. - (void) configDeveloperInfo : (NSMutableDictionary*) cpInfo
  107. {
  108. }
  109. - (void) share: (NSMutableDictionary*) shareInfo
  110. {
  111. [self convertParamsToFBParams:shareInfo];
  112. NSString *link = [shareInfo objectForKey:@"link"];
  113. NSString *photo = [shareInfo objectForKey:@"photo"];
  114. if (link) {
  115. // Link type share info
  116. NSString *link = [shareInfo objectForKey:@"link"];
  117. NSString *caption = [shareInfo objectForKey:@"caption"];
  118. NSString *name = [shareInfo objectForKey:@"name"];
  119. NSString *desc = [shareInfo objectForKey:@"description"];
  120. NSString *photo = [shareInfo objectForKey:@"picture"];
  121. FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
  122. name:name
  123. caption:caption
  124. description:desc
  125. picture:[NSURL URLWithString:photo]];
  126. // If the Facebook app is installed and we can present the share dialog
  127. if ([FBDialogs canPresentShareDialogWithParams:params]) {
  128. [self shareLinkDialogFB:params];
  129. } else {
  130. // Fallback to web feed dialog
  131. [self feedDialogWeb:shareInfo];
  132. }
  133. }
  134. else if (photo) {
  135. NSURL *photoUrl = [NSURL URLWithString:[shareInfo objectForKey:@"photo"]];
  136. UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
  137. FBPhotoParams *params = [[FBPhotoParams alloc] init];
  138. params.photos = @[img];
  139. // If the Facebook app is installed and we can present the share dialog
  140. if ([FBDialogs canPresentShareDialogWithPhotos]) {
  141. [self sharePhotoDialogFB:params];
  142. } else {
  143. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo sharing dialog" andKey:@"error_message"];
  144. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  145. }
  146. }
  147. else {
  148. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, share target absent or not supported, please add 'siteUrl' or 'imageUrl' in parameters" andKey:@"error_message"];
  149. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  150. }
  151. }
  152. - (void) setDebugMode: (BOOL) debug
  153. {
  154. self.debug = debug;
  155. }
  156. - (NSString*) getSDKVersion
  157. {
  158. return [FBSettings sdkVersion];
  159. }
  160. - (void) setSDKVersion: (NSString *)sdkVersion{
  161. [FBSettings setSDKVersion:sdkVersion];
  162. }
  163. - (NSString*) getPluginVersion
  164. {
  165. return @"0.2.0";
  166. }
  167. - (void) dialog: (NSMutableDictionary*) shareInfo
  168. {
  169. [self convertParamsToFBParams:shareInfo];
  170. NSString *dialog_type = [shareInfo objectForKey:@"dialog"];
  171. bool not_supported = false;
  172. if ([dialog_type hasSuffix:@"Link"]) {
  173. // Link type share info
  174. NSString *link = [shareInfo objectForKey:@"link"];
  175. NSString *caption = [shareInfo objectForKey:@"caption"];
  176. NSString *name = [shareInfo objectForKey:@"name"];
  177. NSString *desc = [shareInfo objectForKey:@"description"];
  178. NSString *photo = [shareInfo objectForKey:@"picture"];
  179. FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
  180. name:name
  181. caption:caption
  182. description:desc
  183. picture:[NSURL URLWithString:photo]];
  184. // Additional properties
  185. NSString *place = [shareInfo objectForKey:@"place"];
  186. if (place) {
  187. params.place = place;
  188. }
  189. NSString *ref = [shareInfo objectForKey:@"reference"];
  190. if (place) {
  191. params.ref = ref;
  192. }
  193. NSString *to = [shareInfo objectForKey:@"to"];
  194. if(to){
  195. NSArray *friends = [to componentsSeparatedByString:@","];
  196. params.friends = friends;
  197. }
  198. if ([dialog_type isEqualToString:@"shareLink"]) {
  199. // If the Facebook app is installed and we can present the share dialog
  200. if ([FBDialogs canPresentShareDialogWithParams:params]) {
  201. [self shareLinkDialogFB:params];
  202. } else {
  203. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the link sharing dialog, Facebook app is needed on target device" andKey:@"error_message"];
  204. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  205. }
  206. }
  207. else if ([dialog_type isEqualToString:@"messageLink"]) {
  208. if ([FBDialogs canPresentMessageDialogWithParams:params]) {
  209. [self messageLinkDialogFB:params];
  210. }
  211. else {
  212. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the link message dialog, Facebook Messenger is needed on target device" andKey:@"error_message"];
  213. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  214. }
  215. }
  216. else {
  217. not_supported = true;
  218. }
  219. }
  220. else if ([dialog_type hasSuffix:@"OpenGraph"]) {
  221. NSString *type = [shareInfo objectForKey:@"action_type"];
  222. NSString *previewProperty = [shareInfo objectForKey:@"preview_property_name"];
  223. NSString *title = [shareInfo objectForKey:@"title"];
  224. NSString *image = [shareInfo objectForKey:@"image"];
  225. NSString *link = [shareInfo objectForKey:@"link"];
  226. NSString *desc = [shareInfo objectForKey:@"description"];
  227. id<FBGraphObject> object = [FBGraphObject openGraphObjectForPostWithType: type
  228. title: title
  229. image: image
  230. url: [NSURL URLWithString:link]
  231. description: desc];
  232. id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
  233. [action setObject:object forKey:previewProperty];
  234. FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] initWithAction:action
  235. actionType:type
  236. previewPropertyName:previewProperty];
  237. if ([dialog_type isEqualToString:@"shareOpenGraph"]) {
  238. if ([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) {
  239. [self shareOpenGraphDialogFB:params];
  240. } else {
  241. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the open graph sharing dialog, Facebook app is needed on target device" andKey:@"error_message"];
  242. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  243. }
  244. }
  245. else if ([dialog_type isEqualToString:@"messageOpenGraph"]) {
  246. if ([FBDialogs canPresentMessageDialogWithOpenGraphActionParams:params]) {
  247. [self messageOpenGraphDialogFB:params];
  248. } else {
  249. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the open graph message dialog, Facebook Messenger is needed on target device" andKey:@"error_message"];
  250. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  251. }
  252. }
  253. else {
  254. not_supported = true;
  255. }
  256. }
  257. else if ([dialog_type hasSuffix:@"Photo"]) {
  258. UIImage *img = [[UIImage alloc] initWithContentsOfFile:[shareInfo objectForKey:@"photo"]];
  259. if(img ==nil){
  260. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, photo can't be found" andKey:@"error_message"];
  261. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  262. return;
  263. }
  264. FBPhotoParams *params = [[FBPhotoParams alloc] init];
  265. params.photos = @[img];
  266. if ([dialog_type isEqualToString:@"sharePhoto"]) {
  267. if ([FBDialogs canPresentShareDialogWithPhotos]) {
  268. [self sharePhotoDialogFB:params];
  269. } else {
  270. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo sharing dialog, Facebook app is needed on target device" andKey:@"error_message"];
  271. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  272. }
  273. }
  274. else if ([dialog_type isEqualToString:@"messagePhoto"]) {
  275. if ([FBDialogs canPresentMessageDialogWithPhotos]) {
  276. [self messagePhotoDialogFB:params];
  277. } else {
  278. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo message dialog, Facebook Messenger is needed on target device" andKey:@"error_message"];
  279. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  280. }
  281. }
  282. else {
  283. not_supported = true;
  284. }
  285. }
  286. else if ([dialog_type isEqualToString:@"feedDialog"]) {
  287. [self feedDialogWeb:shareInfo];
  288. }
  289. else {
  290. not_supported = true;
  291. }
  292. if (not_supported) {
  293. NSString *error = [NSString stringWithFormat:@"Share failed, dialog not supported: %@", dialog_type];
  294. NSString *msg = [ParseUtils MakeJsonStringWithObject:error andKey:@"error_message"];
  295. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  296. }
  297. }
  298. -(BOOL) canPresentDialogWithParams:(NSMutableDictionary *)shareInfo{
  299. [self convertParamsToFBParams:shareInfo];
  300. NSString *dialog_type = [shareInfo objectForKey:@"dialog"];
  301. if ([dialog_type hasSuffix:@"Link"]) {
  302. NSString *link = [shareInfo objectForKey:@"link"];
  303. NSString *caption = [shareInfo objectForKey:@"caption"];
  304. NSString *name = [shareInfo objectForKey:@"name"];
  305. NSString *desc = [shareInfo objectForKey:@"description"];
  306. NSString *photo = [shareInfo objectForKey:@"picture"];
  307. FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
  308. name:name
  309. caption:caption
  310. description:desc
  311. picture:[NSURL URLWithString:photo]];
  312. // Additional properties
  313. NSString *place = [shareInfo objectForKey:@"place"];
  314. if (place) {
  315. params.place = place;
  316. }
  317. NSString *ref = [shareInfo objectForKey:@"reference"];
  318. if (place) {
  319. params.ref = ref;
  320. }
  321. if ([dialog_type isEqualToString:@"shareLink"]) {
  322. // If the Facebook app is installed and we can present the share dialog
  323. return [FBDialogs canPresentShareDialogWithParams:params];
  324. }
  325. else if ([dialog_type isEqualToString:@"messageLink"]) {
  326. return [FBDialogs canPresentMessageDialogWithParams:params];
  327. }
  328. }
  329. else if ([dialog_type hasSuffix:@"OpenGraph"]) {
  330. NSString *type = [shareInfo objectForKey:@"action_type"];
  331. NSString *previewProperty = [shareInfo objectForKey:@"preview_property_name"];
  332. NSString *title = [shareInfo objectForKey:@"title"];
  333. NSString *image = [shareInfo objectForKey:@"image"];
  334. NSString *link = [shareInfo objectForKey:@"url"];
  335. NSString *desc = [shareInfo objectForKey:@"description"];
  336. id<FBGraphObject> object = [FBGraphObject openGraphObjectForPostWithType: type
  337. title: title
  338. image: image
  339. url: [NSURL URLWithString:link]
  340. description: desc];
  341. id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
  342. [action setObject:object forKey:previewProperty];
  343. FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] initWithAction:action
  344. actionType:type
  345. previewPropertyName:previewProperty];
  346. if ([dialog_type isEqualToString:@"shareOpenGraph"]) {
  347. return [FBDialogs canPresentShareDialogWithOpenGraphActionParams:params];
  348. }
  349. else if ([dialog_type isEqualToString:@"messageOpenGraph"]) {
  350. return [FBDialogs canPresentMessageDialogWithOpenGraphActionParams:params];
  351. }
  352. }
  353. else if ([dialog_type hasSuffix:@"Photo"]) {
  354. UIImage *img = [[UIImage alloc] initWithContentsOfFile:[shareInfo objectForKey:@"photo"]];
  355. if(img ==nil){
  356. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, photo can't be found" andKey:@"error_message"];
  357. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  358. return false;
  359. }
  360. FBPhotoParams *params = [[FBPhotoParams alloc] init];
  361. params.photos = @[img];
  362. if ([dialog_type isEqualToString:@"sharePhoto"]) {
  363. return [FBDialogs canPresentShareDialogWithPhotos];
  364. }
  365. else if ([dialog_type isEqualToString:@"messagePhoto"]) {
  366. return [FBDialogs canPresentMessageDialogWithPhotos];
  367. }
  368. }
  369. return false;
  370. }
  371. -(void) showDialog:(NSString *) type widthInfo:(NSMutableDictionary *)shareInfo{
  372. [FBWebDialogs presentDialogModallyWithSession:[FBSession activeSession] dialog:type parameters:shareInfo handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
  373. if (error) {
  374. // Error launching the dialog or publishing a story.
  375. NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
  376. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  377. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  378. } else {
  379. if (result == FBWebDialogResultDialogNotCompleted) {
  380. // User clicked the "x" icon
  381. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled sharing" andKey:@"error_message"];
  382. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  383. } else {
  384. // Handle the publish feed callback
  385. NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
  386. if([urlParams objectForKey:@"error_code"] != nil){
  387. NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
  388. [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
  389. return;
  390. }
  391. if (![urlParams valueForKey:@"post_id"]) {
  392. // User clicked the Cancel button
  393. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled sharing" andKey:@"error_message"];
  394. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  395. } else {
  396. // User clicked the Share button
  397. NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams valueForKey:@"post_id"] andKey:@"post_id"];
  398. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  399. }
  400. }
  401. }
  402. }];
  403. }
  404. - (void) shareLinkDialogFB: (FBLinkShareParams*) params
  405. {
  406. // Present the share dialog
  407. [FBDialogs presentShareDialogWithParams: params
  408. clientState: nil
  409. handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
  410. if(error) {
  411. NSString *msg = [ParseUtils MakeJsonStringWithObject:error.description andKey:@"error_message"];
  412. [ShareWrapper onShareResult:self withRet:(int)error.code withMsg:msg];
  413. } else {
  414. if([self checkDialogResult:results]){
  415. NSString *msg = nil;
  416. if([results objectForKey:@"postId"]!=nil){
  417. NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:results];
  418. [mdic removeObjectForKey:@"postId"];
  419. [mdic setObject:[results objectForKey:@"postId"] forKey:@"post_id"];
  420. msg = [ParseUtils NSDictionaryToNSString:mdic];
  421. }else{
  422. msg = [ParseUtils NSDictionaryToNSString: results];
  423. }
  424. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  425. }
  426. }
  427. }];
  428. }
  429. - (void) shareOpenGraphDialogFB: (FBOpenGraphActionParams*) params
  430. {
  431. [FBDialogs
  432. presentShareDialogWithOpenGraphActionParams: params
  433. clientState: nil
  434. handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
  435. if(error) {
  436. NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
  437. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  438. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  439. } else {
  440. if([self checkDialogResult:results]){
  441. NSString *msg = nil;
  442. if([results objectForKey:@"postId"]!=nil){
  443. NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:results];
  444. [mdic removeObjectForKey:@"postId"];
  445. [mdic setObject:[results objectForKey:@"postId"] forKey:@"post_id"];
  446. msg = [ParseUtils NSDictionaryToNSString:mdic];
  447. }else{
  448. msg = [ParseUtils NSDictionaryToNSString: results];
  449. }
  450. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  451. }
  452. }
  453. }];
  454. }
  455. - (void) sharePhotoDialogFB: (FBPhotoParams*) params
  456. {
  457. // Present the share dialog
  458. [FBDialogs presentShareDialogWithPhotoParams:params
  459. clientState:nil
  460. handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
  461. if(error) {
  462. NSString *msg = [ParseUtils MakeJsonStringWithObject:error.description andKey:@"error_message"];
  463. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  464. } else {
  465. if([self checkDialogResult:results]){
  466. NSString *msg = nil;
  467. if([results objectForKey:@"didComplete"]){
  468. NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
  469. msg = [ParseUtils NSDictionaryToNSString:dic];
  470. }else{
  471. msg = [ParseUtils NSDictionaryToNSString:results];
  472. }
  473. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  474. }
  475. }
  476. }];
  477. }
  478. - (void) messageLinkDialogFB: (FBLinkShareParams*) params
  479. {
  480. // Present the share dialog
  481. [FBDialogs presentMessageDialogWithParams: params
  482. clientState: nil
  483. handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
  484. if(error) {
  485. NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
  486. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  487. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  488. } else {
  489. if([self checkDialogResult:results]){
  490. NSString *msg = nil;
  491. if([results objectForKey:@"didComplete"]){
  492. NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
  493. msg = [ParseUtils NSDictionaryToNSString:dic];
  494. }else{
  495. msg = [ParseUtils NSDictionaryToNSString:results];
  496. }
  497. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  498. }
  499. }
  500. }];
  501. }
  502. - (void) messageOpenGraphDialogFB: (FBOpenGraphActionParams*) params
  503. {
  504. [FBDialogs
  505. presentMessageDialogWithOpenGraphActionParams:params
  506. clientState:nil
  507. handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
  508. if(error) {
  509. NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
  510. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  511. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  512. } else {
  513. if([self checkDialogResult:results]){
  514. NSString *msg = nil;
  515. if([results objectForKey:@"didComplete"]){
  516. NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
  517. msg = [ParseUtils NSDictionaryToNSString:dic];
  518. }else{
  519. msg = [ParseUtils NSDictionaryToNSString:results];
  520. }
  521. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  522. }
  523. }
  524. }];
  525. }
  526. - (void) messagePhotoDialogFB: (FBPhotoParams*) params
  527. {
  528. // Present the share dialog
  529. [FBDialogs presentMessageDialogWithPhotoParams:params
  530. clientState:nil
  531. handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
  532. if(error) {
  533. NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
  534. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  535. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  536. } else {
  537. if([self checkDialogResult:results]){
  538. NSString *msg = nil;
  539. if([results objectForKey:@"didComplete"]){
  540. NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
  541. msg = [ParseUtils NSDictionaryToNSString:dic];
  542. }else{
  543. msg = [ParseUtils NSDictionaryToNSString:results];
  544. }
  545. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  546. }
  547. }
  548. }];
  549. }
  550. -(BOOL) checkDialogResult:(NSDictionary *)results{
  551. if([results valueForKey:@"completionGesture"]!=nil &&![[results valueForKey:@"completionGesture"] isEqualToString:@"cancel"]){
  552. return true;
  553. }else{
  554. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
  555. [ShareWrapper onShareResult:self withRet:kShareCancel withMsg:msg];
  556. return false;
  557. }
  558. }
  559. - (void) feedDialogWeb: (NSDictionary*) params
  560. {
  561. [FBWebDialogs
  562. presentFeedDialogModallyWithSession:[FBSession activeSession]
  563. parameters:params
  564. handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
  565. if (error) {
  566. // Error launching the dialog or publishing a story.
  567. NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
  568. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  569. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  570. } else {
  571. if (result == FBWebDialogResultDialogNotCompleted) {
  572. // User clicked the "x" icon
  573. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"User canceled sharing"];
  574. } else {
  575. // Handle the publish feed callback
  576. NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
  577. if([urlParams objectForKey:@"error_code"] != nil){
  578. NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
  579. [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
  580. return;
  581. }
  582. if (![urlParams valueForKey:@"post_id"]) {
  583. // User clicked the Cancel button
  584. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"User canceled sharing"];
  585. } else {
  586. // User clicked the Share button
  587. NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams valueForKey:@"post_id"] andKey:@"post_id"];
  588. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  589. }
  590. }
  591. }
  592. }];
  593. }
  594. - (void) appRequest: (NSMutableDictionary*) shareInfo
  595. {
  596. NSString *message = [shareInfo objectForKey:@"message"];
  597. NSString *title = [shareInfo objectForKey:@"title"];
  598. NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
  599. for(NSString *key in shareInfo){
  600. NSString *item = [shareInfo objectForKey:key];
  601. if(![@"message" isEqualToString:key]&& ![@"title" isEqualToString:key]){
  602. [param setObject:item forKey:key];
  603. }
  604. }
  605. // Display the requests dialog
  606. [FBWebDialogs
  607. presentRequestsDialogModallyWithSession:[FBSession activeSession]
  608. message: message
  609. title: title
  610. parameters: param
  611. handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
  612. if (error) {
  613. // Error launching the dialog or sending the request.
  614. NSString *errorMsg = [NSString stringWithFormat:@"Sending request failed: %@", error.description];
  615. NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
  616. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  617. } else {
  618. if (result == FBWebDialogResultDialogNotCompleted) {
  619. // User clicked the "x" icon
  620. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
  621. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
  622. } else {
  623. // Handle the send request callback
  624. NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
  625. NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  626. if([urlParams objectForKey:@"error_code"] != nil){
  627. NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
  628. [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
  629. return;
  630. }
  631. if([urlParams count]>0){
  632. int i = 0;
  633. NSMutableArray *resultArray = [[NSMutableArray alloc] init];
  634. while ([urlParams objectForKey:[NSString stringWithFormat:@"to[%d]",i]]!=nil) {
  635. resultArray[i] = [urlParams objectForKey:[NSString stringWithFormat:@"to[%d]",i]];
  636. ++i;
  637. }
  638. if([urlParams valueForKey:@"request"]){
  639. [dic setObject:[urlParams objectForKey:@"request"] forKey:@"request"];
  640. }
  641. if([resultArray count]>0){
  642. [dic setObject:resultArray forKey:@"to"];
  643. }
  644. }
  645. if (![urlParams valueForKey:@"request"]) {
  646. // User clicked the Cancel button
  647. NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
  648. [ShareWrapper onShareResult:self withRet:(int)error.code withMsg:msg];
  649. } else {
  650. // User clicked the Send button
  651. NSString *msg = [ParseUtils NSDictionaryToNSString:dic];
  652. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
  653. }
  654. }
  655. }
  656. }];
  657. }
  658. @end