android - iOS 将 Cordova 应用程序作为 webview 嵌入到另一个应用程序中

标签 android ios swift cordova plugins

我在工作中有一个在 Cordova 中开发的应用程序,现在我被要求“插入”该应用程序,以便能够从另一个也将在 Cordova 中开发的应用程序打开它。总而言之,他们想从另一个应用程序打开一个应用程序。在 Android 中,我可以通过这样的 webview 来做到这一点:

  • 这两个应用程序都是用 Ionic 制作的。
  • 我编译它以获取 www 文件夹。
  • 我将 www 文件夹复制到插件项目的 src 文件夹中。
  • 对于 Android,我使用以下代码:

    PhonePlugin.java

    public class PhonePlugin extends CordovaPlugin {
    
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            actionExecutor(action, args);
            callbackContext.success("launched!");
        } catch (Exception e) {
            callbackContext.error(e.getLocalizedMessage());
        }
    
        return true;
    }
    
    private void actionExecutor(String action, JSONArray args) throws Exception {
        if ("start".equals(action)) {
            JSONObject jsonObject = new JSONObject(args.getString(0));
            final String poi = jsonObject.getString("poi");
            final String email = jsonObject.getString("email");
            final String apikey = jsonObject.getString("apikey");
    
            cordova.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    start(poi, email, apikey);
                }
            });
        } else if("finish".equals(action)) {
            cordova.getThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            });
        } else {
            throw new IllegalStateException("Unknown action: " + action);
        }
    }
    
    private void start(String poi, String email, String apikey) {
        Intent intent = new Intent(cordova.getActivity(), PhoneActivity.class);
        intent.putExtra(PhoneActivity.EMAIL, email);
        intent.putExtra(PhoneActivity.APIKEY, apikey);
        intent.putExtra(PhoneActivity.POI, poi);
    
        cordova.getActivity().startActivity(intent);
        cordova.getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
    
    private void finish() {
        cordova.getActivity().finish();
    }}
    

PhoneActivity.java

public class PhoneActivity extends CordovaActivity {
    public static String POI = "POI";
    public static String EMAIL = "EMAIL";
    public static String APIKEY = "APIKEY";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();

        String poi = getIntent().getStringExtra(POI);
        String email = getIntent().getStringExtra(EMAIL);
        String apikey = getIntent().getStringExtra(APIKEY);

        String indexUrl = "file:///android_asset/www/phoneplugin/index.html?poi=" + poi + "&apikey=" + apikey + "&email=" + email;
        loadUrl(indexUrl);
    }

}

使用此代码,它可以在 Android 上实现我想要实现的目标(不是文件夹 android_asset/www/phoneplugin ,而是我的“插件化”Ionic 应用程序中的 www 。有了这个,我可以使用按钮调用我的 start 方法来打开我的“插件化”应用程序,它运行完美,出现启动屏幕,然后应用程序运行(我真的不喜欢这个,但这是我的老板想要,所以 idc)。

现在我的问题出在 iOS 上。我对 Swift 一无所知,我只是进入“插件化”应用程序的启动屏幕,然后插件化应用程序关闭。

这是我到目前为止所拥有的,但不知道如何完成它,或者不知道它是否正确并且问题是其他的:

@objc(电话插件) PhonePlugin 类:CDVPlugin {

@objc(start:)
func start(_ command: CDVInvokedUrlCommand) {

    let json = try? JSON(data: jsonData)
    let authToken = json["token"]
    let key = json["secret_key"]

    let vc = CDVViewController(nibName: nil, bundle: nil)

    vc.startPage = "phoneplugin/index.html"

    let rootVC = UIApplication.shared.keyWindow?.rootViewController

    rootVC?.presentedViewController?.dismiss(animated: false, completion: nil)
    rootVC?.present(vc, animated: true, completion: nil)
}

@objc(finish:)
func finish(_ command: CDVInvokedUrlCommand) {
    let rootVC = UIApplication.shared.keyWindow?.rootViewController
    rootVC?.presentedViewController?.dismiss(animated: false, completion: nil)
}

}

我的plugin.xml(如果需要的话)如下:

<?xml version='1.0' encoding='utf-8'?>
<plugin id="ionic-phone-plugin" version="1.0.0" 
    xmlns="http://apache.org/cordova/ns/plugins/1.0" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <name>PhonePlugin</name>

    <js-module name="PhonePlugin" src="www/PhonePlugin.js">
        <clobbers target="window.cordova.plugins.PhonePlugin" />
    </js-module>

    <engines>
        <engine name="cordova" version=">=6.5.0" />
    </engines>

    <asset src="src/www" target="phoneplugin" />
    <dependency id="cordova-android-support-gradle-release" version="2.1.0" />
    <dependency id="cordova-android-play-services-gradle-release" version="3.0.0" />
    <dependency id="cordova-android-firebase-gradle-release" version="3.0.0" />
    <dependency id="cordova-plugin-bluetooth-serial" version="0.4.7" />
    <dependency id="cordova-plugin-geolocation" version="4.0.1" />
    <dependency id="cordova-plugin-insomnia" version="4.3.0" />
    <dependency id="cordova-plugin-network-information" version="2.0.1" />
    <dependency id="cordova-plugin-request-location-accuracy" version="2.2.3" />
    <dependency id="cordova-plugin-tts" version="0.2.3" />
    <dependency id="cordova.plugins.diagnostic" version="4.0.9" />
    <dependency id="cordova-sqlite-storage" version="2.3.3" />
    <dependency id="cordova-open-native-settings" version="1.5.2" />
    <dependency id="cordova-plugin-statusbar" version="2.4.2" />
    <dependency id="situm-cordova-plugin-official" version="1.14.2" />
    <dependency id="cordova-plugin-screen-orientation" version="3.0.1" />
    <dependency id="cordova-plugin-add-swift-support" version="1.7.2"/>

    <platform name="android">
        <config-file parent="/*" target="res/xml/config.xml">
            <feature name="PhonePlugin">
                <param name="android-package" value="com.test.phoneplugin.PhonePlugin" />
            </feature>
        </config-file>


        <config-file target="AndroidManifest.xml" parent="/manifest/application">
            <activity android:name="com.test.phoneplugin.PhonePluginActivity" android:screenOrientation="portrait" />
        </config-file>

        <source-file src="src/android/com/test/phoneplugin/PhonePlugin.java" target-dir="src/com/test/phoneplugin" />
        <source-file src="src/android/com/test/phoneplugin/PhoneActivity.java" target-dir="src/com/test/phoneplugin" />
    </platform>


    <platform name="ios">
        <config-file parent="/*" target="config.xml">
            <feature name="PhonePlugin">
                <param name="ios-package" value="PhonePlugin" />
            </feature>
        </config-file>

        <source-file src="src/ios/PhonePlugin.swift" />
    </platform>

</plugin>

我尝试安装此插件应用程序的 config.xml 是这样的:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.ionic.adadadasasdfffgg" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>MyApp</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-navigation href="*" />
    <preference name="ScrollEnabled" value="false" />
    <preference name="android-minSdkVersion" value="19" />
    <preference name="BackupWebStorage" value="none" />
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="FadeSplashScreenDuration" value="300" />
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="3000" />
    <platform name="android">
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
        <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
        <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
        <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
        <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
        <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
        <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
        <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
        <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
        <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
        <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
        <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <icon height="57" src="resources/ios/icon/icon.png" width="57" />
        <icon height="114" src="resources/ios/icon/icon@2x.png" width="114" />
        <icon height="40" src="resources/ios/icon/icon-40.png" width="40" />
        <icon height="80" src="resources/ios/icon/icon-40@2x.png" width="80" />
        <icon height="120" src="resources/ios/icon/icon-40@3x.png" width="120" />
        <icon height="50" src="resources/ios/icon/icon-50.png" width="50" />
        <icon height="100" src="resources/ios/icon/icon-50@2x.png" width="100" />
        <icon height="60" src="resources/ios/icon/icon-60.png" width="60" />
        <icon height="120" src="resources/ios/icon/icon-60@2x.png" width="120" />
        <icon height="180" src="resources/ios/icon/icon-60@3x.png" width="180" />
        <icon height="72" src="resources/ios/icon/icon-72.png" width="72" />
        <icon height="144" src="resources/ios/icon/icon-72@2x.png" width="144" />
        <icon height="76" src="resources/ios/icon/icon-76.png" width="76" />
        <icon height="152" src="resources/ios/icon/icon-76@2x.png" width="152" />
        <icon height="167" src="resources/ios/icon/icon-83.5@2x.png" width="167" />
        <icon height="29" src="resources/ios/icon/icon-small.png" width="29" />
        <icon height="58" src="resources/ios/icon/icon-small@2x.png" width="58" />
        <icon height="87" src="resources/ios/icon/icon-small@3x.png" width="87" />
        <icon height="1024" src="resources/ios/icon/icon-1024.png" width="1024" />
        <splash height="1136" src="resources/ios/splash/Default-568h@2x~iphone.png" width="640" />
        <splash height="1334" src="resources/ios/splash/Default-667h.png" width="750" />
        <splash height="2208" src="resources/ios/splash/Default-736h.png" width="1242" />
        <splash height="1242" src="resources/ios/splash/Default-Landscape-736h.png" width="2208" />
        <splash height="1536" src="resources/ios/splash/Default-Landscape@2x~ipad.png" width="2048" />
        <splash height="2048" src="resources/ios/splash/Default-Landscape@~ipadpro.png" width="2732" />
        <splash height="768" src="resources/ios/splash/Default-Landscape~ipad.png" width="1024" />
        <splash height="2048" src="resources/ios/splash/Default-Portrait@2x~ipad.png" width="1536" />
        <splash height="2732" src="resources/ios/splash/Default-Portrait@~ipadpro.png" width="2048" />
        <splash height="1024" src="resources/ios/splash/Default-Portrait~ipad.png" width="768" />
        <splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640" />
        <splash height="480" src="resources/ios/splash/Default~iphone.png" width="320" />
        <splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732" />
    </platform>
    <plugin name="cordova-plugin-whitelist" spec="1.3.3" />
    <plugin name="cordova-plugin-statusbar" spec="2.4.2" />
    <plugin name="cordova-plugin-device" spec="2.0.2" />
    <plugin name="cordova-plugin-splashscreen" spec="5.0.2" />
    <plugin name="cordova-plugin-ionic-webview" spec="^4.0.0" />
    <plugin name="cordova-plugin-ionic-keyboard" spec="^2.0.5" />
    <plugin name="cordova-plugin-googlemaps" spec="~2.4.4">
        <variable name="API_KEY_FOR_IOS" value="AIzaSyBpAjztTxH4ldWJBOWiJXvRZZsBVw0y_oI" />
        <variable name="API_KEY_FOR_ANDROID" value="AIzaSyD3RIddnImiOjtE0ep8mV6FrXEEcc6Uaz8" />
        <variable name="LOCATION_WHEN_IN_USE_DESCRIPTION" value="This app wants to get your location while this app runs only." />
        <variable name="LOCATION_ALWAYS_USAGE_DESCRIPTION" value="This app wants to get your location always, even this app runs in background." />
        <variable name="PLAY_SERVICES_VERSION" value="15.0.1" />
        <variable name="ANDROID_SUPPORT_V4_VERSION" value="27.+" />
    </plugin>
    <plugin name="ionic-phone-plugin" spec="/Users/macbookairdt/Desktop/test/PhonePlugin" />
    <engine name="browser" spec="~5.0.4" />
    <engine name="android" spec="~7.1.4" />
</widget>

有人知道为什么它不起作用吗?

谢谢!

最佳答案

我通过使用以下方法解决了这个问题:

vc.startPage = "file:///phoneplugin/index.html"

关于android - iOS 将 Cordova 应用程序作为 webview 嵌入到另一个应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232490/

相关文章:

java - 为什么RecyclerView在BottomNavigationView下面?

ios - Swift:如何从函数返回类类型

ios - UILabel 是否具有使其可选择的值(value)?

ios - 在 WKWebView 中将 EstimatedProgress/ProgressView 变成进度条

java - WebView - 如果存在后退历史记录,则仅显示后退按钮

php - 在Android(模拟器)中连接服务器数据库

java - 我可以在 Android 上实现自己的文件系统吗?

ios - TabBarViewController 的导航栏覆盖了导航 Controller 中的导航栏

ios - 将 Storyboard从 iphone 转换到 ipad

ios - 如何验证 IDFV (idenitifierForVendor) 的供应商?