我在我的(voID)初始化中做了一个LSSetDefaultHandlerForURLScheme(),我也在我的info.pList中设置了特定的URL方案。所以我有没有Apple Script或Apple Events的URL方案。
当我打电话给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);}
苹果文档:Installing a Get URL Handler
更新
我刚刚注意到在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的应用程序:handleOpenURL: – Documentation
总结以上是内存溢出为你收集整理的swift – 如何使用默认的URL方案处理全部内容,希望文章能够帮你解决swift – 如何使用默认的URL方案处理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)