iOS Question Share to social media functionality - Help with inline objC

Discussion in 'iOS Questions' started by henrywood, Apr 13, 2015.

  1. henrywood

    henrywood Active Member Licensed User

    Hey !

    I am trying to integrate social sharing functionality in my app through Objective C.

    Could someone validate the methology - in particular the Objective C code ?

    I would like to get an event in B4i when the user has completed sharing.

    UPDATED: 09-05-2015: THIS VERSION DOES NOT WORK - PLEASE SEE POST #101 FOR A WORKING VERSION:
    http://www.b4x.com/android/forum/th...elp-with-inline-objc.52849/page-6#post-336177

    A BIG THANK YOU TO ALL WHO HELPED ME WITH THIS CODE !

    Here is my code:

    Code:
    'Code module

    Sub Process_Globals
       
    'These global variables will be declared once when the application starts.
       'Public variables can be accessed from all modules.

    End Sub


    #If OBJC

    - (IBAction)shareToSocialMedia:(NSString *)txt (NSString *)url: (NSString *)image
    {
       //NSString *text = @"How to add Facebook and Twitter sharing to an iOS app";
       NSString *text = txt;
       //NSURL *url = [NSURL URLWithString:@"http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/"];
       NSURL *url = [NSURL URLWithString:url];
       //UIImage *image = [UIImage imageNamed:@"roadfire-icon-square-200"];
       UIImage *image = [UIImage imageNamed:image];

       UIActivityViewController *controller =
       [[UIActivityViewController alloc]
       initWithActivityItems:@[text, url, image]
       applicationActivities:nil];

      controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
      UIActivityTypeMessage,
      UIActivityTypeMail,
      UIActivityTypePrint,
      UIActivityTypeCopyToPasteboard,
      UIActivityTypeAssignToContact,
      UIActivityTypeSaveToCameraRoll,
      UIActivityTypeAddToReadingList,
      UIActivityTypePostToFlickr,
      UIActivityTypePostToVimeo,
      UIActivityTypePostToTencentWeibo,
      UIActivityTypeAirDrop];

       [self presentViewController:controller animated:YES completion:^{
         [self sharedone];
       }];
    }

    - (void)sharedone:
    {
       int status = 1;
       [B4IObjectWrapper raiseEvent:self :@"onsharingdone:" :@[@((int)status)]];
       //[B4IObjectWrapper raiseEventFromDifferentThread:self :@"onsharingdone:" :@[@((int)status)]];

    }

    #end if

    Sub onSharingDone(Status As Int)
       
    Log("onSharingDone: Status = " & Status)
    End Sub

    Sub ShareToSocialMedia(txt As String, theURL As String, imageFile As String)

       
    Dim NativeMe As NativeObject = Me
       NativeMe.RunMethod(
    "shareToSocialMedia:"Array As Object(txt, theURL, imageFile))

    End Sub
     
    Last edited: May 10, 2015
  2. narek adonts

    narek adonts Well-Known Member Licensed User

    changing the completion to a void will give you a notification that the view was presented.
    you need to add this just after initializing UIActivityViewController

    [controller setCompletionHandler:^(NSString*activityType, BOOL completed) {
    [self.bi raiseEvent:nil event:mad:"activity_completed::" params:mad:[@((BOOL)completed),(activityType)]];
    }];

    and add

    Sub activity_completed(status as boolean, type as string)

    End sub

    in the module where your OBJC code is situated.

    Narek
     
    Brian Robinson likes this.
  3. henrywood

    henrywood Active Member Licensed User

    Thanks !
     
  4. henrywood

    henrywood Active Member Licensed User

    Just in case somebody else needs the sharing functionality here with Narek's changes

    Code:
    'Code module

    Sub Process_Globals
       
    'These global variables will be declared once when the application starts.
       'Public variables can be accessed from all modules.
       Private myPage As Page

    End Sub


    #If OBJC

      - (IBAction)share2SocialMedia:(NSString *)txt url:(NSString *)theurl img:(NSString *)theimg
    {
       //NSString *text = @"How to add Facebook and Twitter sharing to an iOS app";
       NSString *text = txt;
       //NSURL *url = [NSURL URLWithString:@"http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/"];
       NSURL *url = [NSURL URLWithString:theurl];
       //UIImage *image = [UIImage imageNamed:@"roadfire-icon-square-200"];

      if([img hasPrefix:@"http://"] || [img hasPrefix:@"https://"] ) {
         UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:theimg]]];
       } else {
         UIImage *image = [UIImage imageNamed:theimg];
       }

       UIActivityViewController *controller =
       [[UIActivityViewController alloc]
       initWithActivityItems:@[text, url, image]
       applicationActivities:nil];

      controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
      UIActivityTypeMessage,
      UIActivityTypeMail,
      UIActivityTypePrint,
      UIActivityTypeCopyToPasteboard,
      UIActivityTypeAssignToContact,
      UIActivityTypeSaveToCameraRoll,
      UIActivityTypeAddToReadingList,
      UIActivityTypePostToFlickr,
      UIActivityTypePostToVimeo,
      UIActivityTypePostToTencentWeibo,
      UIActivityTypeAirDrop];

       [controller setCompletionHandler:^(NSString*activityType, BOOL completed) {
       [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
       }];

       [(_myPage).object presentViewController:controller animated:YES completion:nil];
    }

    #end if

    Sub activity_completed(status As Boolean, T As String)
       
    Log("onSharingDone: Status = " & status & " - Type: " & T)
    End Sub

    Sub ShareToSocialMedia(p As Page, txt As String, theURL As String, imageFile As String)

       myPage = p
       
    Dim NativeMe As NativeObject = Me
       NativeMe.RunMethod(
    "share2SocialMedia:"Array As Object(txt, theURL, imageFile))

    End Sub
     
    Last edited: Apr 17, 2015
    cloner7801 likes this.
  5. narek adonts

    narek adonts Well-Known Member Licensed User

    replace "mad" with "@" please )))
     
    cloner7801 likes this.
  6. henrywood

    henrywood Active Member Licensed User

    Better now ?
     
  7. Pendrush

    Pendrush Well-Known Member Licensed User

    I'm unabele to compile with OBJC code.
     
    Last edited: Apr 17, 2015
  8. henrywood

    henrywood Active Member Licensed User

    Last edited: Apr 17, 2015
  9. Pendrush

    Pendrush Well-Known Member Licensed User

    Error in compile
     
    Last edited: Apr 17, 2015
  10. henrywood

    henrywood Active Member Licensed User

    Now I've edited post #4 again, but:

    1. I am also not sure if this line:

    Code:
    [self.bi raiseEvent:nil event:@"activity_completed::" params:@:[@((BOOL)completed),(activityType)]];
    2. Also I don't know if the call to presentViewController is correct - ie. this line:

    Code:
    [self presentViewController:controller animated:YES completion:nil];
    3. I am unsure about the "correctness" of this line

    Code:
    NativeMe.RunMethod("share2SocialMedia:"Array As Object(txt, theURL, imageFile))
    Maybe someone savyy in Objective C could please take a look (@Erel ?) ?
     
  11. narek adonts

    narek adonts Well-Known Member Licensed User

    this line will not work

    [self presentViewController:controller animated:YES completion:nil];


    instead of self you must add a varibal refering the page which will show the viewcontroller and then refear it in OBJC

    for example:

    Code:
    Dim myPage as Page=Page1

    [(_myPage).object presentViewController:controller animated:YES completion:nil];
     
  12. henrywood

    henrywood Active Member Licensed User

    Last edited: Apr 17, 2015
  13. Pendrush

    Pendrush Well-Known Member Licensed User

    Just tried again, now has 8 errors
     
  14. henrywood

    henrywood Active Member Licensed User

  15. henrywood

    henrywood Active Member Licensed User

  16. narek adonts

    narek adonts Well-Known Member Licensed User

    it should be

    params:mad:

    instead of

    params:mad::
     
  17. henrywood

    henrywood Active Member Licensed User

  18. narek adonts

    narek adonts Well-Known Member Licensed User

    this is the OBJC code that sould work.

    Code:
    #IF OBJC
    - (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg
    {
       //NSString *text = @"How to add Facebook and Twitter sharing to an iOS app";
       NSString *text = txt;
       //NSURL *url = [NSURL URLWithString:@"http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/"];
       NSURL *url = [NSURL URLWithString:theurl];
       //UIImage *image = [UIImage imageNamed:@"roadfire-icon-square-200"];
       UIImage*image;
      if([theimg hasPrefix:@"http://"] || [theimg hasPrefix:@"https://"] ) {
        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:theimg]]];
       } else {
         image = [UIImage imageNamed:theimg];
       }

       UIActivityViewController *controller =
       [[UIActivityViewController alloc]
       initWithActivityItems:@[text, url, image]
       applicationActivities:nil];

      controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
      UIActivityTypeMessage,
      UIActivityTypeMail,
      UIActivityTypePrint,
      UIActivityTypeCopyToPasteboard,
      UIActivityTypeAssignToContact,
      UIActivityTypeSaveToCameraRoll,
      UIActivityTypeAddToReadingList,
      UIActivityTypePostToFlickr,
      UIActivityTypePostToVimeo,
      UIActivityTypePostToTencentWeibo,
      UIActivityTypeAirDrop];

       [controller setCompletionHandler:^(NSString*activityType, BOOL completed) {
       [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
       }];

       [(self._mypage).object presentViewController:controller animated:YES completion:nil];
    }

    # end if

    Sub activity_completed(status As Boolean, T AsString)Log("onSharingDone: Status = " & status & " - Type: " & T)
    End Sub

    Sub ShareToSocialMedia(p AsPage, txt AsString, theURL AsString, imageFile AsString)

    myPage = pDim NativeMe AsNativeObject = Me
    NativeMe.RunMethod("share2SocialMedia:::", Array(txt, theURL, imageFile))

    End Sub
     
    Last edited: Apr 17, 2015
    Pendrush likes this.
  19. henrywood

    henrywood Active Member Licensed User

    @narek Thanks a million !

    But is the call from B4i then still OK - I am this line:

    Code:
    NativeMe.RunMethod("share2SocialMedia:"Array As Object(txt, theURL, imageFile))
     
  20. narek adonts

    narek adonts Well-Known Member Licensed User

    it should be

    NativeMe.RunMethod("share2SocialMedia:::", Array(txt, theURL, imageFile))
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice