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

henrywood

Active Member
Licensed User
Longtime 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:
https://www.b4x.com/android/forum/t...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:

B4X:
'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:

Pendrush

Well-Known Member
Licensed User
Longtime User
/b4i_modshare.m:110:29: error: interface type cannot be statically allocated
UINavigationController navCtl = window.rootViewController.navigationController;
^
/b4i_modshare.m:112:39: error: property 'statusBar' not found on object of type 'UINavigationController *'
CGFloat statusBarHeight = navCtl.statusBar.frame.size.height;
^
2 errors generated.
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
NEW CODE

Could you please try this:

B4X:
#IF OBJC

- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview
{

#define STATUS_BAR_HEIGHT (
  [UIApplicationsharedApplication].statusBarHidden ? 0 : (
  [UIApplicationsharedApplication].statusBarFrame.size.height > 100 ?
  [UIApplicationsharedApplication].statusBarFrame.size.width :
  [UIApplicationsharedApplication].statusBarFrame.size.height
  )
)

  //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;
     UINavigationController *navCtl = window.rootViewController.navigationController;
     CGFloat navBarHeight = navCtl.navigationBar.frame.size.height;
     CGFloat extra = 0;

     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
          extra += STATUS_BAR_HEIGHT;
     }
     if (navCtl.navigationBarHidden == NO)
     {
          extra += navBarHeight;
     }


     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  }
}

#End If
 
Last edited:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Working fine if navbar is HIDDEN
When navbar is visible , same as before (code does not add extra height)
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
This line:
B4X:
  extra += navBarHeight

should have been:

B4X:
  extra += navBarHeight;

Please try once more ?
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Could you please try this code:

B4X:
#IF OBJC

- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview
{

   #define STATUS_BAR_HEIGHT (
       [UIApplicationsharedApplication].statusBarHidden ? 0 : (
         [UIApplicationsharedApplication].statusBarFrame.size.height > 100 ?
         [UIApplicationsharedApplication].statusBarFrame.size.width :
         [UIApplicationsharedApplication].statusBarFrame.size.height
       )
   )

     //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

       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
     UIWindow *window = [[UIApplication sharedApplication] keyWindow];
     UIView *topView = window.rootViewController.view;
     AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
     CGFloat navBarHeight = appDelegate.navController.navigationBar.frame.size.height;
     CGFloat extra = 0;

     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
       extra += STATUS_BAR_HEIGHT;
     }
   
     if (appDelegate.navController.navigationBarHidden == NO)
     {
       extra += navBarHeight;
     }

     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     }
}

#End If
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
What about:

B4X:
#IF OBJC

- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview
{

   CGFloat STATUS_BAR_HEIGHT = (
       [UIApplication sharedApplication].statusBarHidden ? 0 : (
         [UIApplication sharedApplication].statusBarFrame.size.height > 100 ?
         [UIApplication sharedApplication].statusBarFrame.size.width :
         [UIApplication sharedApplication].statusBarFrame.size.height
       )
   );

     //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

       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
     UIWindow *window = [[UIApplication sharedApplication] keyWindow];
     UIView *topView = window.rootViewController.view;
     B4IAppDelegate *appDelegate = (B4IAppDelegate*)[[UIApplication sharedApplication] delegate];
     CGFloat navBarHeight = appDelegate.navController.navigationBar.frame.size.height;
     CGFloat extra = 0;

     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
       extra += STATUS_BAR_HEIGHT;
     }
  
     if (appDelegate.navController.navigationBarHidden == NO)
     {
       extra += navBarHeight;
     }

     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     }
}

#End If
 
Last edited:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
/B4iProject/b4i_modshare.m:115:41: error: property 'navController' not found on object of type 'B4IAppDelegate *'
CGFloat navBarHeight = appDelegate.navController.navigationBar.frame.size.height;
^
/B4iProject/b4i_modshare.m:122:22: error: property 'navController' not found on object of type 'B4IAppDelegate *'
if (appDelegate.navController.navigationBarHidden == NO)
^
2 errors generated.
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Last try:

B4X:
#IF OBJC

- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview :(BOOL)navBarVisible
{

   CGFloat STATUS_BAR_HEIGHT = (
       [UIApplication sharedApplication].statusBarHidden ? 0 : (
         [UIApplication sharedApplication].statusBarFrame.size.height > 100 ?
         [UIApplication sharedApplication].statusBarFrame.size.width :
         [UIApplication sharedApplication].statusBarFrame.size.height
       )
   );

     //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 {
     if (theimg != nil && [theimg length] > 0) {
         image = [UIImage imageNamed:theimg];
       } else {
       image = nil;
     }
   }

     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

       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
     UIWindow *window = [[UIApplication sharedApplication] keyWindow];
     UIView *topView = window.rootViewController.view;
     CGFloat navBarHeight = 44;
     CGFloat extra = 0;

     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
       extra += STATUS_BAR_HEIGHT;
     }
   
     if (navBarVisible == YES)
     {
       extra += navBarHeight;
     }

     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 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, shareButton As Button)
   
   Dim v As View = shareButton

   myPage = p
   Dim NativeMe As NativeObject = Me
   
   If Main.NavControl.NavigationBarVisible Then
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, True))   
   Else
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, False))
   End If
   
End Sub
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
The very last try:

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
      Private myPage As Page
End Sub

#IF OBJC

- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview :(float)nbHeight
{

   CGFloat STATUS_BAR_HEIGHT = (
       [UIApplication sharedApplication].statusBarHidden ? 0 : (
         [UIApplication sharedApplication].statusBarFrame.size.height > 100 ?
         [UIApplication sharedApplication].statusBarFrame.size.width :
         [UIApplication sharedApplication].statusBarFrame.size.height
       )
   );

     //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 {
     if (theimg != nil && [theimg length] > 0) {
         image = [UIImage imageNamed:theimg];
       } else {
       image = nil;
     }
   }

     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

       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
     UIWindow *window = [[UIApplication sharedApplication] keyWindow];
     UIView *topView = window.rootViewController.view;
     CGFloat navBarHeight = nbHeight;
     CGFloat extra = nbHeight;

     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
       extra += STATUS_BAR_HEIGHT;
     }

     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 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, shareButton As Button)

   Dim v As View = shareButton

   myPage = p
   Dim NativeMe As NativeObject = Me
   Dim NativeMe2 As NativeObject = Main.NavControl
   Dim NativeMe3 As NativeObject = NativeMe2.GetField("navigationBar").GetField("frame").GetField("size").GetField("height")
   Dim h As Object = NativeMe3.AsNumber

   If Main.NavControl.NavigationBarVisible Then
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, h))
   Else
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, 0))
   End If

End Sub


Could someone please try this code on IPAD ? I don't have access to an IPAD...
 
Last edited:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User



Error

 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User

This code working as expected with and without navbar.
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
ok, but I would just prefer not to hardcode that value of 44 for the navBarHeight, but thanks for the feedback...
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Maybe this will work:

B4X:
Sub Process_Globals

  Private myPage As Page
 
End Sub

#IF OBJC

//- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview :(float)nbHeight
- (void)share2SocialMedia:(NSString *)txt :(NSString *)theurl :(NSString *)theimg :(UIView *)SBview :(UINavigationBar *)nb
{

   CGFloat STATUS_BAR_HEIGHT = (
       [UIApplication sharedApplication].statusBarHidden ? 0 : (
         [UIApplication sharedApplication].statusBarFrame.size.height > 100 ?
         [UIApplication sharedApplication].statusBarFrame.size.width :
         [UIApplication sharedApplication].statusBarFrame.size.height
       )
   );

     //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 {
     if (theimg != nil && [theimg length] > 0) {
         image = [UIImage imageNamed:theimg];
       } else {
       image = nil;
     }
   }

     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

       UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
     UIWindow *window = [[UIApplication sharedApplication] keyWindow];
     UIView *topView = window.rootViewController.view;
     CGFloat extra = 0;
   
     if (nb != nil) {
       extra += nb.frame.size.height;
     }
   
     if ([UIApplication sharedApplication].statusBarHidden == NO)
     {
       extra += STATUS_BAR_HEIGHT;
     }
 
     //[popup presentPopoverFromRect:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/4, 0, 0)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     //[popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height,0,SBview.Height)inView:topView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       [popup presentPopoverFromRect:CGRectMake(SBview.Left + SBview.Width/2, SBview.Top+SBview.Height+extra, 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, shareButton As Button)
 
   Dim v As View = shareButton

   myPage = p
   Dim NativeMe As NativeObject = Me
   Dim NativeMe2 As NativeObject = Main.NavControl
   'Dim NativeMe3 As NativeObject = NativeMe2.GetField("navigationBar").GetField("frame").GetField("size").GetField("height")
   'Dim h As Object = NativeMe3.AsNumber
   'Dim h As Float = 44
   Dim NativeMe3 As NativeObject = NativeMe2.GetField("navigationBar")
   Dim nb As Object = NativeMe3
 
   If Main.NavControl.NavigationBarVisible Then
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, nb)) 
   Else
     NativeMe.RunMethod("share2SocialMedia:::::", Array As Object(txt, theURL, imageFileOrURL, v, Null))
   End If
 
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…