破晓 发表于 2015-1-19 11:17:35

iOS自定义URL方案解析

在ios中程序间互相调用可以通过url来解决。在oc里面直接在函数handleOpenURL 便可,但是在ane中就没那么简单了。下面详细介绍下air项目中url的设置和使用,使用支付宝快捷支付ane作为例子。网上有稀稀疏疏的几篇文章偶尔提及,但是并没有完全给出一个DEMO来.在此之前请详细阅读官方文档:传送门关于打开URL的ANE例子:传送门IOS里OC的处理方式:传送门在AIR中配置URL供其他程序调用
1.首先需要在-app.xml中加入如下配置:(标签中)<font color="rgb(51, 51, 51)"><iPhone>
      <InfoAdditions><![CDATA[
            <key>UIDeviceFamily</key>
            <array>
                <string>1</string>
                <string>2</string>
            </array>
            <key>CFBundleURLTypes</key>
            <array>
            <dict>
               <key>CFBundleURLSchemes</key>
                <array>
                  <string>AlipayANE</string>
                </array>
                <key>CFBundleURLName</key>
                <string>com.rect.app</string>
            </dict>
      </array>
      ]]></InfoAdditions>
      <requestedDisplayResolution>high</requestedDisplayResolution>
    </iPhone></font>在这类配置了一个Name为”com.rect.app”的Schemes.调用方式为”AlipayANE://”;
2.在AIR项目代码中加入事件监控,如下:protected function handler_appComp($evt:FlexEvent):void
            {
                                 //ANE回调侦听
                AlipayExtension.getInstance().addEventListener(StatusEvent.STATUS,handler_status);
                //URL回调侦听
                NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE,onURLHandle);
            }

            protected function onURLHandle(e:InvokeEvent):void
            {
                if(e.arguments.length>0){
                  trace(e.arguments);
                  var str:String = e.arguments as String;//把URL用String字符串的方式传给OC端
                  AlipayExtension.getInstance().AlipayURLHandle_JustIOS(str);
                }
            }3.然后在OC中如下获取URL对象:ANE_FUNCTION(AlipayUrl)
{
    NSLog(@"Entering AlipayUrl()");
    FREObject fo = NULL;
    context = ctx;
    [ sendMegToAs:context code:@"AlipayUrl" level:@"alipay AlipayUrl begin"];
    //获取URL的字符串
    NSString * urlStr = getStringFromFREObject(argv);
    //支付宝相关参数 这里忽略
    NSString * _allpayPubKey = getStringFromFREObject(argv);
    //NSString to NSURL
    NSURL * _alipayURL = ;
    //拿到URL后 做该做的事情(具体做什么 SDK会告诉你)
    [ parse:_alipayURL _allpayPubKey:_allpayPubKey _context:context];
    [ sendMegToAs:context code:@"AlipayUrl" level:@"alipay AlipayUrl ending"];
    NSLog(@"ending AlipaySignCheck()");
    return fo;
}
}就支付宝来说,如此便可在支付操作完成之后成功返回AIR应用程序.
具体的请详细阅读支付宝快捷支付ANE源码:传送门enjoy your code
本文来自:http://shadowkong.com/archives/1338

页: [1]
查看完整版本: iOS自定义URL方案解析