How to Control iOS device volume with Unity? - ios

I developed Enterprise App, and installed it to my iPad.
The problem is that the iPad will be built in the wall, so it will not be possible to access the volume buttons.
I want to control the volume during the exhibition. I made UIButtons "-" and "+" to control the volume of the device instead of real volume button, not the Unity audio component's volume. I researched several times, so now what should I do in Unity?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class IOSManager : MonoBehaviour {
public static IOSManager _instance;
[DllImport("__Internal")]
private static extern void iosVolumeMinus();
private static extern void iosVolumePlus();
void Awake(){
_instance = this;
}
public void CallMinusFunc()
{
iosVolumeMinus();
}
public void CallPlusFunc()
{
iosVolumePlus();
}
}
Well, I tried somethings and realized that MPMusicPlayerController And MPVolumeView may work, but had some problems.
If I use MPMusicPlayerController I get
alert : 'volume' is deprecated:
first deprecated in iOS 7.0 - Use MPVolumeView for volume control.
But if I use MPVolumeView I get
alert : Incomplete definition of type 'struct
objc_class'.
It is my first time developing an iOS app, so I don't know what the problem is.

Related

Access Unity 3d UI component like Panel/Image as a view in Objective C code

I have a certain task where i want to pass a UIView to one of the SDK's in my iOS project.
Now, my project is a simple 2D game build in Unity 3d. I want to send the Panel in my Canvas to that SDK as UIView.
I am aware of iOS and Unity communication. It happens using as Char*.
I am not sure how should i access that GameObject Panel in my iOS code as UIView. I will be very thankful if i receive any help on this.
Above is one of solution that you can access from ios interface but before see this, read https://docs.unity3d.com/Manual/PluginsForIOS.html. it will help to understand how unity and native code can communicate.
iOS: test.mm
#import "Unity/UnityInterface.h"
...
...
-(void)SendMessage
{
UnitySendMessage("MessageGameCommunicator", "Receiver", [message UTF8String]);
}
Unity: Commnicator.cs, MessageGameCommunicator (GameObject name)
public class Commnicator : MonoBehaviour
{
public static void Receiver(string message)
{
// ...
// Do something.
}
}
After Unity interface get message from ios, you can implement whatever you want.

The Type initializer for 'Realms.Realm' threw an exception

I am trying to implement Realm on a smaller Xamarin/Mvvmcross/iOS/Droid project in order to test it's ability to replace SQLite.
I have it working well on the iOS project but am getting exceptions on the Droid project when attempting to call Realm.GetInstance();
The Type initializer for 'Realms.Realm' threw an exception
Inner Exception
System.Reflection.ReflectionTypeLoadException
The classes in the module cannot be loaded.
I have narrowed it what I believe is an issue with reflection if the MvvmCross setup occurs before the Realm dll is loaded.
For example if I call Realm.GetInstance() in any activity that inherits from MvxActivity or MvxAppCompatActivity (or anywhere in the Mvvmcross Setup / CreateApp process) the exception occurs.
If however I call var db = Realm.GetInstance() (& db.Close()) from a normal Droid Activity first, and then start the Mvx Setup process, by starting an MvxActivity, from the Droid Activity it works fine, and continues to work through the application lifecycle.
Likewise if I subclass Application and open a Realm instance in OnCreate() and close it Real will initialise anywhere else in the application.
sample code
//works
[Application]
public class CustomApplication : Application
{
public CustomApplication (IntPtr javaReference, JniHandleOwnership transfer) : base (javaReference, transfer)
{
}
public override void OnCreate ()
{
base.OnCreate ();
var db = Realm.GetInstance ();
db.Close ();
}
}
//does not work unless Realm.GetInstance() has already been called once
[Activity(Label = "View for FirstViewModel")]
public class FirstView : MvxActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.FirstView);
var db = Realm.GetInstance ();
db.Close ();
}
}
I've put a test project on github at https://github.com/vurtigo/TestRealm
This is an unfortunate mix-up between Realm and Xamarin Android.
The static constructor on the Realm class walks through all the assemblies in the current AppDomain to discover all the types that inherit from RealmObject. However, if at the time Xamarin Android builds Java binding code this will define a new System.Reflection.Emit.AssemblyBuilder assembly that will raise a TypeLoadException when its types are enumerated (see Bug 39679 - ReflectionTypeLoadException after some reflection stuff).
The workaround is to cause the Realm static constructor to be invoked before any of the MvvmCross code causes Xamarin Android to emit binding code. You can do that by accessing any of the static members on Realm such as ReferenceEquals or even by including it in a typeof(Realm) expression. I suppose MvxApplication.Initialize() is a good place to do it.
In any case, I have proposed a fix that will ignore AssemblyBuilder instances in general. The very next Realm release should include it and you'll be able to delete the workaround code as soon as you upgrade.

passing values from unity 3D to objective c file

My app has two windows. First one is unity 3D exported to ios which uses vuforia kit for doing augmnted reality. Second is native xcode files. I need to pass a string value from unity to native objective c file because I need to display some values based on that. I'm using xcode 7 and unity 5. Please help me understand how to do it. Thanks in advance.
This can be done with the native plugin support in Unity. You need to import the native method into a class and call it via another method. You can find more documentation on it here: http://docs.unity3d.com/Manual/NativePlugins.html
Here is a really simple code example that should fit your needs.
The C# class:
using System.Runtime.InteropServices;
public static class SomeScript
{
[DllImport("__Internal")]
private static extern void _NativeMethodName(string stringValue);
public static void PassStringValue(string stringValue)
{
_NativeMethodName(stringValue);
}
}
And the native plugin code:
extern "C" {
void _NativeMethodName(const char* stringValue)
{
NSLog(#"Passed string value: %s", stringValue);
}
}

Determining if Facebook app is installed from Unity

We are using the Facebook SDK for Unity (v6.0) and I'd like to now whether there's a way that I can check if the Facebook app is installed on the device.
The reason is an existing bug in the Facebook SDK (see here: bug)
I want to identify this scenario (occurs only when the FB app is installed), and react accordingly.
"In order to use a native plugin you firstly need to write functions
in a C-based language to access whatever features you need and compile
them into a library. In Unity, you will also need to create a C#
script which calls functions in the native library."
from http://docs.unity3d.com/Manual/NativePlugins.html
So, basically you need to write your code in Objective-C and provide the communication between the Unity and the Native Code.
The code that you need to implement for checking Facebook APP is;
(void) checkFacebookApp
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURLURLWithString:#"fb://"]])
{
return true;
}
}
However you need some communication between the Unity and Xcode project. So;
class SomeScript : MonoBehaviour {
#if UNITY_IPHONE || 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")]
#else
// Other platforms load plugins dynamically, so pass the name
// of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
private static extern float checkFacebookApp ();
void Awake () {
// Calls the FooPluginFunction inside the plugin
// And prints 5 to the console
bool check = checkFacebookApp ();
}
}

Programming native vibration to work on iOS through AS3

I've made a tester class to test the vibration on an iPod Touch but the vibration doesn't work when tested on the device. I've imported all relevant extensions and linked to the relevant Adobe AIR library classes as per this question and answer. Am I using a wrong callback or something?
package
{
import flash.display.Sprite;
import com.adobe.nativeExtensions.Vibration;
public class Test extends Sprite
{
public function Test()
{
run();
}
public function run(): void {
var vibe:Vibration;
if (Vibration.isSupported)
{
vibe = new Vibration();
vibe.vibrate(25000);
}
}
}
}
EDIT: Found out that iPod Touches don't have vibration sensor. After testing this on an iPhone 5, it still doesn't work. As soon as the app is launched, it crashes (and closes) without vibrating.
iPod Touch do not support vibration. :)

Resources