123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- /****************************************************************************
- Copyright (c) 2014 Chukong Technologies Inc.
-
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #import "ShareFacebook.h"
- #import "ShareWrapper.h"
- #import <FacebookSDK/FacebookSDK.h>
- #import "ParseUtils.h"
- #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__);
- @implementation ShareFacebook
- @synthesize mShareInfo;
- @synthesize debug = __debug;
- /**
- * A function for parsing URL parameters.
- */
- - (NSDictionary*)parseURLParams:(NSString *)query {
- NSArray *pairs = [query componentsSeparatedByString:@"&"];
- NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
- for (NSString *pair in pairs) {
- NSArray *kv = [pair componentsSeparatedByString:@"="];
- NSString *val =
- [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSString *key = [kv[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- params[key] = val;
- }
- return params;
- }
- /**
- * shareInfo parameters support both AnySDK style and facebook style
- * 1. AnySDK style
- * - title
- * - site
- * - siteUrl
- * - text
- * - imageUrl
- * - imagePath
- *
- * 2. Facebook style
- * - caption
- * - name
- * - link
- * - description
- * - picture
- */
- - (void)convertParamsToFBParams:(NSMutableDictionary*) shareInfo {
- // Link type share info
- NSString *link = [shareInfo objectForKey:@"siteUrl"];
- if (!link) {
- link = [shareInfo objectForKey:@"link"];
- }
- else {
- [shareInfo setObject:link forKey:@"link"];
- }
- // Photo type share info
- NSString *photo = [shareInfo objectForKey:@"imageUrl"];
- if (!photo) {
- photo = [shareInfo objectForKey:@"imagePath"];
- }
- if (!photo) {
- photo = [shareInfo objectForKey:@"photo"];
- }
- else {
- [shareInfo setObject:photo forKey:@"photo"];
- [shareInfo setObject:photo forKey:@"picture"];
- }
-
- // Title
- NSString *caption = [shareInfo objectForKey:@"title"];
- if (!caption) {
- link = [shareInfo objectForKey:@"caption"];
- }
- else {
- [shareInfo setObject:caption forKey:@"caption"];
- }
-
- // Site name
- NSString *name = [shareInfo objectForKey:@"site"];
- if (!name) {
- link = [shareInfo objectForKey:@"name"];
- }
- else {
- [shareInfo setObject:name forKey:@"name"];
- }
-
- // Description
- NSString *desc = [shareInfo objectForKey:@"text"];
- if (!desc) {
- link = [shareInfo objectForKey:@"description"];
- }
- else {
- [shareInfo setObject:desc forKey:@"description"];
- }
- }
- - (void) configDeveloperInfo : (NSMutableDictionary*) cpInfo
- {
- }
- - (void) share: (NSMutableDictionary*) shareInfo
- {
- [self convertParamsToFBParams:shareInfo];
- NSString *link = [shareInfo objectForKey:@"link"];
- NSString *photo = [shareInfo objectForKey:@"photo"];
-
- if (link) {
- // Link type share info
- NSString *link = [shareInfo objectForKey:@"link"];
- NSString *caption = [shareInfo objectForKey:@"caption"];
- NSString *name = [shareInfo objectForKey:@"name"];
- NSString *desc = [shareInfo objectForKey:@"description"];
- NSString *photo = [shareInfo objectForKey:@"picture"];
-
- FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
- name:name
- caption:caption
- description:desc
- picture:[NSURL URLWithString:photo]];
-
- // If the Facebook app is installed and we can present the share dialog
- if ([FBDialogs canPresentShareDialogWithParams:params]) {
- [self shareLinkDialogFB:params];
- } else {
- // Fallback to web feed dialog
- [self feedDialogWeb:shareInfo];
- }
- }
- else if (photo) {
- NSURL *photoUrl = [NSURL URLWithString:[shareInfo objectForKey:@"photo"]];
- UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
- FBPhotoParams *params = [[FBPhotoParams alloc] init];
- params.photos = @[img];
-
- // If the Facebook app is installed and we can present the share dialog
- if ([FBDialogs canPresentShareDialogWithPhotos]) {
- [self sharePhotoDialogFB:params];
- } else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo sharing dialog" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, share target absent or not supported, please add 'siteUrl' or 'imageUrl' in parameters" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- - (void) setDebugMode: (BOOL) debug
- {
- self.debug = debug;
- }
- - (NSString*) getSDKVersion
- {
- return [FBSettings sdkVersion];
- }
- - (void) setSDKVersion: (NSString *)sdkVersion{
- [FBSettings setSDKVersion:sdkVersion];
- }
- - (NSString*) getPluginVersion
- {
- return @"0.2.0";
- }
- - (void) dialog: (NSMutableDictionary*) shareInfo
- {
- [self convertParamsToFBParams:shareInfo];
- NSString *dialog_type = [shareInfo objectForKey:@"dialog"];
-
- bool not_supported = false;
-
- if ([dialog_type hasSuffix:@"Link"]) {
- // Link type share info
- NSString *link = [shareInfo objectForKey:@"link"];
- NSString *caption = [shareInfo objectForKey:@"caption"];
- NSString *name = [shareInfo objectForKey:@"name"];
- NSString *desc = [shareInfo objectForKey:@"description"];
- NSString *photo = [shareInfo objectForKey:@"picture"];
-
- FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
- name:name
- caption:caption
- description:desc
- picture:[NSURL URLWithString:photo]];
-
- // Additional properties
- NSString *place = [shareInfo objectForKey:@"place"];
- if (place) {
- params.place = place;
- }
- NSString *ref = [shareInfo objectForKey:@"reference"];
- if (place) {
- params.ref = ref;
- }
- NSString *to = [shareInfo objectForKey:@"to"];
- if(to){
- NSArray *friends = [to componentsSeparatedByString:@","];
- params.friends = friends;
- }
- if ([dialog_type isEqualToString:@"shareLink"]) {
- // If the Facebook app is installed and we can present the share dialog
- if ([FBDialogs canPresentShareDialogWithParams:params]) {
- [self shareLinkDialogFB:params];
- } else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the link sharing dialog, Facebook app is needed on target device" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else if ([dialog_type isEqualToString:@"messageLink"]) {
- if ([FBDialogs canPresentMessageDialogWithParams:params]) {
- [self messageLinkDialogFB:params];
- }
- else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the link message dialog, Facebook Messenger is needed on target device" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else {
- not_supported = true;
- }
- }
- else if ([dialog_type hasSuffix:@"OpenGraph"]) {
- NSString *type = [shareInfo objectForKey:@"action_type"];
- NSString *previewProperty = [shareInfo objectForKey:@"preview_property_name"];
- NSString *title = [shareInfo objectForKey:@"title"];
- NSString *image = [shareInfo objectForKey:@"image"];
- NSString *link = [shareInfo objectForKey:@"link"];
- NSString *desc = [shareInfo objectForKey:@"description"];
-
- id<FBGraphObject> object = [FBGraphObject openGraphObjectForPostWithType: type
- title: title
- image: image
- url: [NSURL URLWithString:link]
- description: desc];
- id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
- [action setObject:object forKey:previewProperty];
- FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] initWithAction:action
- actionType:type
- previewPropertyName:previewProperty];
-
- if ([dialog_type isEqualToString:@"shareOpenGraph"]) {
- if ([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) {
- [self shareOpenGraphDialogFB:params];
- } else {
- 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"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else if ([dialog_type isEqualToString:@"messageOpenGraph"]) {
- if ([FBDialogs canPresentMessageDialogWithOpenGraphActionParams:params]) {
- [self messageOpenGraphDialogFB:params];
- } else {
- 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"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else {
- not_supported = true;
- }
- }
- else if ([dialog_type hasSuffix:@"Photo"]) {
- UIImage *img = [[UIImage alloc] initWithContentsOfFile:[shareInfo objectForKey:@"photo"]];
- if(img ==nil){
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, photo can't be found" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- return;
- }
- FBPhotoParams *params = [[FBPhotoParams alloc] init];
- params.photos = @[img];
-
- if ([dialog_type isEqualToString:@"sharePhoto"]) {
- if ([FBDialogs canPresentShareDialogWithPhotos]) {
- [self sharePhotoDialogFB:params];
- } else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo sharing dialog, Facebook app is needed on target device" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else if ([dialog_type isEqualToString:@"messagePhoto"]) {
- if ([FBDialogs canPresentMessageDialogWithPhotos]) {
- [self messagePhotoDialogFB:params];
- } else {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, facebook sdk cannot present the photo message dialog, Facebook Messenger is needed on target device" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- else {
- not_supported = true;
- }
- }
- else if ([dialog_type isEqualToString:@"feedDialog"]) {
- [self feedDialogWeb:shareInfo];
- }
- else {
- not_supported = true;
- }
-
- if (not_supported) {
- NSString *error = [NSString stringWithFormat:@"Share failed, dialog not supported: %@", dialog_type];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:error andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- }
- }
- -(BOOL) canPresentDialogWithParams:(NSMutableDictionary *)shareInfo{
- [self convertParamsToFBParams:shareInfo];
- NSString *dialog_type = [shareInfo objectForKey:@"dialog"];
-
- if ([dialog_type hasSuffix:@"Link"]) {
- NSString *link = [shareInfo objectForKey:@"link"];
- NSString *caption = [shareInfo objectForKey:@"caption"];
- NSString *name = [shareInfo objectForKey:@"name"];
- NSString *desc = [shareInfo objectForKey:@"description"];
- NSString *photo = [shareInfo objectForKey:@"picture"];
-
- FBLinkShareParams *params = [[FBLinkShareParams alloc] initWithLink:[NSURL URLWithString:link]
- name:name
- caption:caption
- description:desc
- picture:[NSURL URLWithString:photo]];
-
- // Additional properties
- NSString *place = [shareInfo objectForKey:@"place"];
- if (place) {
- params.place = place;
- }
- NSString *ref = [shareInfo objectForKey:@"reference"];
- if (place) {
- params.ref = ref;
- }
-
- if ([dialog_type isEqualToString:@"shareLink"]) {
- // If the Facebook app is installed and we can present the share dialog
- return [FBDialogs canPresentShareDialogWithParams:params];
- }
- else if ([dialog_type isEqualToString:@"messageLink"]) {
- return [FBDialogs canPresentMessageDialogWithParams:params];
- }
- }
- else if ([dialog_type hasSuffix:@"OpenGraph"]) {
- NSString *type = [shareInfo objectForKey:@"action_type"];
- NSString *previewProperty = [shareInfo objectForKey:@"preview_property_name"];
- NSString *title = [shareInfo objectForKey:@"title"];
- NSString *image = [shareInfo objectForKey:@"image"];
- NSString *link = [shareInfo objectForKey:@"url"];
- NSString *desc = [shareInfo objectForKey:@"description"];
-
- id<FBGraphObject> object = [FBGraphObject openGraphObjectForPostWithType: type
- title: title
- image: image
- url: [NSURL URLWithString:link]
- description: desc];
- id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
- [action setObject:object forKey:previewProperty];
- FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] initWithAction:action
- actionType:type
- previewPropertyName:previewProperty];
-
- if ([dialog_type isEqualToString:@"shareOpenGraph"]) {
- return [FBDialogs canPresentShareDialogWithOpenGraphActionParams:params];
- }
- else if ([dialog_type isEqualToString:@"messageOpenGraph"]) {
- return [FBDialogs canPresentMessageDialogWithOpenGraphActionParams:params];
- }
- }
- else if ([dialog_type hasSuffix:@"Photo"]) {
- UIImage *img = [[UIImage alloc] initWithContentsOfFile:[shareInfo objectForKey:@"photo"]];
- if(img ==nil){
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"Share failed, photo can't be found" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- return false;
- }
- FBPhotoParams *params = [[FBPhotoParams alloc] init];
- params.photos = @[img];
-
- if ([dialog_type isEqualToString:@"sharePhoto"]) {
- return [FBDialogs canPresentShareDialogWithPhotos];
- }
- else if ([dialog_type isEqualToString:@"messagePhoto"]) {
- return [FBDialogs canPresentMessageDialogWithPhotos];
- }
- }
- return false;
- }
- -(void) showDialog:(NSString *) type widthInfo:(NSMutableDictionary *)shareInfo{
- [FBWebDialogs presentDialogModallyWithSession:[FBSession activeSession] dialog:type parameters:shareInfo handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
- if (error) {
- // Error launching the dialog or publishing a story.
- NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if (result == FBWebDialogResultDialogNotCompleted) {
- // User clicked the "x" icon
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled sharing" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- // Handle the publish feed callback
- NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
- if([urlParams objectForKey:@"error_code"] != nil){
- NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
- return;
- }
- if (![urlParams valueForKey:@"post_id"]) {
- // User clicked the Cancel button
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled sharing" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- // User clicked the Share button
- NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams valueForKey:@"post_id"] andKey:@"post_id"];
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }
- }];
- }
- - (void) shareLinkDialogFB: (FBLinkShareParams*) params
- {
- // Present the share dialog
- [FBDialogs presentShareDialogWithParams: params
- clientState: nil
- handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:error.description andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:(int)error.code withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"postId"]!=nil){
- NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:results];
- [mdic removeObjectForKey:@"postId"];
- [mdic setObject:[results objectForKey:@"postId"] forKey:@"post_id"];
- msg = [ParseUtils NSDictionaryToNSString:mdic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString: results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- - (void) shareOpenGraphDialogFB: (FBOpenGraphActionParams*) params
- {
- [FBDialogs
- presentShareDialogWithOpenGraphActionParams: params
- clientState: nil
- handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"postId"]!=nil){
- NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithDictionary:results];
- [mdic removeObjectForKey:@"postId"];
- [mdic setObject:[results objectForKey:@"postId"] forKey:@"post_id"];
- msg = [ParseUtils NSDictionaryToNSString:mdic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString: results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- - (void) sharePhotoDialogFB: (FBPhotoParams*) params
- {
- // Present the share dialog
- [FBDialogs presentShareDialogWithPhotoParams:params
- clientState:nil
- handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *msg = [ParseUtils MakeJsonStringWithObject:error.description andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"didComplete"]){
- NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
- msg = [ParseUtils NSDictionaryToNSString:dic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString:results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- - (void) messageLinkDialogFB: (FBLinkShareParams*) params
- {
- // Present the share dialog
- [FBDialogs presentMessageDialogWithParams: params
- clientState: nil
- handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"didComplete"]){
- NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
- msg = [ParseUtils NSDictionaryToNSString:dic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString:results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- - (void) messageOpenGraphDialogFB: (FBOpenGraphActionParams*) params
- {
- [FBDialogs
- presentMessageDialogWithOpenGraphActionParams:params
- clientState:nil
- handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"didComplete"]){
- NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
- msg = [ParseUtils NSDictionaryToNSString:dic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString:results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- - (void) messagePhotoDialogFB: (FBPhotoParams*) params
- {
- // Present the share dialog
- [FBDialogs presentMessageDialogWithPhotoParams:params
- clientState:nil
- handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
- if(error) {
- NSString *errorMsg = [NSString stringWithFormat:@"Failed to send message: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if([self checkDialogResult:results]){
- NSString *msg = nil;
- if([results objectForKey:@"didComplete"]){
- NSDictionary *dic = [NSDictionary dictionaryWithObject:[results objectForKey:@"didComplete"] forKey:@"didComplete"];
- msg = [ParseUtils NSDictionaryToNSString:dic];
- }else{
- msg = [ParseUtils NSDictionaryToNSString:results];
- }
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }];
- }
- -(BOOL) checkDialogResult:(NSDictionary *)results{
- if([results valueForKey:@"completionGesture"]!=nil &&![[results valueForKey:@"completionGesture"] isEqualToString:@"cancel"]){
- return true;
- }else{
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareCancel withMsg:msg];
- return false;
- }
- }
- - (void) feedDialogWeb: (NSDictionary*) params
- {
- [FBWebDialogs
- presentFeedDialogModallyWithSession:[FBSession activeSession]
- parameters:params
- handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
- if (error) {
- // Error launching the dialog or publishing a story.
- NSString *errorMsg = [NSString stringWithFormat:@"Share failed: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if (result == FBWebDialogResultDialogNotCompleted) {
- // User clicked the "x" icon
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"User canceled sharing"];
- } else {
- // Handle the publish feed callback
- NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
- if([urlParams objectForKey:@"error_code"] != nil){
- NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
- return;
- }
- if (![urlParams valueForKey:@"post_id"]) {
- // User clicked the Cancel button
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"User canceled sharing"];
- } else {
- // User clicked the Share button
- NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams valueForKey:@"post_id"] andKey:@"post_id"];
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }
- }];
- }
- - (void) appRequest: (NSMutableDictionary*) shareInfo
- {
- NSString *message = [shareInfo objectForKey:@"message"];
- NSString *title = [shareInfo objectForKey:@"title"];
- NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
- for(NSString *key in shareInfo){
- NSString *item = [shareInfo objectForKey:key];
- if(![@"message" isEqualToString:key]&& ![@"title" isEqualToString:key]){
- [param setObject:item forKey:key];
- }
- }
-
- // Display the requests dialog
- [FBWebDialogs
- presentRequestsDialogModallyWithSession:[FBSession activeSession]
- message: message
- title: title
- parameters: param
- handler: ^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
- if (error) {
- // Error launching the dialog or sending the request.
- NSString *errorMsg = [NSString stringWithFormat:@"Sending request failed: %@", error.description];
- NSString *msg = [ParseUtils MakeJsonStringWithObject:errorMsg andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- if (result == FBWebDialogResultDialogNotCompleted) {
- // User clicked the "x" icon
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:kShareFail withMsg:msg];
- } else {
- // Handle the send request callback
- NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
- NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- if([urlParams objectForKey:@"error_code"] != nil){
- NSString *msg = [ParseUtils MakeJsonStringWithObject:[urlParams objectForKey:@"error_message"] andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:(int)[urlParams objectForKey:@"error_code"] withMsg:msg];
- return;
- }
- if([urlParams count]>0){
- int i = 0;
- NSMutableArray *resultArray = [[NSMutableArray alloc] init];
- while ([urlParams objectForKey:[NSString stringWithFormat:@"to[%d]",i]]!=nil) {
- resultArray[i] = [urlParams objectForKey:[NSString stringWithFormat:@"to[%d]",i]];
- ++i;
- }
- if([urlParams valueForKey:@"request"]){
- [dic setObject:[urlParams objectForKey:@"request"] forKey:@"request"];
- }
- if([resultArray count]>0){
- [dic setObject:resultArray forKey:@"to"];
- }
- }
- if (![urlParams valueForKey:@"request"]) {
- // User clicked the Cancel button
- NSString *msg = [ParseUtils MakeJsonStringWithObject:@"User canceled request" andKey:@"error_message"];
- [ShareWrapper onShareResult:self withRet:(int)error.code withMsg:msg];
- } else {
- // User clicked the Send button
- NSString *msg = [ParseUtils NSDictionaryToNSString:dic];
- [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:msg];
- }
- }
- }
- }];
- }
- @end
|