问题描述
有没有一种方法来定义某种类型的处理机制在Android的和的iOS,让我做拦截以下任一操作:
Is there a way to define some kind of handling mechanism in Android and iOS that would allow me to do intercept either of the following:
myapp:///events/3/
- or -
http://myapp.com/events/3/
我想'听'的任何协议或主机,并打开相应的活动/的ViewController。
I'd like to 'listen' for either the protocol or the host, and open a corresponding Activity / ViewController.
我想太多,如果这些可以作为全系统成为可能。我想这将是更多的在iOS的一个问题,但我的理想可以点击上述两种方案中,超链接,从任何应用程序。 Gmail中,Safari浏览器等。
I'd like too if these could be as system wide as possible. I imagine this will be more of an issue on iOS, but I'd ideally be able to click either of those two schemes, as hyperlinks, from any app. Gmail, Safari, etc.
推荐答案
有关iOS的,是的,你可以做两件事情:
For iOS, yes, you can do two things:
-
让你的应用程序做广告,它可以处理的URL与给定的方案。
Have your app advertise that it can handle URL's with a given scheme.
安装协议处理程序来处理任何方式你喜欢。
Install a protocol handler to handle whatever scheme you like.
第一个选项是pretty的简单,在Implementing自定义URL方案的。为了让系统知道你的应用程序可以处理一个给定的方案:
The first option is pretty straightforward, and described in Implementing Custom URL Schemes. To let the system know that your app can handle a given scheme:
-
更新你的应用程序的Info.plist与CFBundleURLTypes条目
update your app's Info.plist with a CFBundleURLTypes entry
实施 -application:didFinishLaunchingWithOptions:
在你的应用程序代理
implement -application:didFinishLaunchingWithOptions:
in your app delegate.
第二种可能性是写自己的协议处理程序。这只有在您的应用程序,但你可以用它结合上述技术。使用上面的方法让系统启动应用程序对于给定的URL,然后使用自定义URL协议处理程序的应用程序中充分利用内部监督办公室的网址加载系统的功率:
The second possibility is to write your own protocol handler. This works only within your app, but you can use it in conjunction with the technique described above. Use the method above to get the system to launch your app for a given URL, and then use a custom URL protocol handler within your app to leverage the power of iOS's URL loading system:
-
创建的<$c$c>NSURLProtocol$c$c>.
覆盖 + canInitWithRequest:
- 通常你只要看看URL方案,并接受它,如果它要处理的方案相匹配,但你可以看看的要求以及其他方面。
Override +canInitWithRequest:
-- usually you'll just look at the URL scheme and accept it if it matches the scheme you want to handle, but you can look at other aspects of the request as well.
填写您的子类: [MyURLProtocol的registerClass];
覆盖 -startLoading
和 -stopLoading
启动和停止加载的要求,分别。
Override -startLoading
and -stopLoading
to start and stop loading the request, respectively.
阅读上面链接以获取更多信息NSURLProtocol文档。难度的关卡在很大程度上取决于你想实现什么。这是常见的iOS应用程序来实现自定义URL处理程序,以便其他应用程序可以进行简单的请求。实现你自己的HTTP或FTP处理程序较为复杂。
Read the NSURLProtocol docs linked above for more information. The level of difficulty here depends largely on what you're trying to implement. It's common for iOS apps to implement a custom URL handler so that other apps can make simple requests. Implementing your own HTTP or FTP handler is a bit more involved.
有关它的价值,这是PhoneGap的是如何工作的在iOS。 PhoneGap的包括NSURLProtocol子类,称为PGURLProtocol,看起来在任何应用程序尝试加载URL的方案,接管,如果它是它识别方案之一。 PhoneGap的开源表妹科尔多瓦 - 你可能会发现它有助于看看
For what it's worth, this is exactly how PhoneGap works on iOS. PhoneGap includes an NSURLProtocol subclass called PGURLProtocol that looks at the scheme of any URL the app tries to load and takes over if it's one of the schemes that it recognizes. PhoneGap's open-source cousin is Cordova -- you may find it helpful to take a look.
这篇关于安卓/ iOS的 - 自定义的URI /协议处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!