Skip to content

Instantly share code, notes, and snippets.

@iseebi
Created September 22, 2010 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iseebi/592158 to your computer and use it in GitHub Desktop.
Save iseebi/592158 to your computer and use it in GitHub Desktop.
ATOK Pad with MonoTouch
using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Collections.Generic;
namespace EbiSoft.Library.iPhone
{
/// <summary>
/// ATOK Pad と連携するための処理を行うクラスです
/// </summary>
public class AtokPad
{
public static readonly string ATOKPadURL = "atokpad://com.atok.ATOK Pad";
public static readonly string PasteboardName = "com.atok.ATOK Pad";
private static int m_atokPadDetectStatus = -1;
/// <summary>
/// ATOK Pad からの結果を受け取ったときに発生するイベントです
/// </summary>
public static event EventHandler<AtokPadReceiveEventArgs> Receive;
/// <summary>
/// ATOK Pad を呼び出せるかどうかを取得します
/// </summary>
/// <remarks>
/// 結果をキャッシュするため、アプリの起動中にATOK Padのインストールまたは削除が行われた場合の認識はできません。
/// </remarks>
public static bool CanCall
{
get
{
if (m_atokPadDetectStatus == -1)
{
NSUrl url = new NSUrl(System.Uri.EscapeUriString(ATOKPadURL));
if (UIApplication.SharedApplication.CanOpenUrl(url))
{
m_atokPadDetectStatus = 1;
}
else
{
m_atokPadDetectStatus = 0;
}
}
return (m_atokPadDetectStatus == 1);
}
}
#region Call Methods
/// <summary>
/// ATOK Pad を呼び出します
/// </summary>
/// <param name="source">
/// 戻り先となるカスタムURLスキーマ
/// </param>
/// <param name="input">
/// ATOK Pad に初期状態で表示する文字列
/// </param>
/// <exception cref="InvalidOperationException">ATOK Padが呼び出せる状態ではありません</exception>
public static void Call(string source, string input)
{
string parameters;
parameters = string.Format("?src={0}", source);
CallInternal(parameters, input);
}
/// <summary>
/// ATOK Pad を呼び出します
/// </summary>
/// <param name="source">
/// 戻り先となるカスタムURLスキーマ
/// </param>
/// <param name="input">
/// ATOK Pad に初期状態で表示する文字列
/// </param>
/// <param name="maxlength">
/// 最大文字数
/// </param>
/// <exception cref="InvalidOperationException">ATOK Padが呼び出せる状態ではありません</exception>
/// <exception cref="ArgumentOutOfRangeException">maxlengthに負の値が指定されています</exception>
public static void Call(string source, string input, int maxlength)
{
if (maxlength < 0)
{
throw new ArgumentOutOfRangeException();
}
string parameters;
parameters = string.Format("?src={0}&maxlength={1}", source, maxlength);
CallInternal(parameters, input);
}
/// <summary>
/// ATOK Padへの情報送信を行う内部メソッド
/// </summary>
/// <param name="parameters">
/// URLパラメータ
/// </param>
/// <param name="input">
/// ATOK Pad に初期状態で表示する文字列
/// </param>
/// <exception cref="InvalidOperationException">ATOK Padが呼び出せる状態ではありません</exception>
private static void CallInternal(string parameters, string input)
{
if (!CanCall)
{
throw new InvalidOperationException("ATOK Pad not found.");
}
string url = System.Uri.EscapeUriString(ATOKPadURL) + parameters;
if (input != null)
{
UIPasteboard pb = UIPasteboard.FromName(PasteboardName, true);
pb.Persistent = true;
pb.String = input;
}
UIApplication.SharedApplication.OpenUrl(new NSUrl(url));
}
#region Call overloads
/// <summary>
/// ATOK Pad を呼び出します
/// </summary>
/// <param name="source">
/// 戻り先となるカスタムURLスキーマ
/// </param>
/// <exception cref="InvalidOperationException">ATOK Padが呼び出せる状態ではありません</exception>
public static void Call(string source)
{
Call(source, null);
}
/// <summary>
/// ATOK Pad を呼び出します
/// </summary>
/// <param name="source">
/// 戻り先となるカスタムURLスキーマ
/// </param>
/// <param name="maxlength">
/// 最大文字数
/// </param>
/// <exception cref="InvalidOperationException">ATOK Padが呼び出せる状態ではありません</exception>
/// <exception cref="ArgumentOutOfRangeException">maxlengthに負の値が指定されています</exception>
public static void Call(string source, int maxlength)
{
Call(source, null, maxlength);
}
#endregion
#endregion
#region Receive Method
/// <summary>
/// ATOK Pad からの戻り値を処理します。UIApplicationDelegate.HandleOpenURL から呼び出します。
/// </summary>
/// <param name="url">
/// UIApplicationDelegate.HandleOpenURL に指定された第2引数の NSUrl
/// </param>
public static void ProcessReceive(NSUrl url)
{
UIPasteboard pb = UIPasteboard.FromName(PasteboardName, false);
string result = pb.String;
UIPasteboard.Remove(PasteboardName);
if (Receive != null)
{
Receive(null, new AtokPadReceiveEventArgs(result, url));
}
}
#endregion
}
/// <summary>
/// ATOK Pad から受信した情報を表すイベントデータ
/// </summary>
public class AtokPadReceiveEventArgs : EventArgs
{
/// <summary>
/// AtokPadReceiveEventArgs コンストラクタ
/// </summary>
/// <param name="result">
/// ATOK Pad から受信したテキスト
/// </param>
/// <param name="key">
/// UIApplicationDelegate.HandleOpenURL に指定された第2引数の NSUrl
/// </param>
internal AtokPadReceiveEventArgs(string result, NSUrl url)
{
Result = result;
Url = url;
}
/// <summary>
/// ATOK Pad から受信したテキストを取得します
/// </summary>
public string Result {
get;
private set;
}
/// <summary>
/// ATOK Pad からの起動時に指定されたURLを取得します
/// </summary>
public NSUrl Url {
get;
private set;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace ATOKPadTest
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
// [Model]
public partial class AppDelegate : UIApplicationDelegate
{
MainViewController mainViewController;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
mainViewController = new MainViewController();
mainViewController.View.Frame = UIScreen.MainScreen.ApplicationFrame;
window.AddSubview (mainViewController.View);
window.MakeKeyAndVisible ();
return true;
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
public override void HandleOpenURL (UIApplication application, NSUrl url)
{
if (url.Scheme == "esatokpadtest")
{
EbiSoft.Library.iPhone.AtokPad.ProcessReceive(url);
}
}
}
}
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.Foundation;
using System;
using EbiSoft.Library.iPhone;
namespace ATOKPadTest
{
public partial class MainViewController : UIViewController
{
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
AtokPad.Receive += delegate(object sender, AtokPadReceiveEventArgs e) {
mainTextView.Text = e.Result;
};
atokPadButton.Hidden = !AtokPad.CanCall;
}
partial void doAtokPad (UIButton sender)
{
AtokPad.Call("esatokpadtest://return/", mainTextView.Text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment