IOS实现Custom Protocol

在IOS中,实现Custom Protocol,需要以下两步:

1、修改Info.plist文件,增加以下内容:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.neohope.custom.protocol.test</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myproto</string>
            </array>
        </dict>
    </array>

2、在AppDelegate.m中,增加以下内容:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if(url!=nil)
    {
        NSString *path = url.absoluteString;
        //do your job here
    }
    return YES;
}

Andoid实现Custom Protocol

Android中实现Custom Protocol,只需要简单的两步:

1、修改AndroidManifest.xml文件,在activity节增加以下内容:

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="myproto"/>
            </intent-filter>

2、修改对应activity中,用以下代码,就可以处理URL中的数据啦

        Intent intent = getIntent();
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            String user = uri.getQueryParameter("user");
            String password = uri.getQueryParameter("password");
            //do your job here
        }

Andoid调用Custom Protocol

    String textUrl = "myprotocal://192.168.0.1:8080/test?username=user&password=user";
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(textUrl));
    intent.addCategory(Intent. CATEGORY_BROWSABLE);
    intent.addCategory(Intent. CATEGORY_DEFAULT);
    startActivity(intent)