Sharing image and text using native code

I am trying to share image and text on any selected social media. I want to implement functionality similar to intent sharing in android.

For this I have written following code in objective c and call same method in my unity c# code

@implementation ViewController
-(void) shareMethod: (const char *) path
{
    NSLog(@"Sample Method Execute");
    NSString *imagePath = [NSString stringWithUTF8String:path];
  
    //        UIImage *image      = [UIImage imageNamed:imagePath];
    NSString *message   = @"Best image from my application.";
  
    NSArray *postItems  = @[message];
  
    UIActivityViewController *activityVc = [[UIActivityViewController alloc]initWithActivityItems:postItems applicationActivities:nil];
    [self presentViewController:activityVc animated:YES completion:nil];
}
@end
extern "C"{
    void sampleMethod(const char * path){
        ViewController *vc = [[ViewController alloc] init];
        [vc shareMethod: path];
        [vc release];
    }
}

But using above code I am getting following warnings in xcode

2014-11-20 21:04:54.565 screenshotdemo[3731:a0b] Sample Method Execute
2014-11-20 21:04:54.566 screenshotdemo[3731:a0b] Warning: Attempt to present <UIActivityViewController: 0x16c56f90> on <ViewController: 0xc8e6db0> whose view is not in the window hierarchy!

Also I want to mention that all NSLog statement printed on screen. If I execute same code in core xcode project then it works perfectly but exporting same thing using Unity creates problem for me.

I am facing this problem from many days. Please I need some help to solve the problem.

Don’t use self. You need to use a view controller that’s already presented:

// need to save as .mm
[UnityGetGLViewController() presentViewController:activityVc animated:YES completion:nil];

// works with .m
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVc animated:YES completion:nil];

Also, don’t forget to release activityVc.

Thanks for this help. One point I want to ask, Where I have to release activityVc?
As per my understanding I have to write

[UnityGetGLViewController() presentViewController:activityVc animated:YES completion:nil];
[avtivityVc release];

Am I right in above?

Now you don’t have to give me any reply because above answer works perfectly for me.
Thanks @cbaltzer , I fill like god helps me because this problem frustrating me from long time.

Now after launch of iOS8, I found problem in executing same code.

At present for my game I have used following code

float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (sysVer  < 8.0) {
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVc animated:YES completion:nil];
    } else {
       
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVc];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:[UIApplication sharedApplication].keyWindow.rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       
    }

Following is scenario for different devices

iPod with iOS Version 6.0 : Works perfectly - Mine
iPad with iOS Version 8.0 : Works perfectly - Mine
iPhone 6 with iOS Version 8.2 : Not Working - Client

Last device is of client and he has detected exception in his device.
So that I didn’t have any logcat details.

Please give me some suggestion what is going wrong in my code!!!

1 Like