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

  2. henrywood

    henrywood Active Member Licensed User

    Could someone try a compile ?

    Please use the code in post #18
     
  3. Pendrush

    Pendrush Well-Known Member Licensed User

    Compiled successfully.
    Now I will try to use it, and will report back my findings. :)
    Thank you both.
     
  4. Pendrush

    Pendrush Well-Known Member Licensed User

    Tested with online image and with image from assets, working fine if I click on Viber for example.
    If click on CANCEL application just Force Close with error:
    Line 50 is END SUB

    Code:
    Sub Button1_Click

        
    Dim tmpPath As String
        tmpPath = 
    File.DirAssets & "/image.png"
       
        modShare.ShareToSocialMedia(Page1, 
    "aaaaa""http://bing.com", tmpPath)
       
    End Sub 'Line 50
     
  5. henrywood

    henrywood Active Member Licensed User

    I will take another look in 1 hor
    So it crashes if sharing is cancelled?
     
  6. Pendrush

    Pendrush Well-Known Member Licensed User

  7. henrywood

    henrywood Active Member Licensed User

    Hey !

    Now I updated the code once again - See below

    Let me know if that solves your problem ?

    NEW 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.

       
    Private myPage As Page

    End Sub

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

         // Dismiss the View ?
         //[(self._mypage).object dismissViewControllerAnimated:YES completion:nil];

         // 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)]];
         }
       }];

       [(self._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(txt, theURL, imageFile))

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

    Pendrush Well-Known Member Licensed User

    Errors again, on compile
     
  9. henrywood

    henrywood Active Member Licensed User

    Try again please :)
     
  10. Pendrush

    Pendrush Well-Known Member Licensed User

    Good job mate. Working as expected. Thank you.
     
  11. Pendrush

    Pendrush Well-Known Member Licensed User

    Working example in attachment.
    You can change code in OBJC, to allow more sharing options.
    These are excluded for share in example:
    Code:
    controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
        UIActivityTypePrint,
        UIActivityTypeCopyToPasteboard,
        UIActivityTypeAssignToContact,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypeAddToReadingList,
        UIActivityTypePostToTencentWeibo,];
     

    Attached Files:

    Mark Turney likes this.
  12. henrywood

    henrywood Active Member Licensed User

    You're welcome :)
     
  13. hanyelmehy

    hanyelmehy Active Member Licensed User

    i get this error when try code in post #31 (on iPhone OS 7)
    Code:
    *** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[2]
    Stack Trace: (
      CoreFoundation       <redacted> + 
    154
      libobjc.A.dylib      objc_exception_throw + 
    38
      CoreFoundation       <redacted> + 
    418
      CoreFoundation       <redacted> + 
    44
      Picture 
    and Words Proverbs          0x0007ce8d -[b4i_modshare share2SocialMedia:::] + 688
      CoreFoundation       <redacted> + 
    68
      CoreFoundation       <redacted> + 
    282
      Picture 
    and Words Proverbs          0x000a7ec5 +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1928
      Picture 
    and Words Proverbs          0x000bb2ed -[B4INativeObject RunMethod::] + 164
      Picture 
    and Words Proverbs          0x0007c453 -[b4i_modshare _sharetosocialmedia::::] + 1206
    Picture 
    and Words Proverbs          0x0005e9b3 -[b4i_act2 _spage1_barbuttonclick:] + 1938
    CoreFoundation       <redacted> + 
    68
    CoreFoundation       <redacted> + 
    282
    Picture 
    and Words Proverbs          0x000a7ec5 +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1928
    Picture 
    and Words Proverbs          0x001d8d19 -[B4IShell runMethod:] + 496
    Picture 
    and Words Proverbs          0x001d7e8d -[B4IShell raiseEventImpl:method:args::] + 2060
    Picture 
    and Words Proverbs          0x001dace1 -[B4IShellBI raiseEvent:event:params:] + 1316
    Picture 
    and Words Proverbs          0x000a73a3 __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
    libdispatch.dylib    <redacted> + 
    10
    libdispatch.dylib    <redacted> + 
    22
    libdispatch.dylib    <redacted> + 
    254
    CoreFoundation       <redacted> + 
    8
    CoreFoundation       <redacted> + 
    1300
    CoreFoundation       CFRunLoopRunSpecific + 
    522
    CoreFoundation       CFRunLoopRunInMode + 
    106
    GraphicsServices     GSEventRunModal + 
    138
    UIKit                UIApplicationMain + 
    1136
    Picture 
    and Words Proverbs          0x00045e65 main + 116
    libdyld.dylib        <redacted> + 
    2
    )
    any suggestion
     
  14. Pendrush

    Pendrush Well-Known Member Licensed User

    I have iPhone 5 with iOS 8.3. Working fine.
    Error is on share with local image or online image?
     
  15. Pendrush

    Pendrush Well-Known Member Licensed User

  16. Pendrush

    Pendrush Well-Known Member Licensed User

    For my sharing I'm using modified version with only 2 options, Subject and TextForShare.

    Code:
    Sub Process_Globals
        
    Private myPage As Page 'ignore

    End Sub


    #IF OBJC

    //- (void)share2SocialMedia:(NSString *)subj :(NSString *)txt :(NSString *)theurl :(NSString *)theimg
    - (void)share2SocialMedia:(NSString *)subj :(NSString *)txt
    {
        NSString *subject = subj;
        NSString *text = txt;
        //NSURL *url = [NSURL URLWithString:theurl];
        //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]
        //initWithActivityItems:@[text, url, image]
        applicationActivities:nil];
        [controller setValue:subject forKey:@"subject"];

        controller.excludedActivityTypes = @[UIActivityTypePostToWeibo,
        UIActivityTypePrint,
        UIActivityTypeCopyToPasteboard,
        UIActivityTypeAssignToContact,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypeAddToReadingList,
        UIActivityTypePostToTencentWeibo,];

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

        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)]];
        }
        }];

        [(self._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, SubjectTxt As String, TxtForShare As String, TheURL As String, ImageFile As String)
    Sub ShareToSocialMedia(p As Page, SubjectTxt As String, TxtForShare As String)

        myPage = p
        
    Dim NativeMe As NativeObject = Me
        NativeMe.RunMethod(
    "share2SocialMedia::"Array(SubjectTxt, TxtForShare))
        
    'NativeMe.RunMethod("share2SocialMedia::::", Array(SubjectTxt, TxtForShare, TheURL, ImageFile))

    End Sub
     
  17. hanyelmehy

    hanyelmehy Active Member Licensed User

    local image
     
  18. hanyelmehy

    hanyelmehy Active Member Licensed User

    This work ,but i need to send image also
     
  19. Pendrush

    Pendrush Well-Known Member Licensed User

    Image path is not good for some reason on iOS 7. I don't have any iOS 7 device to test it. Maybe someone other can help. :(
     
  20. henrywood

    henrywood Active Member Licensed User

    @hanyelmehy Just an idea: I think that imageNamed looks in File.DirAssets and expects a filename WITHOUT extension so maybe you could try that ?
     
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