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
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.
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Error on iPAD

Solution on http://stackoverflow.com/a/27189994
If anyone can try to convert it for b4i.
 
Last edited:
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Something like this maybe:

B4X:
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:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Error on compile
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Almost there. Only one error.

 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Share is working on iPad and iPhone, error is in
B4X:
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

B4X:
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.
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
@Pendrush The code you tried does not have these lines:
B4X:
  UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  UIView *topView = window.rootViewController.view;

so here is the code again:

B4X:
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
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Here is your code, but same thing, popup window is not shown below share button.
Code works as expected, except popup window position.
 

Attachments

  • share_test.zip
    9.8 KB · Views: 268
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
In both cases I have click on first button.
Popup window also have same position if I click second button.
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
@Pendrush

Maybe you could try:

B4X:
#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:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Error on compile again

Called with
B4X:
Sub cmdShareLocalImage_Click

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

End Sub


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

henrywood

Active Member
Licensed User
Longtime 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
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Image 1 - first button click
Image 2 - second button click
Popup share is down and a little bit right
 

Attachments

  • IMG_0228.JPG
    47.1 KB · Views: 245
  • IMG_0229.JPG
    44.9 KB · Views: 257
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…