Xamarin iOS - where does application initialization code go? - ios

I'm building a Xamarin iOS app which uses Ninject (a DI framework). I'm wondering where do I put the code that loads the modules and does the initialization of my application classes?
Would I put it in the application's delegate class?
In the Main.cs class?
In my first ViewController class (which is a menu view so it doesn't really make sens)?
What's the best practice? I can't seem to find it on google.

Due to the fact the UIApplication runloop is created/initialized during the UIApplication construction, I would avoid using the Main entry point as global app initiation point as the app's runloop is not available. Calling any iOS framework members during this phase can led to strange app behavior, crashes, file corruption, etc..
Also any initialization code that hangs (delays) the app at this point will cause the OS to kill your process and any external crash reporting will not happen. Apple's crash reports will be very generic SIGABRT reports.
The UIApplicationDelegate is also created during the UIApplication construction and the FinishedLaunching (application:didFinishLaunchingWithOptions:) delegate method is messaged after the UIApplication, its runloop and UIApplicationDelegate are constructed, but before the UIWindow and root VC are, thus making it the preferred delegate override.

In the FinishedLaunching method of AppDelegate.cs is one place for sure, but in several apps I have needed dependencies resolved prior because they are used in FinishedLaunching, so I have become accustomed to put this in the Main method of App.cs instead.
Here is a very simple example for an app that used Splat.Locator for rudimentary DI:
public class App
{
private static void Main(string[] args)
{
// Register platform-specific dependencies.
RegisterDependencies();
// Launch UI.
UIApplication.Main(args, null, "AppDelegate");
}
private static void RegisterDependencies()
{
// Akavache secure cache encryption provider.
var encryptionProvider = new KeystoreBackedEncryptionProvider(Akavache.BlobCache.TaskpoolScheduler, CacheManager.AppName, CacheManager.CacheName);
Locator.CurrentMutable.RegisterConstant(encryptionProvider, typeof(Akavache.IEncryptionProvider));
}
}

AppDelegate is usually the place you would put any app startup code

Related

Opentok not calling the controller methods (sessiondidconnect) in ios

I am trying to implement an OpenTok application using codename one. I already started with android and had pretty much everything working. I use a native interface to import the OpenTok library.
Now I am trying to implement the ios side. I have a class acting like the view controller :
#interface be_lsit_opentok_iosTestHelper <OTSessionDelegate, OTPublisherDelegate> : NSObject
This class implements all the needed methods for the session connection : https://tokbox.com/developer/tutorials/ios/basic-video-chat/ (step 4).
My class implementing the native interface handles the initialization of the session with the api_key... and then calls the session's "connectWithToken".
Once this is done I can notice using the OpenTok Playground that the connection works but I do also notice that opentok does not send a message to call the method "sessionDidConnect" which is a problem.
I do not understand why the call is not done and what sould I do?
I also tryed to call the method myself using a thread call the method only when the connection to the session is made and that its status is "OTSessionConnectionStatusConnected" and this worked.
I'm guessing here since I didn't do this. You implemented the viewDidLoad in your own code instead of the Codename One view did load.
You can inject code into the Codename One viewDidLoad method using the build hint ios.viewDidLoad. So a build hint like:
ios.viewDidLoad=[self connectToAnOpenTokSession];
Should work but you would also need to add an import for the API into our code. Unfortunately I couldn't find a suitable build hint to define that so I added one ios.viewDidLoadInclude which I will add tomorrow to the build will allow you to define an import or include statement required by the statement above.

How to access the App Delegate from a UI Test?

I want to access a particular property router from the App Delegate of the launched app during a UI Test, but I can't figure out if this is possible or not. I have tried:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let router = appDelegate.router
but this gives a failure and won't even build. I am using #testable on my project module. Any ideas?
Xcode UI testing is designed such that the test code can only see what a user can see, so no objects from the application under test can be used or inspected in the test code. This is also why the views on the screen are represented as XCUIElement objects instead of UIView descendants.
The UI tests run in a separate executable from the application under test. The only way to communicate additional information from the app to a UI test is by constructing a string containing the information and using it as the accessibility identifier for an element.
If you want to test something that requires access to an object from the application code, it is most likely that you need to write a unit test instead of a UI test.

iOS App Internal Control Transfer

I am a novice to iOS app development. While using the StoryBoards for creating the iOS app, the control transfers from applicationDidFinishLaunchingWithOptions() methods to the viewDidLoad() method directly without any code been mentioned in the applicationDidFinishLauching() method.
What is the internal process that occurs in this scenario.
I have seen in the main.m file that there is a piece of code which calls the AppDelegate.class, but nothing such is present in the applicationDidFinishLaunching() method.
You have a default storyboard with initial controller (the arrow incoming to controller). this controller is initiated first after Application initialization is done. I mean, there is no explicit code for this, the Cocoa makes it for you.

Push notifications in Delphi XE6 are displayed

I played with TPushEvents on Android for a while and noticed: when app is minimized push notification immediately goes right to the notification area. I suppose that this behaviour is defined in the native code (inside com.embarcadero.gcm.notifications.GCMNotification).
Can anyone confirm or refute my guess?
How this behaviour can be turned off? (I need to decide by myself whether notification should be displayed to a user.)
An alternative way is to inherit FMXNativeActivity class and overrid its public void receiveGCM(Bundle bundle) method but not deal with pause condition.
I myself can confirm this guess.
Decompile classes.dex, rewrite com.embarcadero.gcm.notifications.GCMNotification to suite your needs, compile and put it back inside classes.dex using the script from here.
I'm just not sure it's legal.

Play 2.0 - starting as Windows service after server restart

I have the Play! application running as a windows service. It is implemented according to this guidance.
The problem is that the RUNNING_PID at the root folder of the application is not removed when the server is restarted and the application cannot start again. I have to remove this file and start the service again manually.
Is there any option to solve it?
YAJSW
In case of YAJSW I found this answer with better understanding. It's of course pretty similar to link you gave, anyway keep in mind that it's more often advised to use dist command instead of stage as it has got better developers attention (more bugs fixed in dist). And Mikhail's answer is just clearer (vote him up!)
RUNNING_PID
In case of RUNNING_PID, there was some pull requests which suggested to add an option of disabling pidfile... anyway as I can see, none of them was accepted still...
Actually if you can't avoid creating it, you can... remove it right after application's start, preferably with Globals object's onStart() method. To stay informed what is current PID of the working instance, just rename the file to something, which won't be checked by Play at the startup - for an example RUNNING_PID_INFO. In such case after server's restart service will run your application without problems.
import play.GlobalSettings;
import java.io.File;
public class Global extends GlobalSettings {
#Override
public void onStart(Application application) {
File pidFile = new File("RUNNING_PID");
pidFile.renameTo(new File("RUNNING_PID_INFO"));
}
#Override
public void onStop(Application application) {
File pidFile = new File("RUNNING_PID_INFO");
pidFile.delete();
}
}
(note: changing pidfile.path in apllication.conf will NOT solve the problem, as play will use that for checking if instance is working).
Since Play Framework 2.1 you can disable the PID file by setting the pidfile.path property:
Windows:
-Dpidfile.path=NUL
Unix:
-Dpidfile.path=/dev/null
Found at https://groups.google.com/forum/#!topic/play-framework/4amD9o37Ki4
I recently installed a play framework app using YAJSW by following this answer. I noticed that now, RUNNING_PID is automatically deleted and you don't have to worry about modifying your code to delete the file. Also, if your service depends on other services, it is better to set DELAYED_AUTO_START as the start mode to ensure the service is properly started after server reboot.

Resources