0

I'm building an article reading app for iPhone. I am stuck in some issues.I'm facing a problem in social sharing. I have tried couple of approaches. First Approach

We are using Facebook sdk to share the article link,title and image. Ideally, when we click on Facebook activity item in uiactivityviewcontroller, my Facebook sdk code should run but I am unable to do so.

Second approach

We tried for custom activity item in uiactivityviewcontroller and were successful in making a custom icon and give action to that which is a default method of class uiactivity(- (void) performActivity), but I want to use this method in my view controller class where we pass article link,title and image by segue.

here is my code:

           #import "ysAPActivityIcon.h"
           #import <Social/Social.h>
           #import <FacebookSDK/FacebookSDK.h>
           @implementation ysAPActivityIcon

            - (NSString *)activityType { return @"it.albertopasca.myApp"; }
            - (NSString *)activityTitle { return @"Open Maps"; }
             - (UIImage *) _activityImage { return [UIImage imageNamed:@"iPad_jobs.png"];           }
             - (BOOL) canPerformWithActivityItems:(NSArray *)activityItems { return YES; }
             - (void) prepareWithActivityItems:(NSArray *)activityItems { }
              - (UIViewController *) activityViewController { return nil; }
              - (void) performActivity {
                         }
          // my view controller class:
            #import "ysAPActivityProvider.h"
            #import "ysAPActivityIcon.h"
              #import "ysDetailViewController.h"
             @interface ysDetailViewController() <UIWebViewDelegate,UIActivityItemSource>
           - (void) performActivity {

           FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
         params.link = [NSURL URLWithString:@"https://www.youtube.com/watch?v=pa8lsBNG31c"];
        //    // If the Facebook app is installed and we can present the share dialog
         if ([FBDialogs canPresentShareDialogWithParams:params]) {

          // Present share dialog
          [FBDialogs presentShareDialogWithLink:params.link
                               handler:^(FBAppCall *call, NSDictionary *results,    NSError *error) {
                                  if(error) {
                                      // An error occurred, we need to handle the error
                                       // See: https://developers.facebook.com/docs/ios/errors
                                      NSLog(@"Error publishing story: %@", error.description);
                                  } else {
                                      // Success
                                      NSLog(@"result %@", results);
                                  }
                              }];

// If the Facebook app is NOT installed and we can't present the share dialog
            } else {
       // FALLBACK: publish just a link using the Feed dialog

// Put together the dialog parameters
       NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"YourStory", @"name",

                               nil];

// Show the feed dialog
           [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                       parameters:params
                                          handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                              if (error) {
                                                  // An error occurred, we need to handle the error
                                                  // See:  https://developers.facebook.com/docs/ios/errors
                                                  NSLog(@"Error publishing story: %@", error.description);
                                              } else {
                                                  if (result ==   FBWebDialogResultDialogNotCompleted) {
                                                      // User canceled.
                                                      NSLog(@"User cancelled.");
                                                  } else {
                                                      // Handle the publish feed callback
                                                      //   NSDictionary *urlParams = [self parseURLParams:[resultURL query]];

                                                      //  if (![urlParams valueForKey:@"post_id"]) {
                                                      // User canceled.
                                                      NSLog(@"User cancelled.");

                                                      //    } else {
                                                      // User clicked the Share button
                                                      //         NSString *result =      [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];
                                                      //        NSLog(@"result %@", result);
                                                  }
                                              }

                                          }];

           }



      }
Daljeet
  • 1,573
  • 2
  • 20
  • 40

1 Answers1

0

In the .h file of your custom UIActivity create the variables you need for title, link, image ... Import the header file of the UIActivity in the implementation file of your ViewController and in the method of action of the button create an instance of the class. Let's say the UIActivity header file is called customAct:

customAct *act = [[customAct alloc] init];

Then use it to add data to variables in the header file:

act.titleString = @"Text you want to add"

And then use those variables in performActivity method.

But if I understood well, you are trying to share to Facebook. UIActivityController gives you by default the ability to share on facebook, twitter, mail, message. Neverless to make facebook sharing button appear you have to login in the settings to your facebook account, same thing for twitter. On the other side, UIActivtyController doesn't provide the ability to share through whatsapp, instagram, ... for that you'll have to make custom activities

Georgio Sayegh
  • 343
  • 1
  • 15