• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

IOS Native plugin for Unity3D

Author:- Tushar Sonu Lambole

1. In xcode project create new File as File-->New-->File..
Select Cocoa Touch in left panel, in right panel Select "Objective-C class"

2. Click Next Name file as "XploarIOSPlugin" You can Use any file name just remember to rename "XploarIOSPlugin" to your file name in below code.

3. Open "XploarIOSPlugin.h" file and paste the below code 


#import "UnityAppController.h"

@interface XploarIOSPlugin : UIViewController
{
    UINavigationController *navController;
}


struct ConfigStruct {
    char* tittle;
    char* message;
};

struct SocialSharingStruct {
    char* text;
    char* url;
    char* image;
    char* subject;
};


#ifdef __cplusplus
extern "C" {
#endif
    
    void showAlertMessage(struct ConfigStruct *confStruct);
    void showSocialSharing(struct SocialSharingStruct *confStruct);
    
#ifdef __cplusplus
}
#endif


@end

4. Open "XploarIOSPlugin.mm" file and paste the below code 

#import "XploarIOSPlugin.h"

@implementation XploarIOSPlugin{
}

//Grab the Unity3D ViewController (UnityGetGLViewController())

#ifdef UNITY_4_0

//Unity4

#import "iPhone_View.h"

#else

//Unity3.5

extern UIViewController* UnityGetGLViewController();

#endif

+(id) withTittle:(char*)tittle withMessage:(char*)message{

return [[XploarIOSPlugin alloc] withTittle:tittle withMessage:message];

}

-(id) withTittle:(char*)tittle withMessage:(char*)message{

self = [super init];

if( !self ) return self;

ShowAlertMessage([[NSString alloc] initWithUTF8String:tittle], [[NSString alloc] initWithUTF8String:message]);

return self;

}

void ShowAlertMessage (NSString *tittle, NSString *message){

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:tittle

message:message

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles: nil];

[alert show];

}

+(id) withText:(char*)text withURL:(char*)url withImage:(char*)image withSubject:(char*)subject{

return [[XploarIOSPlugin alloc] withText:text withURL:url withImage:image withSubject:subject];

}

-(id) withText:(char*)text withURL:(char*)url withImage:(char*)image withSubject:(char*)subject{

self = [super init];

if( !self ) return self;



NSString *mText = [[NSString alloc] initWithUTF8String:text];

NSString *mUrl = [[NSString alloc] initWithUTF8String:url];

NSString *mImage = [[NSString alloc] initWithUTF8String:image];

NSString *mSubject = [[NSString alloc] initWithUTF8String:subject];


NSMutableArray *items = [NSMutableArray new];

if(mText != NULL && mText.length > 0){

[items addObject:mText];

}

if(mUrl != NULL && mUrl.length > 0){

NSURL *formattedURL = [NSURL URLWithString:mUrl];

[items addObject:formattedURL];

}
if(mImage != NULL && mImage.length > 0){

// For image from Web Url

if([mImage hasPrefix:@"http"])

{

NSURL *urlImage = [NSURL URLWithString:mImage];

// NSLog(@"Enter urlImage");

NSData *dataImage = [NSData dataWithContentsOfURL:urlImage];

// NSLog(@"Enter data %d",dataImage.length);

UIImage *imageFromUrl = [UIImage imageWithData:dataImage];

// NSLog(@"Enter data %f",imageFromUrl.size.height);

[items addObject:imageFromUrl];

}else{



// For image in local storage

NSFileManager *fileMgr = [NSFileManager defaultManager];



// Point to Document directory

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];


NSString *imagePath = [[documentsDirectory stringByAppendingString:@"/"] stringByAppendingString:mImage];

// NSLog(@"Image Path %@",imagePath);

if([fileMgr fileExistsAtPath:imagePath]){

NSData *dataImage = [NSData dataWithContentsOfFile:imagePath];

// NSLog(@"Enter data %d",dataImage.length);

UIImage *imageFromUrl = [UIImage imageWithData:dataImage];

// NSLog(@"Enter data %f",imageFromUrl.size.height);

[items addObject:imageFromUrl];

}else{
ShowAlertMessage(@"Error", @"Cannot find image");
}
}
}

UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:Nil];

[activity setValue:mSubject forKey:@"subject"];

UIViewController *rootViewController = UnityGetGLViewController();
[rootViewController presentViewController:activity animated:YES completion:Nil];

return self;
}

# pragma mark - C API
XploarIOSPlugin* instance;

void showAlertMessage(struct ConfigStruct *confStruct) {
instance = [XploarIOSPlugin withTittle:confStruct->tittle withMessage:confStruct->message];
}

void showSocialSharing(struct SocialSharingStruct *confStruct) {
instance = [XploarIOSPlugin withText:confStruct->text withURL:confStruct->url withImage:confStruct->image withSubject:confStruct->subject];
}

@end

Here your IOS code part ends. Copy the both the files "XploarIOSPlugin.h" and "XploarIOSPlugin.m" and paste at the location /Assets/Plugins/iOS of your Unity3D project.

---- In Unity3d ----------

1. Create new c# script name it as "IOSBridge" and paste the below cod



#if UNITY_IPHONE
public struct ConfigStruct
{
    public string tittle;
    public string message;
}
[DllImport ("__Internal")] private static extern void showAlertMessage(ref ConfigStruct conf);

public struct SocialSharingStruct
{
    public string text;
public string url;
public string image;
public string subject;
}

[DllImport ("__Internal")] private static extern void showSocialSharing(ref SocialSharingStruct conf);



#endif


public static void CallSocialShare(string title, string message)
{
#if UNITY_IPHONE
//IPHONE - Display the UIViewController
ConfigStruct conf = new ConfigStruct();
conf.tittle  = title;
conf.message = message;
showAlertMessage(ref conf);

#endif
}
public static void CallSocialShareAdvanced(string defaultTxt, string subject, string url, string img)

{
#if UNITY_IPHONE
//IPHONE - Display the UIViewController
SocialSharingStruct conf = new SocialSharingStruct();
conf.text = defaultTxt; 
conf.url = url;
conf.image = img;
conf.subject = subject;

showSocialSharing(ref conf);

#endif
}

2. Call above method CallSocialShare("This is Alert Tittle"," This is Message Text");

CallSocialShareAdvanced("AR Battle Tank"," AR Battle Tank Application link","https://play.google.com/store/apps/details?id=com.tushar.ARBattleTank","01.jpg");

"01.jpg" can be replace with any web url of image like 
"http://blog.gettyimages.com/wp-content/uploads/2013/01/Siberian-Tiger-Running-Through-Snow-Tom-Brakefield-Getty-Images-200353826-001.jpg"






Contact

Get in touch with me


Adress/Street

B/1, Keshar Gaurav BLD, Gujarathi Baug, Shahapur

Phone number

+(91) 9423026579

Website

http://tusharlambole.blogspot.com/