- Home /
Objective-C Value to C wrapper - Unity Plugin (ios app check)
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;
}
}
Answer by OJ3D · Jul 20, 2016 at 01:52 PM
Finally got the answer!
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)UnityPluginTest-1.mm
GetMyOjbectiveCUnityPlugin.cs ( this is the bridge. Place this in the same folder)GetMyOjbectiveCUnityPlugin.cs
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
FYI - I have a similar response in another Unity Forum - Objectiv-C to Unity Plugin ios app Status - OJ3D)
Cheers, OJ
If you get a value of 20,40 for the status's that still means a "NO". I accidentally left them there from an earlier test. You can spot those "param" values in the "UnityPluginTest-1.mm" file and update them to 0 if you'd like within their respective Objective-C methods.
$$anonymous$$y forum version has those values updated - ObjC-UnityPlugin-AppStatus
Anyone wanting to do the plist pre-loaded so you don't have to do it manually try these xcode techniques from unity -https://community.unity.com/t5/Unit...Xcode-project-targets-Info-plist/td-p/2235796
Also here's a list of popular URL schemes: http://wiki.akosma.com/IPhone_URL_Schemes
Thank you very much for the detailed responses and sharing. However, there's no need to add "fb" as an URL scheme to open facebook app. If you add that to your Info.plist, the device will open "fb://" links with your app, so be careful.
Answer by OJ3D · Jul 19, 2016 at 07:09 PM
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
}
}
Anyway, still stuck with getting nothing back. :(
Answer by OJ3D · Jul 20, 2016 at 01:52 PM
Finally got the answer!
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.
Second, feel free to use my attached scripts ( I put them in another Unity Forum - Objectiv-C to Unity Plugin ios app Status - OJ3D)
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
Cheers, OJ
Your answer
![](https://koobas.hobune.stream/wayback/20220612085026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iOS Facebook integration issue. 0 Answers
Facebook sdk for ios 9 is not opening the app when logging in, Why? 1 Answer
Do our company need a Facebook business account for integrating our unity game? 1 Answer
Facebook SDK Login Loads for a long time 0 Answers
Unity Facebook SDK Login in using FB app rather than Safari browser 0 Answers