Skip to content

Commit

Permalink
Add Clipboard support & Safari support for iOS 9
Browse files Browse the repository at this point in the history
Updated readme
  • Loading branch information
jamesmontemagno committed Jan 26, 2016
1 parent 70218e8 commit 3418c5c
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 175 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ Task OpenBrowser(string url);
Task ShareLink(string url, string message = null, string title = null);
```

## Clipboard
Added in 3.2.X is the ability to set text directly on the clipboard. All Operatings support this except for Windows Phone 8.1 RT

```
/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to dislay (no required, Android only)</param>
/// <returns></returns>
Task<bool> SetClipboardText(string text, string label = null);
/// <summary>
/// Gets if clipboard is supported
/// </summary>
bool SupportsClipboard { get; }
```


## iOS Specific Support
Added in iOS 9 is the ability to use the new SFSafariViewController to open the browser: https://blog.xamarin.com/keep-users-engaged-with-ios-9s-sfsafariviewcontroller/ This is really awesome and you can toggle this on by setting:

```csharp
Plugin.Share.ShareImplementation.UseSafariViewController = true;
```
in your iOS project.

If your app is on iOS 9 and it is true then it will use the new controller, else it will drop to normal OpenUrl.

Please be aware of ATS restrcitions with SafariViews.


#### Maintaners
* [Jakob Gürtl](https://github.com/jguertl)
* [James Montemagno](https://github.com/jamesmontemagno)
Expand Down
343 changes: 170 additions & 173 deletions Share.sln

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Share/Share.Plugin.Abstractions/IShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ public interface IShare
/// <returns>awaitable Task</returns>
Task ShareLink(string url, string message = null, string title = null);

/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to dislay (no required, Android only)</param>
/// <returns></returns>
Task<bool> SetClipboardText(string text, string label = null);

/// <summary>
/// Gets if clipboard is supported
/// </summary>
bool SupportsClipboard { get; }
}
}
37 changes: 37 additions & 0 deletions Share/Share.Plugin.Android/ShareImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Android.App;
using Android.Content;
using Plugin.Share.Abstractions;
using System;
Expand Down Expand Up @@ -74,8 +75,44 @@ public async Task ShareLink(string url, string message = null, string title = nu
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(chooserIntent);

}

/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to display (not required, Android only)</param>
/// <returns></returns>
public Task<bool> SetClipboardText(string text, string label = null)
{
try
{
var sdk = (int)Android.OS.Build.VERSION.SdkInt;
if (sdk < (int)Android.OS.BuildVersionCodes.Honeycomb)
{
var clipboard = (Android.Text.ClipboardManager)Application.Context.GetSystemService(Context.ClipboardService);
clipboard.Text = text;
}
else
{
var clipboard = (ClipboardManager)Application.Context.GetSystemService(Context.ClipboardService);
clipboard.PrimaryClip = ClipData.NewPlainText(label ?? string.Empty, text);
}
return Task.FromResult(true);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Unable to copy to clipboard: " + ex);
}

return Task.FromResult(false);
}

/// <summary>
/// Gets if cliboard is supported
/// </summary>
public bool SupportsClipboard { get { return true; } }

}
}
17 changes: 17 additions & 0 deletions Share/Share.Plugin.WindowsPhone8/ShareImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,22 @@ public async Task ShareLink(string url, string message = null, string title = nu
}

}

/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to display (not required, Android only)</param>
/// <returns></returns>
public Task<bool> SetClipboardText(string text, string label = null)
{
Clipboard.SetText(text);
return Task.FromResult(true);
}

/// <summary>
/// Gets if cliboard is supported
/// </summary>
public bool SupportsClipboard { get { return true; } }
}
}
32 changes: 32 additions & 0 deletions Share/Share.Plugin.WindowsPhone81/ShareImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,37 @@ private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs
Debug.WriteLine("Unable to share text: " + ex);
}
}

/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to display (not required, Android only)</param>
/// <returns></returns>
public Task<bool> SetClipboardText(string text, string label = null)
{
#if WINDOWS_UWP || WINDOWS_APP
var dataPackage = new DataPackage();
dataPackage.RequestedOperation = DataPackageOperation.Copy;
dataPackage.SetText(text);

Clipboard.SetContent(dataPackage);
return Task.FromResult(true);
#else
return Task.FromResult(false);
#endif
}

/// <summary>
/// Gets if cliboard is supported
/// </summary>
public bool SupportsClipboard
{
#if WINDOWS_UWP || WINDOWS_APP
get { return true; }
#else
get { return false; }
#endif
}
}
}
38 changes: 36 additions & 2 deletions Share/Share.Plugin.iOSUnified/ShareImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static async Task Init()
{
var test = DateTime.UtcNow;
}

public static bool UseSafariViewController { get; set; }
/// <summary>
/// Open a browser to a specific url
/// </summary>
Expand All @@ -25,7 +27,22 @@ public async Task OpenBrowser(string url)
{
try
{
UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
if (UseSafariViewController && UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
{
var sfViewController = new SafariServices.SFSafariViewController(new NSUrl(url));
var vc = GetVisibleViewController();

if (sfViewController.PopoverPresentationController != null)
{
sfViewController.PopoverPresentationController.SourceView = vc.View;
}

await vc.PresentViewControllerAsync(sfViewController, true);
}
else
{
UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -111,6 +128,23 @@ UIViewController GetVisibleViewController()

return rootController.PresentedViewController;
}


/// <summary>
/// Sets text on the clipboard
/// </summary>
/// <param name="text">Text to set</param>
/// <param name="label">Label to display (not required, Android only)</param>
/// <returns></returns>
public Task<bool> SetClipboardText(string text, string label = null)
{
UIPasteboard.General.String = text;
return Task.FromResult(true);
}

/// <summary>
/// Gets if cliboard is supported
/// </summary>
public bool SupportsClipboard { get { return true; } }

}
}

0 comments on commit 3418c5c

Please sign in to comment.