This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
forked from chrisntr/MonoMobile.Extensions
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from xamarin/ios-unified-support
iOS Unified API support with samples
- Loading branch information
Showing
73 changed files
with
4,151 additions
and
1,278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
bin | ||
Bin | ||
obj | ||
packages | ||
TestResults | ||
.DS_Store | ||
_ReSharper.* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,123 @@ | ||
## Examples | ||
|
||
### Contacts | ||
To access the address book (requires `READ_CONTACTS` permissions | ||
on Android): | ||
|
||
```csharp | ||
using Xamarin.Contacts; | ||
// ... | ||
var book = new 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): | ||
|
||
```csharp | ||
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()); | ||
``` | ||
|
||
### 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: | ||
|
||
```csharp | ||
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): | ||
|
||
```csharp | ||
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: | ||
|
||
```csharp | ||
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()); | ||
``` | ||
## Examples | ||
|
||
### Contacts | ||
To access the address book (requires `READ_CONTACTS` permissions | ||
on Android): | ||
|
||
```csharp | ||
using Xamarin.Contacts; | ||
// ... | ||
var book = new 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): | ||
|
||
```csharp | ||
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: | ||
|
||
```csharp | ||
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): | ||
|
||
```csharp | ||
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: | ||
|
||
```csharp | ||
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.