关闭

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

674人阅读 评论(0) 收藏 举报

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又是如何实现程序协议化访问的:)

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:767956次
    • 积分:11914
    • 等级:
    • 排名:第1231名
    • 原创:321篇
    • 转载:654篇
    • 译文:30篇
    • 评论:39条
    友情链接
    友情链接 二
    最新评论