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

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

  1. hanyelmehy

    hanyelmehy Active Member Licensed User

    Thank you for your idea ,but the same error
     
  2. Pendrush

    Pendrush Well-Known Member Licensed User

    Non of CODEs posted in this topic is NOT SAFE to use in your app.
    App crash when you click on Share button on iPad (iOS 8.3), and my app was rejected on Apple Store.
    If someone have iPad with iOS 8.3, maybe to try and test code. I'm unable to test it on iPad.
     
  3. Pendrush

    Pendrush Well-Known Member Licensed User

    Error on iPAD
    Solution on http://stackoverflow.com/a/27189994
    If anyone can try to convert it for b4i.
     
    Last edited: May 8, 2015
  4. henrywood

    henrywood Active Member Licensed User

    Something like this maybe:

    Code:
    Sub Process_Globals
       
    Private myPage As Page
    End Sub


    #IF OBJC

    #define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.width :[[UIScreen mainScreen] bounds].size.height)

    #define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.height :[[UIScreen mainScreen] bounds].size.width)

    - (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) {

      // Raise the event for B4i
      if (completed) {
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
      } else {

      NSString *activity = @"";
           [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activity)]];
      }
      }];

       //if iPhone
       if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
         [(self._mypage).object presentViewController:controller animated:YES completion:nil];

       } else { //if iPad
        // Change Rect to position Popover
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        UIView *topView = window.rootViewController.view;
        [popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       }
    }

    #End If

    Sub activity_completed(status As Boolean, T As String)

       
    'If Main.DEBUG Then
       '  Utils.Log_("onSharingDone: Status = " & status & " - Type: " & T & " - Will look for callback: ShareModule_SharingCompleted in " & myPage.Title)
       'End If

       
    If SubExists(myPage, "ShareModule_SharingCompleted"2) = True Then
         
    CallSub3(myPage, "ShareModule_SharingCompleted", status, T)
       
    End If

    End Sub

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

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

    End Sub
    Could anyone try to test please ?
     
    Last edited: May 9, 2015
  5. Pendrush

    Pendrush Well-Known Member Licensed User

    Error on compile
     
  6. henrywood

    henrywood Active Member Licensed User

    tried to edit the code a little - please try again ?
     
  7. Pendrush

    Pendrush Well-Known Member Licensed User

    Almost there. Only one error.

     
  8. henrywood

    henrywood Active Member Licensed User

    edited the code yet again - please try a compile now ?
     
  9. Pendrush

    Pendrush Well-Known Member Licensed User

    Share is working on iPad and iPhone, error is in
    Code:
    Sub activity_completed(status As Boolean, T As String)

       
    'If Main.DEBUG Then
       '  Utils.Log_("onSharingDone: Status = " & status & " - Type: " & T & " - Will look for callback: ShareModule_SharingCompleted in " & myPage.Title)
       'End If

       
    If SubExists(myPage, "ShareModule_SharingCompleted"2) = True Then
         
    CallSub3(myPage, "ShareModule_SharingCompleted", status, T)
       
    End If

    End Sub



    This code also working

    Code:
    Sub Process_Globals
       
    Private myPage As Page
    End Sub


    #IF OBJC

    #define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.width :[[UIScreen mainScreen] bounds].size.height)

    #define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.height :[[UIScreen mainScreen] bounds].size.width)

    - (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,
      UIActivityTypeAssignToContact,
      UIActivityTypeSaveToCameraRoll,
      UIActivityTypeAddToReadingList,
      UIActivityTypePostToFlickr,
      UIActivityTypePostToVimeo,
      UIActivityTypePostToTencentWeibo];

      [controller setCompletionHandler:^(NSString*activityType, BOOL completed) {

      // Raise the event for B4i
      if (completed) {
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
      } else {

      NSString *activity = @"";
           [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activity)]];
      }
      }];

       //if iPhone
       if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
         [(self._mypage).object presentViewController:controller animated:YES completion:nil];

       } else { //if iPad
        // Change Rect to position Popover
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
        [popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:(self._mypage).RootPanel.object permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       }
    }

    #End If

    Sub activity_completed(status As Boolean, T As String)

       
    'If Main.DEBUG Then
       '  Utils.Log_("onSharingDone: Status = " & status & " - Type: " & T & " - Will look for callback: ShareModule_SharingCompleted in " & myPage.Title)
       'End If

    '   If SubExists(myPage, "ShareModule_SharingCompleted", 2) = True Then
    '     CallSub3(myPage, "ShareModule_SharingCompleted", status, T)
    '   End If

    End Sub

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

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

    End Sub
    But in both code, in iPad portrait popup window is on center horizontally, but on landscape popup window is on left side.
     
  10. narek adonts

    narek adonts Well-Known Member Licensed User

    Please post your project to test it
     
  11. Pendrush

    Pendrush Well-Known Member Licensed User

    Share test
     

    Attached Files:

  12. Pendrush

    Pendrush Well-Known Member Licensed User

  13. henrywood

    henrywood Active Member Licensed User

    @Pendrush The code you tried does not have these lines:
    Code:
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
      UIView *topView = window.rootViewController.view;
    so here is the code again:

    Code:
    Sub Process_Globals
      
    Private myPage As Page
    End Sub


    #IF OBJC

    #define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.width :[[UIScreen mainScreen] bounds].size.height)

    #define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.height :[[UIScreen mainScreen] bounds].size.width)

    - (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) {

      // Raise the event for B4i
      if (completed) {
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
      } else {

      NSString *activity = @"";
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activity)]];
      }
      }];

      //if iPhone
      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
      [(self._mypage).object presentViewController:controller animated:YES completion:nil];

      } else { //if iPad
      // Change Rect to position Popover
      UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
      UIWindow *window = [[UIApplication sharedApplication] keyWindow];
      UIView *topView = window.rootViewController.view;
      [popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
      }
    }

    #End If

    Sub activity_completed(status As Boolean, T As String)

      
    'If Main.DEBUG Then
      '  Utils.Log_("onSharingDone: Status = " & status & " - Type: " & T & " - Will look for callback: ShareModule_SharingCompleted in " & myPage.Title)
      'End If

      
    If SubExists(myPage, "ShareModule_SharingCompleted"2) = True Then
      
    CallSub3(myPage, "ShareModule_SharingCompleted", status, T)
      
    End If

    End Sub

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

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

    End Sub
     
  14. Pendrush

    Pendrush Well-Known Member Licensed User

    Here is your code, but same thing, popup window is not shown below share button.
    Code works as expected, except popup window position.
     

    Attached Files:

  15. Pendrush

    Pendrush Well-Known Member Licensed User

    In both cases I have click on first button.
    Popup window also have same position if I click second button.
    IMG_0226.JPG IMG_0227.JPG
     
  16. henrywood

    henrywood Active Member Licensed User

    @Pendrush

    Maybe you could try:

    Code:
    #IF OBJC

    #define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.width :[[UIScreen mainScreen] bounds].size.height)

    #define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait)||([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown))?[[UIScreen mainScreen] bounds].size.height :[[UIScreen mainScreen] bounds].size.width)

    - (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview
    {
      //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) {

      // Raise the event for B4i
      if (completed) {
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activityType)]];
      } else {

      NSString *activity = @"";
      [self.bi raiseEvent:nil event:@"activity_completed::" params:@[@((BOOL)completed),(activity)]];
      }
      }];

      //if iPhone
      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
      [(self._mypage).object presentViewController:controller animated:YES completion:nil];

      } else { //if iPad
      // Change Rect to position Popover
      UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
      //UIWindow *window = [[UIApplication sharedApplication] keyWindow];
      //UIView *topView = window.rootViewController.view;
      [popup presentPopoverFromRect:CGRectMake(SBview.Left, SBview.Top+SBview.Height, SCREEN_WIDTH/2, SCREEN_HEIGHT/4)inView:sbView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
      }
    }

    #End If

    Sub ShareToSocialMedia(p As Page, txt As String, theURL As String, imageFileOrURL As String, shareButton As Button)

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

    End Sub
     
    Last edited: May 9, 2015
  17. Pendrush

    Pendrush Well-Known Member Licensed User

    Error on compile again
    Called with
    Code:
    Sub cmdShareLocalImage_Click

        
    Dim strImagePath As String
        strImagePath = 
    File.DirAssets & "/image.png"
        modShare.ShareToSocialMedia(Page1, 
    "Text for share. Local image.""http://www.b4x.com", strImagePath, cmdShareLocalImage)

    End Sub


    Sub cmdShareOnlineImage_Click
         
        modShare.ShareToSocialMedia(Page1, 
    "Text for share. Online image.""http://www.b4x.com""http://www.b4x.com/images/b4A_bubble.png", cmdShareOnlineImage)
    End Sub
     
  18. henrywood

    henrywood Active Member Licensed User

    Typo - SBView should be SBview
    You also need to pass in a reference to the cmdShareOnlineImage button as fifth parameter
    when calling modShare.ShareToSocialMedia
     
  19. Pendrush

    Pendrush Well-Known Member Licensed User

    Yes, I know, i have pass button, as you can see in my previous post.
     
  20. Pendrush

    Pendrush Well-Known Member Licensed User

    Image 1 - first button click
    Image 2 - second button click
    Popup share is down and a little bit right
     

    Attached Files:

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