Hey all, need some quick help with my objective-c plugin.
All it does it check to see if Facebook or yelp is installed.
The problem is, I’m not familiar with Objective-C or retrieving the value in the extern C wrapper. see my code below.
I appreciate any help provided.
Thanks y’all,
//
// UnityPluginTest-1.mm
//
// Created by OJ on 7/13/16.
//
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
/* method declaration */
- (BOOL)isYelpInstalledX;
- (BOOL)isFBInstalledX;
@end
@implementation SampleClass
- (BOOL)isYelpInstalledX {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yelp://"]];
}
- (BOOL)isFBInstalledX {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]];
}
@end
extern "C"
{
bool isFBInstalled(){
// Need to get the Objective C BOOL value from above, my c# script will read get this value once retrieved
//return -(Bool) isFBInstalledX value //--this doesn't work
//return ..... // I give up :(
}
bool isYelpInstalled(){
return false;
}
}
First - Apparently apple now requires you to place the URL schemes you’re planning to call in your info.plist -
“If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by using the LSApplicationQueriesSchemes array in your Xcode project’s Info.plist file. For each URL scheme you want your app to use with this method, add it as a string in this array.” - here’s that resource Apple UIApplication-canOpenURL
In this case my strings would be fb, and yelp.
Second, feel free to use my attached scripts:
UnityPluginTest-1.mm (put this in your “Assets>plugins>ios” folder)[74496-unityplugintest-1mm.txt|74496]
GetMyOjbectiveCUnityPlugin.cs ( this is the bridge. Place this in the same folder)[74497-getmyojbectivecunityplugincs.txt|74497]
delete the “txt” portion in the file extension to get them to work
To call the FB/Yelp status from a script of your own you can do:
int testfb = GetMyOjbectiveCUnityPlugin().WegotFBApp(); //0 is No, 1 yes
int testyelp = GetMyOjbectiveCUnityPlugin().WegotYelpApp(); //0 is No, 1 yes
The following is as far as I got to, unfortunately it’s returning as false when fb & yelp are actually both installed on my device:
// UnityPluginTest-1.mm
// Created by OJ on 7/13/16.
//In unity, You'd place this file in your "Assets>plugins>ios" folder
//Objective-C Code
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
/* method declaration */
+ (BOOL)isYelpInstalledX;
+ (BOOL)isFBInstalledX;
@end
@implementation SampleClass
+ (BOOL)isYelpInstalledX {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yelp://"]];
}
+ (BOOL)isFBInstalledX {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]];
}
@end
//C-wrapper that Unity communicates with
extern "C"
{
bool isFBInstalled(){
// Need to get the Objective C BOOL value from above, my c# script will read get this value once retrieved
return [SimpleClass isFBInstalledX]; //Tested with a build but not returning the true status, also not sure if it's bc it's a BOOL vs a bool
}
bool isYelpInstalled(){
// Need to get the Objective C BOOL value from above, my c# script will read get this value once retrieved
return [SimpleClass isYelpInstalledX ]; //Tested with a build but not returning the true status, also not sure if it's bc it's a BOOL vs a bool
}
}
Anyone wondering what the C# script that calls this .mm Obj-c, it looks like this:
using UnityEngine;
using System.Runtime.InteropServices;
class GetMyOjbectiveCUnityPlugin : MonoBehaviour {
#if UNITY_IOS || UNITY_XBOX360
// On iOS and Xbox 360 plugins are statically linked into
// the executable, so we have to use __Internal as the
// library name.
[DllImport ("__Internal")]
private static extern bool isFBInstalled();
[DllImport ("__Internal")]
private static extern bool isYelpInstalled();
#else
// Other platforms load plugins dynamically, so pass the name
// of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
void Awake () {
//Assign value we receive to these
bool FBStatus;
bool YelpStatus;
// Calls the isFBInstalled function inside the plugin
FBStatus = isFBInstalled(); //returns the status if FB installed on ios device in ObjC plugin
YelpStatus= isYelpInstalled();//returns the status if Yelp installed on ios device in ObjC plugin
}
}
First - Apparently apple requires you to place the URL schemes you’re planning to call in your info.plist -
“If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by using the LSApplicationQueriesSchemes array in your Xcode project’s Info.plist file. For each URL scheme you want your app to use with this method, add it as a string in this array.” - here’s that resource Apple UIApplication-canOpenURL
In this case my strings would be fb, and yelp.
UnityPluginTest-1.mm (put this in your “Assets>plugins>ios” folder)
GetMyOjbectiveCUnityPlugin.cs ( this is the bridge. Place this in the same folder)
To call the FB/Yelp status from a script of your own you can do:
int testfb = GetMyOjbectiveCUnityPlugin().WegotFBApp(); //0 is No, 1 yes
int testyelp = GetMyOjbectiveCUnityPlugin().WegotYelpApp(); //0 is No, 1 yes