Cordova 6.0 IOS在WebView中加载外部URL

时间:2016-03-16 13:01:34

标签: ios cordova

我是Cordova的新手,可以使用一些帮助做一个简单的任务。

我有这个hello world应用程序,我试图在应用程序WebView上加载外部URL,我使用的代码是cordova生成的代码,我对它做的唯一更改是:

  • 在index.html文件中添加了http://www.google.pt的链接。
  • <allow-navigation href="*" />添加到config.xml文件中。

我仔细阅读了文档,据我所知,这个非常简单的更改应该允许我在应用程序中加载URL,但事实并非如此。点击链接后,Safari会打开并加载谷歌。我需要它在应用程序内部加载,而不是在safari中加载。

我在android上测试了完全相同的东西,但效果很好。

这是我的config.xml文件:

&#13;
&#13;
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <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:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    
    <allow-navigation href="*" />
</widget>
&#13;
&#13;
&#13;

这是我的index.html文件:

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <div style="margin-top: 20px; border: 1px solid grey;">
            	<a href="http://www.google.pt/">Google!</a>
            </div>
        </div>
        
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>
&#13;
&#13;
&#13;

我正在使用cordova 6并使用ios 9.2在ios模拟器上进行测试,也使用8.1进行测试,但也无法正常工作。

任何想法都错了吗?

谢谢, 西皮

1 个答案:

答案 0 :(得分:3)

这是科尔多瓦的enter image description here,只需删除href中有http和https的<allow-intent href="http://*/*" /> <allow-intent href="https://*/*" />

 <?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />

    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>

    <allow-navigation href="*" />
</widget>

喜欢这个

public class Perimeter extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perimeter);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    public void PerimeterGame(View view) {
        Random rand = new Random();
        int number = rand.nextInt(12) + 1;
        TextView myText = (TextView) findViewById(R.id.rand1);
        String myString = String.valueOf(number);
        myText.setText(myString);


        Random rand2 = new Random();
        int number2 = rand2.nextInt(12) + 1;
        TextView myText2 = (TextView) findViewById(R.id.rand2);
        String myString2 = String.valueOf(number2);
        myText2.setText(myString2);

        ((TextView) findViewById(R.id.question)).setText
                ("Find the perimeter of a rectange with a width of " + myString + "cm" + " and " + "length of " + myString2 + "cm" + ".");
    }
        public void AnswerCheck(View view){

          int perimeter;

            Random randOne = new Random();
            int number = randOne.nextInt(12) + 1;
            TextView myText = (TextView) findViewById(R.id.rand1);
            String myString = String.valueOf(number);
            myText.setText(myString);


            Random randTwo = new Random();
            int number2 = randTwo.nextInt(12) + 1;
            TextView myText2 = (TextView) findViewById(R.id.rand2);
            String myString2 = String.valueOf(number2);
            myText2.setText(myString2);


            EditText num = (EditText)findViewById(R.id.answertext);
            int val = Integer.parseInt(num.getText().toString() );


            perimeter=(number+number2+number+number2);

            if(val==perimeter){
                Toast.makeText(this, "The answer is correct", Toast.LENGTH_SHORT).show();

            }
            else{

                Toast.makeText(this, "The answer is incorrect ", Toast.LENGTH_SHORT).show();
            }




    }

}