「Xamarin.Mac/カスタムURLスキームを利用する」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator |
imported>Administrator (→実装) |
||
2行目: | 2行目: | ||
== 実装 == | == 実装 == | ||
+ | === カスタムURLスキームを有効にする === | ||
以下でカスタムURLスキームが利用可能になる。 | 以下でカスタムURLスキームが利用可能になる。 | ||
# Xamarin Studioでプロジェクト内の「Info.plist」を開く | # Xamarin Studioでプロジェクト内の「Info.plist」を開く | ||
9行目: | 10行目: | ||
# 「URL Schems」にURLスキーム名を入れる(例:monobook) | # 「URL Schems」にURLスキーム名を入れる(例:monobook) | ||
+ | === カスタムURLスキームで呼ばれるメソッドを作る === | ||
次にURLスキームが叩かれた際に呼ばれるメソッドの登録と実装をする。 | 次にURLスキームが叩かれた際に呼ばれるメソッドの登録と実装をする。 | ||
<source lang="csharp"> | <source lang="csharp"> | ||
31行目: | 33行目: | ||
{ | { | ||
// URLスキームで呼ばれるメソッドを指定する。 | // URLスキームで呼ばれるメソッドを指定する。 | ||
+ | // Export("handleGetURLEvent:withReplyEvent:")属性が付いたメソッドが呼ばれる。 | ||
var appleEventManager = NSAppleEventManager.SharedAppleEventManager; | var appleEventManager = NSAppleEventManager.SharedAppleEventManager; | ||
appleEventManager.SetEventHandler(this | appleEventManager.SetEventHandler(this | ||
62行目: | 65行目: | ||
/// <summary> | /// <summary> | ||
− | /// Xamarin. | + | /// NSAppleEventDescriptorのParamDescriptorForKeywordメソッドに指定する定数。 |
+ | /// Xamarin.Macに定数らしきものが見当たらなかったのでxcode眺めつつ作った。 | ||
/// </summary> | /// </summary> | ||
public static class AEKeyword | public static class AEKeyword | ||
88行目: | 92行目: | ||
</source> | </source> | ||
− | + | === デバッグ === | |
+ | ターミナルでopenコマンドを叩けば簡単に[[デバッグ]]できる。openの引数(URLスキーム)にアンパサンド(&)を含む場合はダブルクォーテーション(")で囲むこと。 | ||
open "monobook://" | open "monobook://" | ||
open "monobook://inbox" | open "monobook://inbox" |
2016年7月15日 (金) 08:17時点における版
Xamarin.MacでカスタムURLスキームを使いたい。
実装
カスタムURLスキームを有効にする
以下でカスタムURLスキームが利用可能になる。
- Xamarin Studioでプロジェクト内の「Info.plist」を開く
- 下部の「Advanced」タブを開く
- URL Typesの「Add URL Type」ボタンを押す
- 「Identifier」は何でもよい。アプリのIdentifierでも入れておく。
- 「URL Schems」にURLスキーム名を入れる(例:monobook)
カスタムURLスキームで呼ばれるメソッドを作る
次にURLスキームが叩かれた際に呼ばれるメソッドの登録と実装をする。
using System;
using System.Linq;
using System.Collections.Generic;
using AppKit;
using Foundation;
using ObjCRuntime;
namespace UrlScheme
{
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
public AppDelegate()
{
}
public override void DidFinishLaunching(NSNotification notification)
{
// URLスキームで呼ばれるメソッドを指定する。
// Export("handleGetURLEvent:withReplyEvent:")属性が付いたメソッドが呼ばれる。
var appleEventManager = NSAppleEventManager.SharedAppleEventManager;
appleEventManager.SetEventHandler(this
, new Selector("handleGetURLEvent:withReplyEvent:")
, AEEventClass.Internet
, AEEventID.GetUrl);
}
[Export("handleGetURLEvent:withReplyEvent:")]
private void HandleGetURLEvent(NSAppleEventDescriptor descriptor, NSAppleEventDescriptor replyEvent)
{
// URIを取得する。
var uriString = descriptor.ParamDescriptorForKeyword(AEKeyword.DirectObject).StringValue;
// QueryString以外はUriクラスがパースしてくれる。
var uriObject = new Uri(uriString);
// QueryStringをパースする。
// Xamarin.MacはPCL縛りでSystem.WebのHttpUtility.ParseQueryStringが使えない。
var parameters = string.IsNullOrEmpty(uriObject.Query)
? new Dictionary<string, string>()
: uriObject.Query
.TrimStart(new[] { '?' })
.Split('&')
.Select(p => p.Split('='))
.ToDictionary(p => p[0], p => 1 < p.Length ? Uri.UnescapeDataString(p[1]) : null)
;
Console.WriteLine(parameters);
}
/// <summary>
/// NSAppleEventDescriptorのParamDescriptorForKeywordメソッドに指定する定数。
/// Xamarin.Macに定数らしきものが見当たらなかったのでxcode眺めつつ作った。
/// </summary>
public static class AEKeyword
{
public static readonly uint DirectObject = Create("----");
public static readonly uint ErrorNumber = Create("errn");
public static readonly uint ErrorString = Create("errs");
public static readonly uint ProcessSerialNumber = Create("psn ");
public static readonly uint PreDispatch = Create("phac");
public static readonly uint SelectProc = Create("selh");
public static readonly uint AERecorderCount = Create("recr");
public static readonly uint AEVersion = Create("vers");
static uint Create(string key)
{
return (
((uint)key[0]) << 24 |
((uint)key[1]) << 16 |
((uint)key[2]) << 8 |
((uint)key[3]) << 0);
}
}
}
}
デバッグ
ターミナルでopenコマンドを叩けば簡単にデバッグできる。openの引数(URLスキーム)にアンパサンド(&)を含む場合はダブルクォーテーション(")で囲むこと。
open "monobook://" open "monobook://inbox" open "monobook://inbox?sort=date" open "monobook://inbox?sort=date&filter=abc"