デフォルトのURLスキームでの扱い方
-
22-09-2019 - |
質問
アプリに URI (または URL スキーム) サポートを構築したいと考えています。
私は、 LSSetDefaultHandlerForURLScheme()
私の中で + (void)initialize
そして、特定のURLスキームも設定しました info.plist
. 。したがって、私はURLスキームを持っていません Apple Script
または Apple Events
.
電話すると myScheme:
私のお気に入りのブラウザでは、システムがアプリをアクティブ化します。
問題は、スキームが呼び出されたときにそれらをどのように処理するかです。あるいは、次のように言うとよいでしょう。アプリがいつ何を行うべきかを定義するにはどうすればよいですか myScheme:
と呼ばれます。
実装する必要がある特別なメソッドはありますか、それともどこかに登録する必要がありますか?
解決
AppleScript について言及しているので、Mac OS X で作業していると思います。
カスタム URL スキームを登録して使用する簡単な方法は、.plist でスキームを定義することです。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>URLHandlerTestApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>urlHandlerTestApp</string>
</array>
</dict>
</array>
スキームを登録するには、これを AppDelegate の初期化に入れます。
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(handleURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
アプリケーションが URL スキーム経由でアクティブ化されるたびに、定義されたセレクターが呼び出されます。
URL 文字列を取得する方法を示すイベント処理メソッドのスタブ:
- (void)handleURLEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
NSLog(@"%@", url);
}
Apple のドキュメント: Get URL ハンドラーのインストール
アップデートイベント ハンドラーをインストールするサンドボックス アプリの問題に気づきました。 applicationDidFinishLaunching:
. 。サンドボックスを有効にすると、カスタム スキームを使用する URL をクリックしてアプリが起動されるときに、ハンドラー メソッドは呼び出されません。少し前にハンドラーをインストールすることで、 applicationWillFinishLaunching:
, 、メソッドは期待どおりに呼び出されます。
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(handleURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
}
- (void)handleURLEvent:(NSAppleEventDescriptor*)event
withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
NSLog(@"%@", url);
}
iPhone で URL スキームのアクティベーションを処理する最も簡単な方法は、UIApplicationDelegate の application:handleOpenURL:
- ドキュメンテーション
他のヒント
すべてのクレジットはに行くべきの weichsel と KCH の
私は自分の利便
のためのSWIFT(2.2 / 3.0)のコードを追加していますfunc applicationWillFinishLaunching(_ notification: Notification) {
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleGetURL(event:reply:)), forEventClass: UInt32(kInternetEventClass), andEventID: UInt32(kAEGetURL) )
}
func handleGetURL(event: NSAppleEventDescriptor, reply:NSAppleEventDescriptor) {
if let urlString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue {
print("got urlString \(urlString)")
}
}
問題は、スキームが呼び出されたときにそれらをどのように処理するかです。
そこで登場するのがApple Eventだ。Launch Services は、アプリに URL を開いてもらいたい場合、アプリに kInternetEventClass
/kAEGetURL
イベント。
あなたは、スクリプト用語SDEFに「取得URL」コマンドを定義し、対応するメソッドを実装することができます。例えば、ターミナルのSDEFは、URLを処理するため、次のコマンド定義が含まれています。
<command name="get URL" code="GURLGURL" description="Open a command an ssh, telnet, or x-man-page URL." hidden="yes">
<direct-parameter type="text" description="The URL to open." />
</command>
とそれへのアプリケーション応答することを宣言します:
<class name="application" code="capp" description="The application's top-level scripting object.">
<cocoa class="TTApplication"/>
<responds-to command="get URL">
<cocoa method="handleGetURLScriptCommand:" />
</responds-to>
</class>
TTApplicationクラス(NSApplicationののサブクラス)メソッドを定義します。
- (void)handleGetURLScriptCommand:(NSScriptCommand *)command { … }
私は、コードのわずかに異なるスウィフト4/5バージョンを追加しています
func applicationWillFinishLaunching(_ notification: Notification) {
NSAppleEventManager
.shared()
.setEventHandler(
self,
andSelector: #selector(handleURL(event:reply:)),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL)
)
}
@objc func handleURL(event: NSAppleEventDescriptor, reply: NSAppleEventDescriptor) {
if let path = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue?.removingPercentEncoding {
NSLog("Opened URL: \(path)")
}
}