Saturday, August 31, 2013

Run iPhone or iPad application in emulator

 

If you have an iOS app which you would like to send to a customer to see your progress you can do that easily, and without sending him the source code.

The customer needs to have a Mac and XCode installed with the same same version of the SDK you target in your app.

On the development machine:

1. Get simplaunch: https://github.com/landonf/simlaunch/downloads

2. In Xcode, make sure compilation target is set to  “[ProjectName]\iPhone [x] Simulator”

     Compile your app (Project\Build or Command+B or RightWinKey+B)

3. Click on “Organizer” button (it’s in the right top of the XCode window)

4. Click the “Projects” tab and make sure your project is selected in the list from the left (it has the dot on the right of the name)

5. Click the arrow after the “Derived Data” path

6. Navigate to Build\Producs\Debug-iphonesimulator

7. Drag the [AppName].app on the “Simulator Bundler” icon on desktop

8. It will generate the bundled app is in the same folder with the source app

This is the app you can send to your customer.

On the customer machine, the customer needs to unarchive the file and run the app from inside (double-click it).

image

More info:

http://stackoverflow.com/questions/932480/sharing-iphone-apps-for-the-simulator

http://stackoverflow.com/questions/6349073/looking-for-a-better-way-to-demo-iphone-apps

http://stackoverflow.com/questions/5919959/iphone-simulator-folder-not-in-application-support

http://stackoverflow.com/questions/1187611/how-to-install-iphone-application-in-iphone-simulator

http://cocoamanifest.net/articles/2011/12/running-your-ios-app-in-the-simulator-from-the-command-line.html

http://stackoverflow.com/questions/8351278/run-iphone-ipad-simulator-for-continuous-integration

http://stackoverflow.com/questions/932480/sharing-iphone-apps-for-the-simulator/3493270#3493270

http://www.mcafee.com/us/resources/white-papers/foundstone/wp-pen-testing-iphone-ipad-apps.pdf

Friday, August 30, 2013

Android native development startup


Installation


#1. Download JDK 6
Java 7 is not officially supported by the Android SDK at this time. Please use the latest version of the Java 6 JDK.
http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html

For a 64 bit dev, note that the following trio is required:
  • 64-bit OS
  • 64-bit JDK
  • 64-bit Eclipse

#2.  Add the path to JDK (for example 'C:\Program Files\Java\jdk1.6.0_45\bin') to PATH environment variable.

#3. Download ‘Eclipse Standard’ from http://www.eclipse.org/downloads/
It is not an installer but a .zip archive. Extract its content to a folder like D:\Eclipse

#4 Run eclipse.exe.
It will ask you to select a Workspace. By default it gives a location in C:\Users
Better choose something like D:\Eclipse\Workspaces\MyWorkspace

#5. Install ADT plugin for Eclipse (Android Development Tools)
http://developer.android.com/sdk/installing/installing-adt.html

#6. You might need to install platforms and packages
http://developer.android.com/sdk/installing/adding-packages.html
Emulator runs best using Intel image, so make sure it is selected for all platforms images you install (if available)

Emulator

Keyboard shortcuts: http://developer.android.com/tools/help/emulator.html


Copy and paste: http://stackoverflow.com/questions/3391160/paste-text-on-android-emulator




Thursday, August 29, 2013

PCL libraries overview

This is very good article on the PCL architecture and different ways to implement a PCL library:
http://blogs.msdn.com/b/dsplaisted/archive/2012/08/27/how-to-make-portable-class-libraries-work-for-you.aspx
Important quote:
“The code to use your library should also be simple.  That means the project referencing your library shouldn’t have to manage hooking up the platform specific part of your library with the portable part.  One way to do this is to have an initialize method in your platform specific part of your library which passes references to platform specific implementations down to the portable part of your library.  Here’s an example of what this could look like"
“For consumers of your library, it will be easier if the portable part of your library connects to the platform specific part with no action required on their part.  You can do this by having the portable part load the platform-specific part by calling Assembly.Load(), and using reflection to get the functionality it needs.  For an example of how to do this, see  the Portable Class Libraries Contrib project.  The code which handles this functionality is in the Source\Portable.Runtime\Adaptation folder.  The Reactive Extensions (Rx) also use this method to connect their portable System.Reactive.Core assembly with the platform-specific System.Reactive.PlatformServices assembly. “
A simple example of PCL is PCL Storage: http://pclstorage.codeplex.com/
The project consists of a PCL project (platform independent) called PCLStorage.Abstractions and specific platform projects called PCLStorage.[PlatformName]
image
PCLStorage.Abstractions is referenced by all platform implementation projects (the PCLStorage.[PlatformName] projects).
It contains definitions of interfaces which platform implementations implement.
image
namespace PCLStorage
{
    public static class FileSystem
    {
        public static IFileSystem Current { get; }
    }

    public interface IFileSystem
    {
        IFolder LocalStorage { get; }
        IFolder RoamingStorage { get; }

        Task<IFile> GetFileFromPathAsync(string path);
        Task<IFolder> GetFolderFromPathAsync(string path);
    }

    public enum CreationCollisionOption
    {
        GenerateUniqueName = 0,
        ReplaceExisting = 1,
        FailIfExists = 2,
        OpenIfExists = 3,
    }

    public interface IFolder
    {
        string Name { get; }
        string Path { get; }

        Task<IFile> CreateFileAsync(string desiredName, CreationCollisionOption option);
        Task<IFile> GetFileAsync(string name);
        Task<IList<IFile>> GetFilesAsync();

        Task<IFolder> CreateFolderAsync(string desiredName,
            CreationCollisionOption option);
        Task<IFolder> GetFolderAsync(string name);
        Task<IList<IFolder>> GetFoldersAsync();

        Task DeleteAsync();
    }

    public enum FileAccess
    {
        Read,
        ReadAndWrite
    }

    public interface IFile
    {
        string Name { get; }
        string Path { get; }

        Task<Stream> OpenAsync(FileAccess fileAccess);
        Task DeleteAsync();
    }

    public static class PortablePath
    {
        public static char DirectorySeparatorChar { get; }
        public static string Combine(params string[] paths);
    }
    public static class FileExtensions
    {
        public static async Task<string> ReadAllTextAsync(this IFile file)
        public static async Task WriteAllTextAsync(this IFile file, string contents);
    }
}



So when developer installs PCLStorage from NuGet for a specific project on a specific platofrm, NuGet manager will add as reference to the project the PCLStorage.Abstractions.dll PCL and the PCLStorage.[PlatformName].dll implementation.

In code, here’s how PCLStorage library is used:


public async Task PCLStorageSample()
{
    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
        CreationCollisionOption.OpenIfExists);
    IFile file = await folder.CreateFileAsync("answer.txt",
        CreationCollisionOption.ReplaceExisting);
    await file.WriteAllTextAsync("42");
}

FileSystem.Current is instantiating the platform specific IFileSystem.

This code resides in a file in PCLStorage.dll PCL which is linked to other PCLStorage.[PlatfortmName].dll projects

PCLStorage.dll is not used besides just for holding files to be linked (it is not used as reference by any projects)

Why use ‘AppName.Droid’ for Xamarin Android apps and not ‘AppName.Android’

 

It looks like the if you use “Android” you end up having to use `global::Android` a lot.

So even if “Android” might look better than “Droid”, use “Droid”

Monday, August 26, 2013

Xamarin iOS app setup intro (Visual Studio)


To make the Visual Studio build iOS apps, you need to install Xamarin Studio on the Mac and possibly update your XCode version. Follow the requirements and instructions from http://docs.xamarin.com/guides/ios/getting_started/introduction_to_xamarin_ios_for_visual_studio
After all setup is done, in Visual Studio create a iOS \ Universal app.
Make sure you have Solution Platforms combo-box available in the standard command bar, otherwise click on the overflow button and choose Add\Remove buttons, and choose Solution Platforms. This combo-box should show iPhone and iPhoneSimulator options.
To connect to the mac machine, I am using VNC viewer: http://www.realvnc.com/download/viewer/
In Visual Studio, running the app you might get an error on the mac: “The simulated application quit.” and an option to switch SDK.
Here’s the things I did to make it work:
- make sure you have Application name (‘HelloWorld_App1’), Identifier (‘com.helloworldapp’), Version (1) and Deployment Target (6.1) set.
- if you get the error, right click and select to dock the simulator.  I was able to select ‘Reset settings’ from the menu just before the error dialog appeared. If not try to delete ~/Library/Application Support/iPhone Simulator directory on your Mac



Other info on the iOS setup:

http://forums.xamarin.com/discussion/comment/6084/#Comment_6084
http://blogs.endjin.com/2013/05/xamarin-platform-setup-gotchas/

Monday, June 17, 2013

Svc handling in IIS

I bump into this issue every-time I am running on a new machine.
When trying to access WCF services, they do not work. That's because IIS by default does not handle it.

In Windows Features (Control Panel\Turn Windows features on or off), you need to make sure you have WCF Activation installed.


Thursday, June 06, 2013

More dynamic Android Fragments with MVVM and MVVMCross

 

Scenario: I need to create and load fragments dynamically (by code).

Example: I have a hierarchical data with categories and items, a category can contain items and other categories, like in Windows Explorer:

image

When user clicks on a folder icon, if it’s a folder, it will show the same view with folder’s child folders and items. If it’s an icon, we want to show just a label with the name of the item (in real-life, this can be rendering a document).

In Android, we will need

  1. an activity class and layout (or even a parent fragment) as host for the views. it will create and show the fragments dynamically by code,
  2. a fragment class and layout for the folder view (with a GridView)
  3. a fragment class and layout for the item view (with a TextView)

With MVVMCross, we will also have:

  1. a view-model class for the activity/parent fragment host
  2. a view-model class for the folder view
  3. a view-model class for the item view

With MVVM, when user clicks on a folder icon, the view-model is responsible to handle the event (by a Command) and trigger navigation to another fragment view, depending on the icon. But remember that the view-model must NOT directly instantiate and show the views, this is platform specific UI code.

In MVVMCross, we call MvxViewModel.ShowViewModel method to‘show’ the view-model. By default, what happens is, the MVVMCross finds the associated activity class (based on a name convention) and shows it. But In our case, this won’t work, because the view-models refer to fragments.

The solution is to use MVVMCross features to control the view creation mechanism.

MVVMCross has a built-in MvxAndroidViewPresenter class used to create and show an activity based on the view-model:

	public class MvxAndroidViewPresenter : IMvxAndroidViewPresenter, IMvxViewPresenter
{
public virtual void Show (MvxViewModelRequest request)
{
IMvxAndroidViewModelRequestTranslator mvxAndroidViewModelRequestTranslator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator> ();
Intent intentFor = mvxAndroidViewModelRequestTranslator.GetIntentFor (request);
this.Activity.StartActivity (intentFor);
}
}

We can tell MVVMCross to use instead our own view presenter implementation which creates and shows fragments inside our activity or parent fragment.

	public class CustomPresenter : MvxAndroidViewPresenter, ICustomPresenter
{
// map between view-model and fragment host which creates and shows the view based on the view-model type
private Dictionary<Type, IFragmentHost> dictionary = new Dictionary<Type, IFragmentHost>();

public override void Show(MvxViewModelRequest request)
{
IFragmentHost host;
if (this.dictionary.TryGetValue(request.ViewModelType, out host))
{
if (host.Show(request))
{
return;
}
}
base.Show(request);
}

public void Register(Type viewModelType, IFragmentHost host)
{
this.dictionary[viewModelType] = host;
}
}

The class is responsible for keeping a mapping between view-model types and the hosts which will instantiate and show the fragments.


We can now can register it for MVVMCross to use it, this being done in the Setup class in the Android app:

public class Setup : MvxAndroidSetup
{
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var customPresenter = new CustomPresenter();
Mvx.RegisterSingleton<ICustomPresenter>(customPresenter);
return customPresenter;
}
}

Note that the custom presenter once registered in MVVMCross, will be called for ANY view-model, whenever we call MvvmViewModel : ShowViewModel<T>();


ICustomPresenter and IFragmentHost are some simple interfaces we define in our code in order to nicely decouple the MVVMCross custom view presenter from the fragment host:

public interface ICustomPresenter
{
void Register(Type viewModelType, IFragmentHost host);
}

public interface IFragmentHost
{
bool Show(MvxViewModelRequest request);
}

It makes sense for the activity class to implement the IFragmentHost interface:

public class MainView: MvxFragmentActivity, IFragmentHost
{
public bool Show(Cirrious.MvvmCross.ViewModelsMvxViewModelRequest request)
{
// create view model
var loaderService = Mvx.Resolve<IMvxViewModelLoader> ();
var viewModel = loaderService.LoadViewModel (request, null /* saved state */);

// decide which fragment to create based on the view-model type
var fragmentView = ...

// load fragment into view
var ft = fragmentManager.BeginTransaction ();
ft.Replace (Resource.Id.fragmentHost, fragmentView);
ft.AddToBackStack (null);
ft.Commit ();
}
}

Further more, we can have the activity have its own dictionary to keep a mapping between view-models and fragments, such that we can instantiate fragments based on the view-model class:

		// map between view-model and fragment which creates and shows the fragment based on the view-model type
private Dictionary<Type, Type> vmTypeFragmentTypeDictionary = new Dictionary<Type, Type>();

public bool ShowFragment(FragmentManager fragmentManager, MvxViewModelRequest request, bool addToBackstack)
{
Type fragmentType;
if (vmTypeFragmentTypeDictionary.TryGetValue (request.ViewModelType, out fragmentType)) {

// create view model
var loaderService = Mvx.Resolve<IMvxViewModelLoader> ();
var viewModel = loaderService.LoadViewModel (request, null /* saved state */);

// create fragment view and bind it to the view model
var fragmentView = Activator.CreateInstance (fragmentType) as MvxFragment;
fragmentView.ViewModel = viewModel;

// load fragment into view
var ft = fragmentManager.BeginTransaction ();
ft.Replace (Resource.Id.fragmentHost, fragmentView);
if (addToBackstack) {
ft.AddToBackStack (null);
}
ft.Commit ();
return true;
}
else {
return false;
}
}

public void Register(Type viewModelType, Type fragmentType)
{
vmTypeFragmentTypeDictionary[viewModelType] = fragmentType;
}


To make it reusable, we can even create a base class with this code, called for example MvxActivityFragmentHost and have the activity derive from it:

public class MvxActivityFragmentHost : MvxFragment, IFragmentHost
{
...
}

public class MainView : MvxActivityFragmentHost
{
....
}