Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Latest commit

 

History

History
123 lines (95 loc) · 3.51 KB

GettingStarted.md

File metadata and controls

123 lines (95 loc) · 3.51 KB

Examples

Contacts

To access the address book (requires READ_CONTACTS permissions on Android):

using Xamarin.Contacts;
// ...

var book = new Xamarin.Contacts.AddressBook ();
book.RequestPermission().ContinueWith (t => {
	if (!t.Result) {
		Console.WriteLine ("Permission denied by user or manifest");
		return;
	}

	foreach (Contact contact in book.OrderBy (c => c.LastName)) {
		Console.WriteLine ("{0} {1}", contact.FirstName, contact.LastName);
	}
}, TaskScheduler.FromCurrentSynchronizationContext());

Geolocation

To get the user's location (requires ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions on Android):

using Xamarin.Geolocation;
// ...

var locator = new Geolocator { DesiredAccuracy = 50 };
//            new Geolocator (this) { ... }; on Android
locator.GetPositionAsync (timeout: 10000).ContinueWith (t => {
	Console.WriteLine ("Position Status: {0}", t.Result.Timestamp);
	Console.WriteLine ("Position Latitude: {0}", t.Result.Latitude);
	Console.WriteLine ("Position Longitude: {0}", t.Result.Longitude);
}, TaskScheduler.FromCurrentSynchronizationContext());

NOTE: On iOS 8.0+ you must set either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in your Info.plist file so that Xamarin.Mobile will request the appropriate permission from the user.

Media

MediaPicker allows you to invoke the native UI to take or select photos or video. Given that there is this UI interaction, the code (while simpler than doing it manually) will not be completely cross-platform.

To take a photo on iOS, Windows Phone or WinRT:

using Xamarin.Media;
// ...

var picker = new MediaPicker();
picker.PickPhotoAsync().ContinueWith (t => {
	MediaFile file = t.Result;
	Console.WriteLine (file.Path);
}, TaskScheduler.FromCurrentSynchronizationContext());

On Android and optionally on iOS, you control the UI.

To take a photo on Android (requires WRITE_EXTERNAL_STORAGE permissions):

using Xamarin.Media;
// ...

protected override void OnCreate (Bundle bundle)
{
	var picker = new MediaPicker (this);
	if (!picker.IsCameraAvailable)
		Console.WriteLine ("No camera!");
	else {
		var intent = picker.GetTakePhotoUI (new StoreCameraMediaOptions {
			Name = "test.jpg",
			Directory = "MediaPickerSample"
		});
		StartActivityForResult (intent, 1);
	}
}

protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
	// User canceled
	if (resultCode == Result.Canceled)
		return;

	data.GetMediaFileExtraAsync (this).ContinueWith (t => {
		Console.WriteLine (t.Result.Path);
	}, TaskScheduler.FromCurrentSynchronizationContext());
}

To take a photo on iOS controlling the UI:

using Xamarin.Media;
// ...

var picker = new MediaPicker();
MediaPickerController controller = picker.GetTakePhotoUI (new StoreCameraMediaOptions {
	Name = "test.jpg",
	Directory = "MediaPickerSample"
});

// On iPad, you'll use UIPopoverController to present the controller
PresentViewController (controller, true, null);

controller.GetResultAsync().ContinueWith (t => {
	// Dismiss the UI yourself
	controller.DismissViewController (true, () => {
		MediaFile file = t.Result;
	});
	
}, TaskScheduler.FromCurrentSynchronizationContext());

#####Note to iOS 8 Developers Showing a MediaPicker in response to a UIActionSheet.Clicked event will cause unexpected behavior on iOS 8. Apps should be updated to conditionally use an UIAlertController with a style of UIAlertControllerStyle.ActionSheet. See the iOS sample for more info.