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

Commit

Permalink
Merge pull request #36 from xamarin/ios-unified-support
Browse files Browse the repository at this point in the history
iOS Unified API support with samples
  • Loading branch information
bholmes committed Jun 4, 2015
2 parents c396bd8 + e934561 commit 90e3a23
Show file tree
Hide file tree
Showing 73 changed files with 4,151 additions and 1,278 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bin
Bin
obj
packages
TestResults
.DS_Store
_ReSharper.*
Expand Down
240 changes: 123 additions & 117 deletions GettingStarted.md
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.


5 changes: 3 additions & 2 deletions MonoDroid/Samples/ContactsSample/Contacts Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<AndroidSupportedAbis>armeabi;armeabi-v7a;x86</AndroidSupportedAbis>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -36,7 +37,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<AndroidSupportedAbis>armeabi;armeabi-v7a;x86</AndroidSupportedAbis>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Android" />
Expand Down Expand Up @@ -70,7 +71,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Xamarin.Mobile\Xamarin.Mobile.Android.csproj">
<Project>{32dff77e-ae38-48d6-b067-cf555798ea32}</Project>
<Project>{32DFF77E-AE38-48D6-B067-CF555798EA32}</Project>
<Name>Xamarin.Mobile.Android</Name>
</ProjectReference>
</ItemGroup>
Expand Down
Loading

0 comments on commit 90e3a23

Please sign in to comment.