Solutions to common issues working with Xamarin

Solutions to common issues working with Xamarin

Hi everyone, I have been thinking to write this article for quite a while now. Being continuously working on Xamarin in-depth and proposing solutions to the clients, you face some common problems. This article focuses on the resolving these issues faced by Xamarin developers and help them for smooth Xamarin Solution.

You may find some problems related to core development and some related to the solution proposal. If you feel anything is missing and needs to be present in this list, feel free to drop a comment and I would be really happy to add it to this list with mentioner’s name.

 

Problem 1: It may happen that you are unable to access Android SDK as it was installed in the administrator folder and was not having required permission to access it.

Solution: User must have Admin rights to access that folder.

Problem 2: Unable to provide internet access to emulator even after changing the proxy settings.

Solution: You can try to increase the target API level and install the ARM EABI v7a system image for it and then change the proxy settings of the emulator.

Problem 3: Emulator starts and stops immediately. Failed to create Context 0x3005 for an emulator.

Solution: It requires to update the Intel GPU driver.

Problem 4: Unable to use the local database in Xamarin app as it cannot find the physical path of the database file.

Solution:

Xamarin creates its own database in its APK file as follows:

Set absolute path for folder creation like:

String path = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);

String Applicationfolderpath = System.IO.Path.Combine (path,”Database1”);

Where Database1 is the name of the newly created database

Create the folder path like below:

System.IO.Directory.CreateDirectory (Applicationfolderpath);

Create database in set path like below:

String databasefilename = System.IO.Path.Combine (Applicationfolderpath,”User.db3”);

Where User.db3 is the name of the database file.

SQLite.SQLite3.Config (SQLite.SQLite3.ConfigOption.Serialized);

SQLite.SQLiteConnection Db = null;

Db = new SQLite.SQLiteConnection (databasefilename);

Var res = db.CreateTable<Classname> ();

Problem 5: Unable to provide WCF service on another machine

Solution: Provide service on other machine using IIS manager.

Problem 6: What is the difference between Page and Activity?

Solution:   

  • Page
  1. It is a visual that occupies most or all of the screen and contains a single child.
  2. A Xamarin.Forms.Page represents a View Controller in iOS or a page in Windows iPhone.
  3. On Android each page takes up the screen like an Activity but Xamarin.Forms Pages are not Activity.
  • Activity
  1. Activity represents a screen in an application.
  2. Android Application Architecture doesn’t have a single application instance so there is an aggregation of one or more activities.

 Problem 7: Mac machine is required to develop iOS app on Xamarin

Solution:

developer.xamarin.com/guides/ios/getting_started/hello,_iOS/hello,iOS_quickstart/

Problem 8: Visual Studio for showing option to select an emulator to debug and run iOS simulator on Windows only

Solution:

  1. Build the application
  2. Select the emulator from Device Manager
  3. Start the Emulator
  4. Click on the Start button of the Visual Studio
  5. It will then display error as “deployment failed due to fast dev process”
  6. Go to Project=>Properties=>Android application then uncheck the “use FastDev process”
  7. Again click on start
  8. With the latest updates for Xamarin.iOS, you can run iOS simulator on Windows Machine only. To enable this, go to settings and check run emulator on windows option

Problem 9: How to develop iOS app using Visual Studio.

Solution:

Required Components

For Mac

  • Latest iOS SDK
  • XCode latest version
  • Mac OS X 10.9.4 + or 10.10 above

For Windows

  • Windows 7 or higher
  • Visual Studio 2012 Professional or higher
  • Xamarin’s plugin for Visual Studio

Create Xamarin.forms blank project in Visual Studio. It will then ask for Mac IP or Mac name. Provide the required information and connect it to the Mac machine.

 Problem 10: How to add an application on Google Play Store.

Solution:

  1. Create the APK file of the application
  2. Navigate to Google Developer Site
  3. Logging into Google Developer Console. If you do not have an account create a new account. The last step in the process of creating a developer account is paying a one-time $25 developer fee using a credit or debit card
  4. Click on the button Add New Application
  5. Initialize APK Upload. Select correct language and write the name of the app as you wish it to appear in the google play store. Then select upload APK button
  6. Click on Upload your first APK to Production button centered on your screen
  7. Select and upload APK file
  8. Navigate to the store listing tab on the left side and write the description in the description box which will appear on the google play store
  9. The next step in the store listing tab is to add screenshots
  10. Then add a store listing icon
  11. The final step on the “Store Listing” page is filling out the categorization, contact details, and privacy policy, located below the icon selection area. Fill out the required fields as you wish and hit save. The “store listing” tab should now have a green check-mark next to it
  12. Click on Price and Distribution tab where you will select paid or free, distribution countries, and check the boxes saying that your app complies with the guidelines and the US export laws. Click on save button
  13. Once all the three tab at the left have a green mark you are then able to select “Publish this app” from the “Ready to Publish” drop-down menu in the top right corner of the developer console. A confirmation bar should appear at the top, stating that your app will appear in the Google Play store in just a few hours

Problem 11: On creating new Xamarin.forms project in Visual Studio it is giving the error “Could not add all required packages to the project.

The following packages failed to install from ‘C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 12.0\COMMON7\IDE\EXTENSIONS\XAMARIN\XAMARIN\3.9.236.0\Packages'”:

Xamarin.Forms.1.3.1.6296: Could not install package ‘Xamarin.Forms 1.3.1.6296’. You are trying to install this package into a project that targets ‘Xamarin.iOS,Version=v1.0’, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author”.

Solution:

  1. To solve this issue open your visual studio go to tools -> Extensions & Updates -> select updates -> select visual studio gallery then update your NuGet package manager
  2. Update the Visual Studio Version if the updates for it are available. Update it using the same above procedure as for updating NuGet manager

Problem 12: On creating new Xamarin.forms project in Xamarin Studio it is giving the error “Could not add all the required packages to the project”.

Solution:

  1. Add the xamarin.forms packages
  2. Install PCL libraries. Following is the link to download PCL libraries https://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-    00cb3caf4981/
  3. After this reinstall or repair xamarin
  4. In Xamarin Studio, right click on the project select options=>build=>general then change the target framework to that profile which is installed.

Problem 13: Windows application prerequisites and steps.

Solution: Prerequisites for Windows app

  1. You need the latest version of windows
  2. Microsoft Visual Studio: Microsoft Visual Studio 2013 helps you design, code, test, and debug your apps
  3. If you don’t already have Visual Studio 2013, install the free Visual Studio Express 2013 for Windows

If you already have Visual Studio Professional 2013 or a higher edition, get Update 3 for Visual Studio 2013 to add the Windows Phone development tools and other enhancements to your existing installation:

(Optional) Register your Windows phone device for development:

It’s important to test your Windows Phone Store apps on a real phone. Before you can deploy apps to your Windows Phone, you have to register the phone for development.

  1. Connect your phone to your PC.
  2. Run the Developer Phone Registration tool (phonereg.exe).

Register as an app developer:

Now that you’ve installed the tools you need, you need a developer license or a developer account. A developer license is free and lets you develop Store apps, but you need a developer account to sell your apps in the Windows Store and the Windows Phone Store.

  1. To get a developer license, just launch Visual Studio. You’ll be prompted to get a developer license
  2. To get a developer account, go to the Signup

Steps to create a Project:

  1. Make sure you’ve downloaded and installed the Windows Phone SDK
  2. Launch Visual Studio from the Windows Start screen. If the Registration window appears, you can register the product, or you can temporarily dismiss the prompt
  3. Create a new project by selecting the FILE | New | Project menu commandIn the New Project window, expand the installed Visual C# or Visual Basic templates, and then expand Store Apps. Select the Windows Phone Apps
  4. In the list of Windows Phone Apps templates, select the Blank App (Windows Phone)
  5. At the bottom of the New Project window, type MiniBrowser as the Name of the project
  6. Click OK. The new project is created and opens in Visual Studio
  7. https://dev.windows.com/en-us/getstarted#1

Notes

Installation instructions for Xamarin

  • Install Java JDK
  • Install Android SDK (Including all API levels)
  • Visual Studio 2012 Professional
  • Install Xamarin Studio

If Xamarin Studio is installed before Visual Studio then Xamarin needs to be reinstalled

Problem 13: When your codebase is on TFS and linked with the local workspace present on Windows machine. How to design UI for iOS apps easily.

Solution:

  1. It is recommended that you must connect with Mac agent and share your codebase for mac machine user. This will enable that same codebase is present on both machines
  2. Once the codebase is shared, select the main folder and uncheck read-only option if present by any chance
  3. Open the project in Xamarin Studio on Mac
  4. Right click on the Storyboard file inside Xamarin Studio and open it with XCode

The support by iOS designer in Xamarin is not much fast yet and you are always recommended to use XCode to design your iOS app. Once the changes in XCode are done, they are reflected in your Xamarin project

Problem 14: How do I share code in Xamarin Native apps?

Solution:

You can create .NET class library if you want to share code between iOS and Android apps. It is recommended for a normal mobile app to consume to class libraries like Common Code and Service Connection Code.

If you wish to extend support to Windows Phone as well, then .NET class library does not work there. You are advised to use Portable Class Library (PCL). This library can be used in iOS, Android and Windows Phone Xamarin apps.

Problem 15: What are different types of Xamarin apps and which one should I propose to my client?

Solution:

  1. If you wish to go native, you can go for Xamarin.iOS, Xamarin.Android and Xamarin.Mac
  2. If you wish to go cross platform with UI sharing, you can go with Xamarin.Forms option
  3. If you wish to develop windows app which works on Windows Phone, Windows 10 then go with Xamarin UWP (Universal Windows Platform) app
  4. Still there are lot of different templates being created as the new versions of Xamarin.Studio are coming up

These were some common problems faced by developers. I am working on Xamarin in depth and would keep on posting some interesting information on this blog regularly. If you have any questions, you can post in comments section.

Thank you.

 

 

 

 

Continous Integration for iOS and Android apps using Xamarin

Continous Integration for iOS and Android apps using Xamarin

Why Continuous Integration?

In the complex projects, developers and testers (also called as full stack developers in agile methodologies) simultaneously work on their modules. You can imagine several products are getting created in parallel leading to huge amount of integration work in the regular intervals of time. A single change in the development code needs to be checked into the code repository. This delta is then integrated, tested to make sure that the changes didn’t add any defects, hence a stable product, stable build.

This process is called as Continuous Integration (CI). It lets you find defects in the least possible time in the development cycle from the time of their occurrence. It helps to identify code coverage and facilitate the stability of the release build.

Pre-requisites for Xamarin CI using TFS

For this post, we are going to concentrate on using TFS On-Premise Server for Continuous Integration. When using TFS (i.e. configuring Build.vNext), first steps is to log in and configure the agent pools. Agent Pool is called as a collection of several build agents (i.e. services running on the operating system / build server and listens for builds from TFS). This agent pool needs to be created first and then the build agents. It is to be noted that build agents should be free and should also meet the demands of the particular build requested. For example, an iOS build can only be requested to build agent which has Xamarin.iOS capabilities, similarly for Android also. The choice of how many pools or agents you need totally depends on the different projects/purposes.

For the management of the agent pools, you should know below account groups:

  1. Administrator Account Group
  2. Service Account Group

Administrator account group member is allowed to add, remove and configure agents in the pool whereas Service account group only let you run builds.

At first, you need to add your user account in Agent Pool Administrators group by logging into TFS. Later, you can create any number of Agent Pool Service Accounts for the production environment.

CI for Xamarin.Android

This section will address the complete steps required to perform the continuous integration process for Xamarin.Android Native Apps. This will also consist of the process to add the windows build agent to build the source code for a project.

Create and Configure Xamarin.Android Build Agent on Windows

This section will provide you step by step details for creation of Xamarin.Android Build Agent on Windows.

When Build agent receives a build from TFS Server, it executes the steps in the build definition. Currently, you have to use a Windows build agent to build Windows/Windows Phone apps and Xamarin.Android apps and a cross-platform build agent on the Mac to build Xamarin.iOS projects. In the future, the cross-platform build agent should also be able to build Xamarin.Android projects too.

This section will provide you step by step details about different items to be installed on your machine to satisfy it as a Windows Build Agent:

Install below software’s on the build machine.

  1. Xamarin (Latest Version)
  2. Java JDK 1.7
  3. Android SDK (Latest Target Version)

Please note that ANDROID_HOME and JAVA_HOME environment variables should be set to locate the required components and advertise them as capabilities of a build agent. Once done, you need to download the agent from TFS, configure and run the build agent.

For configuration of build agent, you need to create a BuildAgents (or any name with which it makes sense) folder in the C:\ drive to store all of the agents in one machine. You can create AndroidAgent folder in it and extract the agent zip file downloaded from TFS. Once done, open Windows PowerShell as Administrator and run .\ConfigureAgent.ps1, press enter. Fill in the required details for the agent and you are done!

The Windows Build Agent is successfully created with the Xamarin.Android Capabilities. The agent starts as a service in Windows. If you wish to start/stop the agent manually, you can stop the service.

Create Xamarin.Android Build Definition

This section will provide you step by step analysis on the creation of a Build Definition for Xamarin.Android project. It uses Windows Build Agent for creation and deployment of the builds.

  1. Navigate to TFS Team project Home page, click on the Build tab and click Add button to add new build definition
  2. Select Xamarin.Android template and click OK. This will create a new Build Definition with some pre-defined steps

There are different tabs associated with the creation of build definition like Build, Options, Repository, Variables, Triggers, General, Retention and History.

You can modify build steps to build particular Android project present in the TFS Code Repository, sign APK file as a build artifact to TFS. Some more steps are as follows:

  1. For the Target input field, type: PackageForAndroid (this will trigger the build target that will create .APK package once the project is built)
  2. Add Android Signing builds step, check the ‘Sign the APK’ option and enter valid Keystore details as required
  3. Add Publish Build Artifacts step and put ‘Path to Publish’ as $(build.binariesdirectory)\$(BuildConfiguration)

Please note that BuildConfiguration is the variable created under Variables tab which will be used runtime.

  1. Check the Continuous Integration (CI) in Triggers tab or you can schedule the trigger as per your project requirements
  2. Select the Default agent Queue/Pool which is already created
  3. Save the changes and click OK

Once the build definition is created, you can queue new build for the definition and if everything has been configured correctly, your build definition should finish with the message “Build Succeeded”. If failure, you can check logs about the exact cause behind the issue.

CI for Xamarin.iOS

This section will address the complete steps required to perform the continuous integration process for Xamarin.iOS Native Apps. This will also consist of the process to add the windows build agent to build the source code for the project.

Create and Configure Xamarin.iOS Build Agent on Mac OS

This section covers the creation of the cross-platform build agent and configuration for Xamarin.iOS project. The cross-platform agent has been recently introduced along with Visual Studio 2015. This agent can only be configured and executed on Mac OS X and Linux Machines for build definitions created using the Build.vNext tools included in TFS 2015.

Please note that the cross-platform agents are written in Node.js and you can install/update/run them using Node.js.

This section will provide you step by step details about different items to be installed on your machine to satisfy it as a Mac OS X Build Agent:

  1. Install below software’s on the build machine:
  • Xamarin
  • Xcode
  • js
  1. The next step is to install the VSO Agent using the following command in Mac OS X Terminal:

sudo npm install vsoagent-installer –g

If you think that this will create an agent for you, then it is not. This is just going to create a copy which will be used later to create actual agents.

  1. To provide permissions for Node Package Manager, please run below command:

sudo chown –R $USER ~/.npm

  1. It is recommended to create a BuildAgents folder in the root of your Macintosh HD partition to store all of the agents you wish to create (you can have multiple agents running on the same machine). Inside this BuildAgents folder, create another folder called Agent1 (or whatever you want to name your build agent). You can create these folders using the Finder
  2. Install the VSO agent in the folder you created and run ./config.sh file to start configuration
  3. The console will ask following questions to configure the cross-platform build agent on Mac OS X:
    1. Enter alternative username: You need to enter the username of the account that was configured to administer build agents earlier
    2. Enter alternative password: Enter the corresponding password for the account chosen in the previous step
    3. Enter server URL: You need to enter the TFS URL
    4. Enter Authentication Type: Click enter to continue
    5. Enter agent name: You can use whatever name you want to here. This name is to identify agent in the agent pool
    6. Enter agent pool name: This allows you to add an agent to a specific pool which is already available in TFS. If you do not pass this value, the agent will be added to the default pool

The cross-platform build agent is now successfully configured with Xamarin.iOS capabilities. Run ./run.sh to start the agent as a service. Do not close the Terminal window until you want to stop the build agent. You can stop the build agent by pressing Ctrl-C.

Create Xamarin.iOS Build Definition

This section will provide you step by step analysis on the creation of a Build Definition for Xamarin.iOS project. It uses Mac OS X Build Agent for creation and deployment of the builds:

  1. Navigate to TFS Team project Home page, click on the Build tab and click Add button to add new build definition
  2. Select Xamarin.iOS template and click OK. This will create a new Build Definition with some pre-defined steps

Most of the steps mentioned in the build definition for Android are pretty similar.

You can modify build steps to build particular iOS project present in the TFS Code Repository. You need to modify Xamarin.iOS build steps so the build agent will build the solution and upload the .ipa package that is created as a build artifact to TFS. Some more steps are as follows:

  1. Click on ‘Add Build Step…’ and select ‘Publish Build Artifacts’ item
  2. Looking at the new ‘Publish Build Artifacts’ build step, In the ‘Copy Root’ input field type: $(build.binariesdirectory)\$(BuildConfiguration)
  3. Check the Continuous Integration (CI) in Triggers tab or you can schedule the trigger as per your project requirements
  4. Select the Default agent Queue/Pool which is already created
  5. Save the changes and click OK

Once the build definition is created, you can queue new build for the definition and if everything has been configured correctly, your build definition should finish with the message “Build Succeeded”. If failure, you can check logs about the exact cause behind the issue.

This way your Continous Integration System for both Android and iOS apps using Xamarin can be set up. I will cover more details about Xamarin in coming posts.

This post basically tells you about the fastest way to integrate social platforms like Facebook, Twitter and Google+ in iOS.

FACEBOOK

There are two ways we can integrate Facebook with iOS.

  • Using Facebook SDK
    1. Facebook SDK is only for Facebook and can perform various tasks related to Facebook only. It is reasonably easy to integrate Facebook into any iOS app and much more powerful than the Social Framework. Another benefit is that support goes back to iOS 4.3
    2. SDK offers several features provided in FB and several of them can be integrated in iOS app. For example: you can use different dialogs to post on your friend’s wall and can run FQL queries and graph operations too. Not possible using social framework
    3. User is asked for authentication first, once the user is authorized then he can use Facebook APIs in SDK and perform operations
  • Using Social Framework in iOS
    1. The Social Framework is designed for simple, site-neutral data manipulation. Examples might include getting the user’s ‘activity feed’ or posting a new status or photo
    2. Social framework doesn’t means only FaceBook. It is larger than only Facebook, it includes Facebook, Twitter, Sina Weibo and LinkedIn also
    3. Social Framework supports iOS 6.0 onwards
    4. If your app supports iOS 6.1 and above, Social Framework is highly recommended
    5. iOS specific native dialogs are open to post anything on social platforms
    6. User can configure Facebook and Twitter app already integrated in iOS and then can direct share content from iOS app on the configured accounts

Single SLComposeViewController helps you to share multiple social platforms. The only thing that you need to do is to change the type of social account and you are through.

Note: According to Apple, 89% of the iOS devices are already on iOS 7. The percentage will be higher if iOS 6 is included. So, if the requirements for the usage of Social Platform features are minimum (like share), then Social Framework is the way to go!

TWITTER

For Twitter, the best way to integrate is to use Social Framework. In the old versions of iOS, twitter framework was present but that is replaced by social framework. If the accounts are not configured in the settings section, then you will see below screen and will ultimately take you to settings to configure Twitter or Facebook.

 

Login

GOOGLE+

As there is no integrated support for Google+ in iOS, it is mandatory to use Google+ SDK for iOS for integration.

You can start your integration journey for Google+ from here .

If you have questions on the integration of social platforms, then you can ask in the comments section. Enjoy integrating!

– See more at: http://atozgeek.com/integrating-social-platforms-in-ios/#sthash.9Peg6rai.dpuf