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:

narek adonts

Well-Known Member
Licensed User
Longtime 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"activity_completed::" params[@((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
 
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Just in case somebody else needs the sharing functionality here with Narek's changes

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

Pendrush

Well-Known Member
Licensed User
Longtime User
I'm unabele to compile with OBJC code.
 
Last edited:
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Error in compile
 
Last edited:
Upvote 0

henrywood

Active Member
Licensed User
Longtime User
Now I've edited post #4 again, but:

1. I am also not sure if this line:

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

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

3. I am unsure about the "correctness" of this line

B4X:
 NativeMe.RunMethod("share2SocialMedia:", Array As Object(txt, theURL, imageFile))

Maybe someone savyy in Objective C could please take a look (@Erel ?) ?
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime 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:

B4X:
Dim myPage as Page=Page1

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

Pendrush

Well-Known Member
Licensed User
Longtime User
Just tried again, now has 8 errors
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
this is the OBJC code that sould work.

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

henrywood

Active Member
Licensed User
Longtime User
@narek Thanks a million !

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

B4X:
NativeMe.RunMethod("share2SocialMedia:", Array As Object(txt, theURL, imageFile))
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…