在windows以及iOS中Registeringthe Application Handling the Custom Protocol 把应用程序注册到特定协议上

翻译 2012年03月22日 22:55:48

To register an application to handlea particular URL protocol, add a new key, along with the appropriate subkeysand values, to HKEY_CLASSES_ROOT. The root key must match the protocol schemethat is being added. For instance, to add an "alert:" protocol, addanalert key to HKEY_CLASSES_ROOT, as follows:

HKEY_CLASSES_ROOT
alert

URL Protocol = ""

Under this new key, the URLProtocol string value indicates that this key declares a custom protocolhandler. Without this key, the handler application will not launch. The valueshould be an empty string.

首先通过windows操作系统的注册表,增加系统key,注意URL protocol必须是空的。

Keys should also be added for DefaultIconand shell. The Default string value of theDefaultIcon key mustbe the file name to use as an icon for this new URL protocol.使用URL打开时候显示的图标,类似IE图标。

 The string takes the form "path,iconindex" with a maximum length of MAX_PATH. The name of the first keyunder theshell key should be an action verb, such as open. Underthis key, acommand key or a DDEEXEC key indicate how the handlershould be invoked. The values under thecommand and DDEEXEC keysdescribe how to launch the application handling the new protocol.

Finally, the Default stringvalue should contain the display name of the new protocol. The followingexample shows how to register an application, alert.exe in this case, to handlethealert protocol.

HKEY_CLASSES_ROOT
alert

(Default) = "URL:Alert Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "alert.exe,1"
shell
open
command

(Default) = "C:\Program Files\Alert\alert.exe" "%1"

When a user clicks a link registeredto your custom URL protocol, Internet Explorer launches the registered URLprotocol handler. If the specifiedopen command specified in theregistry contains a %1 parameter, Internet Explorer passes the URI tothe registered protocol handler application.

Launchingthe Handler

By adding the above settings to theregistry, navigating to URLs such asalert:Hello%20Worldwould cause an attempt to launch alert.exe with the complete URL on the commandline. Internet Explorer decodes the URL, but the WindowsRun... commanddoes not. If a URL contains spaces, it may be split across more than oneargument on the command line.

For example, if the link above isfollowed through Internet Explorer, the command line would be:  一般的测试方式就是通过浏览器,使用该协议alert:helloworld

"C:\ProgramFiles\Alert\alert.exe" "alert:Hello World"

然后我们看Ios中的概念:名称有些不一样,IPC Using URL Handlers,透过URL处理器实现进程间通讯,其实就是通过Protocol启动某个进程。

WhenSafari encounters a URL beginning with alocola:// it issues a call to open theURL, causing Safari to exit. 简单来说,在浏览器中通过alocola某个自定义Protocol,然后就可以启动关联的程序

Thesystem launches Alocola and passes the URL to it. Alocola then gathers thelocation information and opens an http:// or https:// URL on the originatingsite, causing Alocola to exit.

如果在程序Alocola中,访问http://https://那么程序会退出,浏览器程序会启动,当然URL string就作为参数了。

 

  1. Open     the Info.plist file (typically found under the “Resources Folder”) in     Xcode.
  2. Select     the top-level “Information Property List” element and add a new child-key     to by clicking the                                                                                                                                   button.
  3. Scroll     to the bottom of the drop-down list and select “URL types.”
  4. Enter     a unique identifier (e.g., com.mydomain.myprotocol) for the value of the     “URL identifier” key.
  5. With     “URL identifier” still selected, click the            button     and select “URL Schemes” from the resulting list
  6. Enter     the protocol name — e.g., http, alocola or, as in the example below,     myprotocol — as the value for the “Item 1″ key.
  7. The     finished Info.plist looks like this:
                   

That’s all there is to registering aprotocol handler. The iPhone will automatically wire up the association whenthe app in installed.通过以上,其实就是最后那个图所显示的那样,那么当在safari中访问myprotocol://,程序就会运行起来了

二、响应自定义URL协议

编辑文件AppDelegate.m,加入方法:

- (BOOL)application:(UIApplication *)applicationhandleOpenURL:(NSURL*)url  {

    if(!url) { 

        returnNO;

    }else{

        NSString* queryString=[url query];

        NSLog(@"query string:%@",queryString);

        if ([@"from=URLSchemeRequester"isEqualToString:queryString]) {

            returnYES;

        }else

            returnNO;

    }

}

注意,在这段代码中,我们判断了URL中的查询字串如果等于from=URLSchemeRequester,我们就会打开程序,否则不会处理。

三、在其他程序里打开自定义URL协议

新建Empty Application,命名为 URLSchemeRequester。

修改AppDelegate,将程序窗口的背景色修改为某个颜色比如红色,以便我们能识别是当前看到的是哪个应用程序:

self.window.backgroundColor = [UIColorredColor];

接着在窗口中加入一个Button:

UIButton* btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"打开URLSchemeResponder"forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

    [self.windowaddSubview:btn];

 

接着在btnAction方法中打开自定义协议urlschemetest,即调用外部程序URLSchemeResponder:

-(void)btnAction{

    [[UIApplicationsharedApplication]openURL:

     [NSURLURLWithString:@"urlschemetest://?from=URLSchemeRequester"]];  //参数from?取值 

}

如果我们想让URLSchemeResponder不能直接启动,只能由URLSchemeRequester通过openURL方式启动,可以修改URLSchemeResponder的application: didFinishLaunchingWithOptions方法,对launchOptions进行判断:

id url=[launchOptions objectForKey:   

二、响应自定义URL协议

编辑文件AppDelegate.m,加入方法:

- (BOOL)application:(UIApplication *)applicationhandleOpenURL:(NSURL*)url  {

    if(!url) { 

        returnNO;

    }else{

        NSString* queryString=[url query];

        NSLog(@"query string:%@",queryString);

        if ([@"from=URLSchemeRequester"isEqualToString:queryString]) {

            returnYES;

        }else

            returnNO;

    }

}

注意,在这段代码中,我们判断了URL中的查询字串如果等于from=URLSchemeRequester,我们就会打开程序,否则不会处理。

三、在其他程序里打开自定义URL协议

新建Empty Application,命名为 URLSchemeRequester。

修改AppDelegate,将程序窗口的背景色修改为某个颜色比如红色,以便我们能识别是当前看到的是哪个应用程序:

self.window.backgroundColor = [UIColorredColor];

接着在窗口中加入一个Button:

UIButton* btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"打开URLSchemeResponder"forState:UIControlStateNormal];

    [btn addTarget:selfaction:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

    [self.windowaddSubview:btn];

 

接着在btnAction方法中打开自定义协议urlschemetest,即调用外部程序URLSchemeResponder:

-(void)btnAction{

    [[UIApplicationsharedApplication]openURL:

     [NSURLURLWithString:@"urlschemetest://?from=URLSchemeRequester"]];

}

如果我们想让URLSchemeResponder不能直接启动,只能由URLSchemeRequester通过openURL方式启动,可以修改URLSchemeResponder的application: didFinishLaunchingWithOptions方法,对launchOptions进行判断:

id url=[launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];   //如此判断其是否由url启动???

    NSLog(@"url:%@",url);

    if (url) {

        self.window = [[[UIWindowallocinitWithFrame:[[UIScreenmainScreenbounds]] autorelease];

        // Override point forcustomization after application launch.

        self.window.backgroundColor = [UIColorpurpleColor];

        UILabel* lb=[[UILabelalloc]initWithFrame:CGRectMake(120, 240, 180, 44)];

        [lb setText:@"URL Scheme Responder"];

        [self.windowaddSubview:lb];

        [lbrelease];

        [self.windowmakeKeyAndVisible];

        returnYES;

    }else{

        exit(0);

        abort();

    }

参数launchOptions是一个字典,如果我们以 UIApplicationLaunchOptionsURLKeyw为Key去检索luanchOptions,能够得到一个NSURL对象。该对象的值跟应用程序启动方式有关。如果应用程序的启动方式为openURL,这个NSURL对象不应该为空。如果为nil,则表明应用程序不是用openURL方式启动

];

    NSLog(@"url:%@",url);

    if (url) {

        self.window = [[[UIWindowallocinitWithFrame:[[UIScreenmainScreenbounds]] autorelease];

        // Override point forcustomization after application launch.

        self.window.backgroundColor = [UIColorpurpleColor];

        UILabel* lb=[[UILabelalloc]initWithFrame:CGRectMake(120, 240, 180, 44)];

        [lb setText:@"URL Scheme Responder"];

        [self.windowaddSubview:lb];

        [lbrelease];

        [self.windowmakeKeyAndVisible];

        returnYES;

    }else{

        exit(0);

        abort();

    }

参数launchOptions是一个字典,如果我们以 UIApplicationLaunchOptionsURLKeyw为Key去检索luanchOptions,能够得到一个NSURL对象。该对象的值跟应用程序启动方式有关。如果应用程序的启动方式为openURL,这个NSURL对象不应该为空。如果为nil,则表明应用程序不是用openURL方式启动


下篇我们看看win 8又是如何实现程序协议化访问的:)

相关文章推荐

windows下如何禁止某个特定的应用程序

windows下如何禁止某个特定的应用程序        最近,想了下如何禁止打开windows下的特定应用程序,查阅资料后终于解决了,下面把具体方法分享给大家(以禁止当前主流游戏--英雄联盟为例)...

windows phone 8 新增功能:从一个应用程序启动另一个程序(file association 和 Protocol association两种方式)

一. 启动手机预装内置程序打开文件 file association 这里以打开word文档为例子 [csharp] view plaincopy ...

将Windows应用程序注册为服务运行

将Windows应用程序注册为服务运行 instsrv srvany

windows应用程序开发系列五:注册窗口类

开发流程: 1 定义一个窗口处理函数 2 注册窗口类 3 创建窗口 4 显示窗口 5 更新窗口 6 消息循环 窗口类的分类     -系统窗口类      系统已经定义好的窗口类,所...

IOS应用程序扩展Application Extension是什么(包括第三方输入法以及来电防骚扰)

对于开发者来说怎么开发扩展功能? 1.创建一个新工程,myExtension,下图是创建后的目录结构 这些文件都不用管里面不需要写代码,就是一个空工程2.点击File->New->Target...

IOS Empty Application ,纯代码创建的视图应用程序

IOS,纯代码创建的视图应用程序
  • holdsky
  • holdsky
  • 2014年04月03日 16:10
  • 973

iOS Application Life Cycle 应用程序生命周期

应用程序的状态 IOS的应用程序一共有5种状态。 Not running(未运行):程序未启动 Inactive(未激活):其他两个状态切换时出现的短暂状态。唯一在此状态停留时间比较长的...

【IOS6.0 自学瞎折腾】(五)应用程序的启动过程和Application生命周期

一 :main函数入口 看下项目资源结构,其实程序的入口也是在main.m里面。 #import #import "BvinAppDelegate.h" int main(int...
  • BvinCN
  • BvinCN
  • 2013年09月02日 14:25
  • 608

IOS Application应用程序核心架构

一个新建的IOS application程序主要由以下文件组成:   main.m   *appDelegate.h/.m   MainWindow.xib   *info.plist   ...
  • omegayy
  • omegayy
  • 2012年02月21日 13:11
  • 3378
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在windows以及iOS中Registeringthe Application Handling the Custom Protocol 把应用程序注册到特定协议上
举报原因:
原因补充:

(最多只允许输入30个字)