April 17, 2014

Easy In App Purchases for iOS

By

Xamarin.iOS makes it easy to use Apple’s StoreKit API to include In App Purchasing in our iOS mobile applications. However, there’s quite a bit of repetitive code that has to be created for every app that includes it.

That’s where the new Xamarin.InAppPurchase component can help!

Xamarin.InAppPurchase in Use

Using Xamarin.InAppPurchase, we can easily and quickly add iTunes App Store In App Purchasing of products, features or subscriptions to our iOS mobile applications. All without any of the repetitive code required when calling StoreKit directly.

Working with Product Identifiers

By decorating our Product Identifiers with specific keywords, we can have Xamarin.InAppPuchase automatically handle things such as the product type, subscription duration, consumable quantities and downloading of hosted content from the iTunes App Store.

For example, we can use product.nonconsumable to define a non-consumable product, gold.coins.consumable_x25 a consumable package of 25 gold coins, magazine.subscription.duration1month an auto renewing monthly subscription and antivirus.nonrenewingsubscription.duration6months a non-renewing six month subscription.

So let’s take a look at how easy it is to get a list of available In App Products from the iTunes App Store with Xamarin.InAppPurchase by using the following code:

using Xamarin.InAppPurchase;
using Xamarin.InAppPurchase.Utilities;
...
public InAppPurchaseManager PurchaseManager = new InAppPurchaseManager ();
...
// Ask the iTunes App Store to return information about available In App Products for sale
PurchaseManager.QueryInventory (new string[] {
"product.nonconsumable",
"gold.coins.consumable_x25",
"newsletter.freesubscription",
"magazine.subscription.duration1month",
"antivirus.nonrenewingsubscription.duration6months",
"content.nonconsumable.downloadable",
});

After this code runs, the PurchaseManager will contain a list of any valid products with all of the localized information read from the iTunes App Store, ready for us to display to the end user.

When the user is ready to buy a given item, we can use the following code to start the purchase process with the iTunes App Store:

// Ask iTunes App Store to purchase product
PurchaseManager.BuyProduct (Product);

We can then monitor several of the events exposed by the PurchaseManager to update the app’s UI or activate new content or features. At any time we can ask if a given product has been purchased using:

// Was the feature purchased?
if (PurchaseManager.ProductPurchased("my.nonconsumabe.feature")) {
...
}

In addition, Xamarin.InAppPurchase will automatically track our consumable product quantities and subscription expiration dates and provides methods to work with them easily.

Secure Automatic Persistence

The Xamarin.InAppPurchase component includes ways to automatically and securely persist the user’s purchased products. This information can be stored either to the app’s user preferences, a local file, iCloud, or our own custom persistence methods.

Now, let’s look at restoring previous purchase history that has been automatically saved by the Xamarin.InAppPurchase component. We’ll add the following code before our call to QueryInventory above:

// Setup automatic purchase persistence and load any previous purchases
PurchaseManager.automaticPersistenceType = InAppPurchasePersistenceType.LocalFile;
PurchaseManager.PersistenceFilename = "AtomicData";
PurchaseManager.shuffleProductsOnPersistence = false;
PurchaseManager.RestoreProducts ();

With this code in place the Xamarin.InAppPurchase component automatically saves any changes to the user’s purchased products and restores that history when our iOS app starts.

Simulating the iTunes App Store

There are several situations that can arise when working with In App Purchases, many of which can be hard to test for. The Xamarin.InAppPurchase component provides the ability to simulate interaction with the iTunes App Store so we can fully test out our application. This helps to ensure and provide a smooth, issue free In App Purchase experience for our users.

Xamarin.InAppPurchase running in simulation

When running in the simulation mode, decorate your product identifiers with specific keywords to test such things as invalid product IDs, failed purchases, hosted content download, etc. You can even test products before they are added to iTunes Connect and can test In App Purchases inside the iOS Simulator on a Mac.

Let’s look at how easy it is to run the Xamarin.InAppPurchase component in the simulation mode. We’ll modify our code above to look like the following:

// Initialize the purchase manager
PurchaseManager.simulateiTunesAppStore = true;
...
// Ask the iTunes App Store to return information about available In App Products for sale
PurchaseManager.QueryInventory (new string[] {
    "product.nonconsumable",
    "feature.nonconsumable",
    "feature.nonconsumable.fail",
    "gold.coins.consumable_x25",
    "gold.coins.consumable_x50",
    "gold.coins.consumable_x100",
    "newsletter.freesubscription",
    "magazine.subscription.duration1month",
    "antivirus.nonrenewingsubscription.duration6months",
    "antivirus.nonrenewingsubscription.duration1year",
    "product.nonconsumable.invalid",
    "content.nonconsumable.downloadable",
    "content.nonconsumable.downloadfail",
    "content.nonconsumable.downloadupdate"
});
...
// Setup the list of simulated purchases to restore when doing a simulated restore of purchases
// from the iTunes App Store
PurchaseManager.simulatedRestoredPurchaseProducts = "product.nonconsumable,antivirus.nonrenewingsubscription.duration6months,content.nonconsumable.downloadable";

Now we can run our iOS mobile app and test interactions such as the purchasing of a product failing, downloading of hosted content failing halfway through, or any other event that can happen with In App Purchases.

For more details about the Xamarin.InAppPurchase component and to get a full demo iOS application built using it on the Xamarin Component Store.

TwitterFacebookGoogle+LinkedInEmail
April 16, 2014

Epic Evolve Sessions

By

Last year, Xamarin Evolve 2013 hosted over 80 training and conference sessions on a vast array of topics. This year will be even bigger. Join us in October for Xamarin Evolve 2014, with a full extra day to make room for even more amazing sessions. If you didn’t get to attend last years conference or just want to catch up on the great sessions that were presented you can watch online right now. Here are just a few of our favorites from Xamarin Evolve 2013 that you don’t want to miss:

How C# Saved My Marriage, Enhanced My Career, and Made Me an Inch Taller

with Scott Hanselman

Multiplatformism: Lessons Learned Bringing Bastion to Six New Platforms

with Andrew Wang

Getting the Most from Xamarin Studio

with Michael Hutchinson

Push Notifications with PushSharp

with Jonathan Dick

Buttons are a Hack

with Josh Clark

If you enjoyed these sessions, we have all the videos from Xamarin Evolve 2013 online. And you definitely don’t want to miss Xamarin Evolve 2014 in October — two full days of training from Xamarin University followed by three days live sessions from Xamarin and Industry experts. Don’t wait, Register for Evolve 2014 today!

TwitterFacebookGoogle+LinkedInEmail
April 15, 2014

C# Meetups featuring Google Glass, Bluetooth LE & More

By

Community growth continues to be explosive with new Mobile .NET developer groups starting all over the world. This month, we have already seen the inaugural meeting for a new group in Paris, France, and we are pleased to announced two new groups in Tampa Bay, FL and Madison, WI! So, April isn’t over yet as it packed full meetups happing everywhere on vast topics from MvvmCross, Intro to Xamarin, and Bluetooth LE!

Mobile .NET Developers Meetups April 2014

Here are some upcoming Xamarin developer group meetups:

New Group: Tampa Bay Xamarin User Group usa flag

  • Tampa Bay, FL: Wednesday, April 16th 7:00PM
  • First TBXUG Meeting – Intro to Xamarin

Boston Mobile C# Developers Group usa flag

  • Cambridge, MA: Thursday, April 17th 6:30PM
  • Developing for Google GlassChris Hardy, Xamarin

.NET BC canada flag

  • Vancouver, BC: Tuesday, April 22nd 6:00PM
  • Cross Platform Mobile Development for the C# Developer with XamarinJames Montemagno, Xamarin

New Group: Madison Mobile .NET Developers Group us

  • Madison, WI: Wednesday, April 23rd 5:30PM
  • Introduction to Xamarin

Dutch Mobile .NET Developers Group Netherlands flag

  • Aalsmeer, Netherlands: Thursday, April 24th 6:00PM
  • Responsive design and Bluetooth LE

Vancouver .NET Mobile Developers Group Canada

  • Vancouver, BC: Thursday, April 24th 6:30PM
  • Mobile Development Techniques using C# and MvvMCross

Detroit Mobile .Net Users Group us

  • Southfield, MI: Monday, April 28th 6:00PM
  • Build / Xamarin Recap, Humanitarian Toolbox

Sydney Mobile .Net Developers Group Australia Flag

  • Sydney, Australia: Tuesday, April 29th 6:30PM
  • Build 2014 and Cross Platform Visual State Management

If you don’t see your city listed above, don’t worry as new events and developer group meetups are being added frequently on the Xamarin Events forum.

If you are interested in starting a developer group in your city we are here to help you get started. We have tips and trick on staring a developer group, a brand new introduction to Xamarin slide deck, and of course our community sponsorship program to get you on your way. We also want to hear from you so please feel free to send us an email or tweet @XamarinHQ so we can help spread the word and continue to grow the Xamarin community.

TwitterFacebookGoogle+LinkedInEmail
April 10, 2014

Xamarin Evolve 2014 Registration Now Open

By

Untitled-1We are very excited to announce the opening of registration for Xamarin Evolve 2014, our worldwide developer conference! Join us and fellow developers from October 6-10 in Atlanta, Georgia for the largest cross-platform mobile development event of the year!

Even Bigger!

This year, we’re doubling capacity and adding an extra day of conference sessions to make room for more great content covering mobile development with C#, Visual Studio, and Xamarin Test Cloud. Evolve sessions will cover a range of topics, including game-changing Xamarin product announcements by Nat and Miguel, best practices in mobile user experience design, advanced cross-platform techniques, memory management, real world app case studies from leading companies, secure data integration, lightning talks and so much more.

World-Class Training

Xamarin University LogoAttend two days of training from Xamarin University spanning all topics from mobile development fundamentals to advanced areas such as security and code-sharing strategies.

Learn from Mobile Experts

Over the course of three days you will have the chance to attend an immense amount of live sessions covering all areas of mobile development by industry leaders and experts. The entire Xamarin engineering team will be on-hand for one-on-one mobile coaching to answer questions, debug code, and provide advance.

Xamarin Evolve Welcomes Top Sponsor, Microsoft

We are also delighted to announce today that Microsoft will be joining Evolve 2014 again as our sole Platinum Sponsor. In recent months, Xamarin and Microsoft have partnered globally to help developers build great apps with C#, Visual Studio, and Portable Class Libraries, and this past week, Xamarin joined with Microsoft to launch the .NET Foundation. Together, Xamarin and Microsoft bring C# to 2.6 billion devices, and together we will make Xamarin Evolve 2014 an event you won’t forget.

MicrosoftSponsorEvolve2014

Registration Now Open

Tickets for Xamarin Evolve 2014 our second worldwide developer conference, are officially available for purchase today.

Register Now

Team Xamarin

TwitterFacebookGoogle+LinkedInEmail
April 9, 2014

Swipe To Refresh added to Android

By

Swipe-to-Refresh animation

Google recently released an update to the Android support library bringing in an exciting new standard implementation of a pattern that they have been progressively introducing through applications such as GMail or Google Now.

This pattern, called swipe-to-refresh, uses an upward pull user feedback to inform the app that a data refresh is requested. This complete another common pattern, infinite scrolling, that instead use a downward movement to load more data.

We have updated our component to expose the new SwipeRefreshLayout class that implements the pattern. To make use of it, simply wrap in your existing Xamarin.Android app layout.

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refresher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

Setting up the SwipeRefreshLayout instance is pretty easy from code. You simply have to define a color scheme and a callback that is invoked when an update operation is requested by the user.

refresher = FindViewById<SwipeRefreshLayout> (Resource.Id.refresher);
refresher.SetColorScheme (Resource.Color.xam_dark_blue,
                          Resource.Color.xam_purple,
                          Resource.Color.xam_gray,
                          Resource.Color.xam_green);
refresher.Refresh += async delegate {
	await forum.FetchItems (clear: true);
	refresher.Refreshing = false;
};

The color scheme can be any set of color or the same color. In any case, the first parameter is also used as the initial feedback bar background.

We have also added a dedicated sample that shows how this code can be integrated in a complete scenario, including a complex layout utilizing fragments.

TwitterFacebookGoogle+LinkedInEmail

OpenSSL “Heartbleed” Update

By

You might have read by now that details of the “Heartbleed” OpenSSL vulnerability were published earlier this week. For those that don’t already know, OpenSSL is a popular, general purpose cryptography library in wide use across the internet. Like nearly every company on the internet, we immediately took audit of our systems, and uncovered some systems using a vulnerable version of OpenSSL. We immediately began updating all Xamarin services that were affected, and as of Tuesday afternoon, no Xamarin systems are subject to this vulnerability.

We have also reissued all HTTPS certificates for all xamarin.com domains with new encryption keys, and have had the previous versions revoked by our issuer, which was also completed Tuesday evening. At this point, we believe that it is extremely unlikely that any of our systems were compromised before we patched OpenSSL; however, for an additional layer of security, we recommend that you take the following steps to safeguard your Xamarin account — especially if you used the same password for your Xamarin account as on other accounts hosted elsewhere:

  1. Sign out of your Xamarin account and then sign back in (you’ll want to do this in the IDE as well)
  2. Change your Xamarin account password

At Xamarin we are committed to your security and privacy. We are continuing to track the issue closely and will keep you up to date if anything new is discovered. For more information on this OpenSSL vulnerability visit www.heartbleed.com

TwitterFacebookGoogle+LinkedInEmail

C#, iOS, and Android Take Center Stage at Build

By

It was an exciting week for .NET developers with a slew of announcements at this years Build conference in San Francisco.

Microsoft Build Sign

The most anticipated Build session of the week hands down was Miguel’s “Go Mobile with C# and Xamarin”, where over 1,500 developers packed the main hall for a chance to see Miguel talk on the transformation of mobile development. The audience loves seeing his live coding session where he used iOS’ CoreImage and Twitter integration to add Sepia filter to an image and tweet it out to the world!

Packed house at Miguel de Icaza's build talk

Continuing to wow the crowd, Miguel unveiled our brand new Xamarin T-shirt Store app enabling developers to try out Xamarin to build their first app to order their very own limited edition C# shirt that he was rocking on stage.

A major highlight of the week was when C#’s lead architect, Anders Hejlsberg, open sourced the .NET Compiler Platform, Roslyn, live on stage. This marked a major milestone allowing developers to gain access to a plethora of information that compilers have about the code we write each day. Immediately after Anders open sourced Roslyn, our own Miguel de Icaza hit the stage to showcase the power of an open sourced Roslyn by compiling code with a modified version of Roslyn running on Mono inside of Xamarin Studio on a Mac!

If Roslyn wasn’t enough, Xamarin took part in the official launch of the .NET Foundation. This new foundation will foster open development, collaboration and community engagement on the .NET platform. Xamarin initial contribution consists of six open source .NET Projects including Mailkit, Mimekit, System.Drawing, and more! We are excited to join other community leaders including Laurent Bugnion, Niels Hartvig, Paul Betts, and other to aid in advancing .NET open source projects..NET Foundation

The excitement for Xamarin, C#, and mobile apps continued throughout the week with an always packed Xamarin booth. Thousands of developers got to chat and get a hands on demo of creating beautiful Android and iOS apps in C#.

Packed Xamarin booth at //Build/

Build wouldn’t be complete without an epic Xamarin party, and this year was no different. Our friends .NET joined us to co-host a mega party in downtown San Francisco. Thousands of developers packed in to get face time with Nat, Miguel, Joseph, and the rest of the Xamarin crew at a party that is not to be forgotten! We want to thank everyone that was able to stop by the party and booth.

Xamarin Build Event at Mezzanine

If you thought that Build was epic, you will absolutely want to join us for Xamarin Evolve, the largest mobile developer conference of the year. Tickets go on sale on April 10th, so make sure to visit the official Evolve website to sign up for details.

TwitterFacebookGoogle+LinkedInEmail
April 8, 2014

Native Printing with Android

By

The latest version of Android, KitKat, adds long-awaited printing support to the platform. Android applications can now provide low-level control of print jobs, in addition to print integration for applications incorporating web content. This makes KitKat particularly well-suited to hybrid applications built with Xamarin using Razor templates.

Android KitKat Printing

Printing with a WebView

First let’s look at the easy to use print support offered through the WebView control.

Printing requires 3 steps:

  1. In an Activity, get a reference to the PrintManager system service.
  2. Create an instance of a class that inherits from PrintDocumentAdapter.
  3. Call the PrintManager‘s Print method, passing it the print adapter.

The PrintDocumentAdapter is an abstract class that provides a contract to implement for supplying print content. For web content, the WebView class includes a PrintDocumentAdapter, making printing HTML from a WebView incredibly easy to do:

The following example shows a WebView built using the Android WebView template, which uses Razor as an HTML templating engine:

Android Printing from a Xamarin Razor Template

Although printing can be done using any WebView, integrating printing with Razor and this project template makes it really easy to build a hybrid application that can leverage native features such as KitKat printing.

Printing from the WebView with C# is as simple as adding these 2 lines of code in the activity:

var printMgr = (PrintManager)GetSystemService(Context.PrintService);
printMgr.Print("Razor HMTL Hybrid", webView.CreatePrintDocumentAdapter(), null);

When we call Print Android presents a system dialog, allowing the user to choose the print destination, as shown below:

Android Print Dialog

Custom Print Adapter

To print from native Android views, taking low-level control of the print layout, we can implement our own PrintDocumentAdapter.

For example, let’s say we would like to print the layout from the following screen:

Android Print Activity Low Level

In our implementation of PrintDocumentAdapter, the required methods to implement are:

  • OnLayout – Allows laying out print content based on the PrintAttributes.
  • OnWrite – Allows writing a PDF file with content to print.

In OnLayout, we create a PrintDocumentInfo instance, which contains metadata about the document being printed.

public override void OnLayout (PrintAttributes oldAttributes, PrintAttributes newAttributes,
                               CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
  document = new PrintedPdfDocument (context, newAttributes);
  CalculateScale (newAttributes);
  var printInfo = new PrintDocumentInfo
    .Builder ("MyPrint.pdf")
    .SetContentType (PrintContentType.Document)
    .SetPageCount (1)
    .Build ();
  callback.OnLayoutFinished (printInfo, true);
}

In OnWrite, we implement the code to draw printed content and write it to the output stream to be printed.

public override void OnWrite (PageRange[] pages, ParcelFileDescriptor destination,
                              CancellationSignal cancellationSignal, WriteResultCallback callback)
{
  PrintedPdfDocument.Page page = document.StartPage (0);
  page.Canvas.Scale (scale, scale);
  view.Draw (page.Canvas);
  document.FinishPage (page);
  WritePrintedPdfDoc (destination);
  document.Close ();
  document.Dispose ();
  callback.OnWriteFinished (pages);
}

Printing with our custom adapter is just like using the WebView‘s adapter. Simply pass an instance of the custom adapter, called GenericPrintAdapter in this case, to the PrintManager‘s Print method:

var printManager = (PrintManager)GetSystemService (Context.PrintService);
var content = FindViewById<LinearLayout> (Resource.Id.linearLayout1);
var printAdapter = new GenericPrintAdapter (this, content);
printManager.Print ("MyPrintJob", printAdapter, null);

When the user clicks print, the same system dialog is displayed. In addition to a local printer, you can select Google Cloud Print or print to a PDF file, resulting in the print shown below:

Android Printed to PDF

The printing support added to KitKat is a much welcome feature that is easy to get started with. You can download the code used in this post from my GitHub repo.

Discuss this post in the Xamarin forums.

TwitterFacebookGoogle+LinkedInEmail
April 7, 2014

Sennheiser Cuts Development Time by 40% with Xamarin

By

SennheiserlogoSennheiser is a world leader in quality audio experiences.  The company recently launched Sennheiser TeamConnect, an end-to-end enterprise-grade meeting solution that integrates speakers, microphones, and a central control unit.  When it came to the central control unit used by employees to remotely control the telephone line and audio levels of TeamConnect, the company sought simplicity for the product’s user interface.

Rather than creating a physical control panel, the company decided to put the controls into the user’s hands via an iOS app for iPads and iPhones.

Dirk Eismann, the main developer on the project had experience using Apache Flex, PhoneGap, and pure Objective-C for iOS development, but this was the first Xamarin project for him.

Screen Shot 2014-03-24 at 11.27.41 AM

“I knew that Xamarin’s performance advantages over other cross platform tools and its rock-solid support for all of the iOS APIs were just what this project needed.“ Eismann says.

With a long background in Java, and a familiarity with C#, Eismann found it easy to make the transition to Xamarin, and completed the application code within just 6 weeks.

“Xamarin makes it surprisingly easy to do native mobile development,” Eismann says. “I especially liked how concepts common to .NET developers like events and delegates are added on top to the iOS API. This greatly simplifies development for anyone who has worked with .NET code.”

Download the full case study to learn more about the benefits Sennheiser has enjoyed from using Xamarin—including 40% faster development time, precisely mapped APIs, ease of use, and the ability to share code across platforms.

View the Case Study

TwitterFacebookGoogle+LinkedInEmail
April 3, 2014

Xamarin and Microsoft Launch the .NET Foundation

By

dotnet_logoIt has been an amazing day today. Not only did I have the opportunity to share the stage with the creator of C#, Anders Heljsberg, in the Microsoft Build 2014 day 2 keynote, but we also jointly announced with Microsoft the launch of the .NET Foundation, a newly created entity that will foster open innovation on .NET.

Xamarin contributed six projects to the foundation, and we are excited to be working with Microsoft as we take .NET in this new direction.

Miguel on stag with C# shirt

Microsoft contributed Roslyn to the Foundation, and on stage we demoed Roslyn working on a Mac with the iOS version of our new Xamarin Store App – a beautiful, native app that let’s C# lovers order their own limited edition C# t-shirt for free.  This demo was a great proof point for how open source will help get .NET everywhere.  We are very busy integrating the best bits of Roslyn into our technology stack.

Xamarin is contributing six projects to the .NET Foundation:

  • Xamarin Mobile: a library that exposes a single set of APIs for accessing common mobile device functionality across iOS, Android, and Windows platforms.
  • Xamarin.Auth: A cross-platform API for authenticating users and storing their accounts.
  • Couchbase for .NET: A lightweight, document-oriented (NoSQL), syncable database engine.
  • Xamarin’s System.Drawing:our cross-platform implementation of the familiar drawing API
  • MailKit and MimeKit: robust cross-platform email libraries optimized for mobile.

The .NET Foundation will help accelerate the adoption of .NET across every platform and computing form factor—servers, mobile devices and desktops, and we are very excited for Xamarin to be a part of it.

Check out the Build website to see the recordings of the Day 2 keynote. Tomorrow you should also be able to see a recording of my ”Go Mobile with Xamarin and C#” session.

Miguel and Anders on Stage at build

TwitterFacebookGoogle+LinkedInEmail