4.3怎么解决跨域问题

1.同源策略如下:

URL

说明

是否允许通信

http://www.a.com/a.js

http://www.a.com/b.js

同一域名下

允许

http://www.a.com/lab/a.js

http://www.a.com/script/b.js

同一域名下不同文件夹

允许

http://www.a.com:8000/a.js

http://www.a.com/b.js

同一域名,不同端口

不允许

http://www.a.com/a.js

https://www.a.com/b.js

同一域名,不同协议

不允许

http://www.a.com/a.js

http://70.32.92.74/b.js

域名和域名对应ip

不允许

http://www.a.com/a.js

http://script.a.com/b.js

主域相同,子域不同

不允许

http://www.a.com/a.js

http://a.com/b.js

同一域名,不同二级域名(同上)

不允许(cookie这种情况下也不允许访问)

http://www.cnblogs.com/a.js

http://www.a.com/b.js

不同域名

不允许

特别注意两点:

第一,如果是协议和端口造成的跨域问题"前台"是无能为力的,

第二:在跨域问题上,域仅仅是通过"URL的首部"来识别而不会去尝试判断相同的ip地址对应着两个域或两个域是否在同一个ip上。

"URL的首部"指window.location.protocol +window.location.host,也可以理解为"Domains, protocols and ports must match"。

2. 前端解决跨域问题

1> document.domain + iframe      (只有在主域相同的时候才能使用该方法)

1) 在www.a.com/a.html中:

1

2

3

4

5

6

7

8

9

10

document.domain = 'a.com';

var ifr = document.createElement('iframe');

ifr.src = 'http://www.script.a.com/b.html';

ifr.display = none;

document.body.appendChild(ifr);

ifr.onload = function(){

    var doc = ifr.contentDocument || ifr.contentWindow.document;

    //在这里操作doc,也就是b.html

    ifr.onload = null;

};

2) 在www.script.a.com/b.html中:

1

document.domain = 'a.com';

2> 动态创建script

这个没什么好说的,因为script标签不受同源策略的限制。

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

function loadScript(url, func) {

  var head = document.head || document.getElementByTagName('head')[0];

  var script = document.createElement('script');

  script.src = url;

   

  script.onload = script.onreadystatechange = function(){

    if(!this.readyState || this.readyState=='loaded' || this.readyState=='complete'){

      func();

      script.onload = script.onreadystatechange = null;

    }

  };

   

  head.insertBefore(script, 0);

}

window.baidu = {

  sug: function(data){

    console.log(data);

  }

}

loadScript('http://suggestion.baidu.com/su?wd=w',function(){console.log('loaded')});

//我们请求的内容在哪里?

//我们可以在chorme调试面板的source中看到script引入的内容

3> location.hash + iframe

原理是利用location.hash来进行传值。

假设域名a.com下的文件cs1.html要和cnblogs.com域名下的cs2.html传递信息。

1) cs1.html首先创建自动创建一个隐藏的iframe,iframe的src指向cnblogs.com域名下的cs2.html页面

2) cs2.html响应请求后再将通过修改cs1.html的hash值来传递数据

3) 同时在cs1.html上加一个定时器,隔一段时间来判断location.hash的值有没有变化,一旦有变化则获取获取hash值

注:由于两个页面不在同一个域下IE、Chrome不允许修改parent.location.hash的值,所以要借助于a.com域名下的一个代理iframe

代码如下:

先是a.com下的文件cs1.html文件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function startRequest(){

    var ifr = document.createElement('iframe');

    ifr.style.display = 'none';

    ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';

    document.body.appendChild(ifr);

}

   

function checkHash() {

    try {

        var data = location.hash ? location.hash.substring(1) : '';

        if (console.log) {

            console.log('Now the data is '+data);

        }

    } catch(e) {};

}

setInterval(checkHash, 2000);

cnblogs.com域名下的cs2.html:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//模拟一个简单的参数处理操作

switch(location.hash){

    case '#paramdo':

        callBack();

        break;

    case '#paramset':

        //do something……

        break;

}

   

function callBack(){

    try {

        parent.location.hash = 'somedata';

    } catch (e) {

        // iechrome的安全机制无法修改parent.location.hash

        // 所以要利用一个中间的cnblogs域下的代理iframe

        var ifrproxy = document.createElement('iframe');

        ifrproxy.style.display = 'none';

        ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata';    // 注意该文件在"a.com"域下

        document.body.appendChild(ifrproxy);

    }

}

a.com下的域名cs3.html

1

2

//因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值

parent.parent.location.hash = self.location.hash.substring(1);

4> window.name + iframe

window.name 的美妙之处:name 值在不同的页面(甚至不同域名)加载后依旧存在,并且可以支持非常长的 name 值(2MB)。

1) 创建a.com/cs1.html

2) 创建a.com/proxy.html,并加入如下代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<head>

  <script>

  function proxy(url, func){

    var isFirst = true,

        ifr = document.createElement('iframe'),

        loadFunc = function(){

          if(isFirst){

            ifr.contentWindow.location = 'http://a.com/cs1.html';

            isFirst = false;

          }else{

            func(ifr.contentWindow.name);

            ifr.contentWindow.close();

            document.body.removeChild(ifr);

            ifr.src = '';

            ifr = null;

          }

        };

   

    ifr.src = url;

    ifr.style.display = 'none';

    if(ifr.attachEvent) ifr.attachEvent('onload', loadFunc);

    else ifr.onload = loadFunc;

   

    document.body.appendChild(iframe);

  }

</script>

</head>

<body>

  <script>

    proxy('http://www.baidu.com/', function(data){

      console.log(data);

    });

  </script>

</body>

3) 在b.com/cs1.html中包含:

1

2

3

<script>

    window.name = '要传送的内容';

</script>

5> postMessage(HTML5中的XMLHttpRequest Level 2中的API)

1) a.com/index.html中的代码:

1

2

3

4

5

6

7

8

9

<iframe id="ifr" src="b.com/index.html"></iframe>

<script type="text/javascript">

window.onload = function() {

    var ifr = document.getElementById('ifr');

    var targetOrigin = 'http://b.com';  // 若写成'http://b.com/c/proxy.html'效果一样

                                        // 若写成'http://c.com'就不会执行postMessage

    ifr.contentWindow.postMessage('I was there!', targetOrigin);

};

</script>

2) b.com/index.html中的代码:

1

2

3

4

5

6

7

8

9

10

<script type="text/javascript">

    window.addEventListener('message', function(event){

        // 通过origin属性判断消息来源地址

        if (event.origin == 'http://a.com') {

            alert(event.data);    // 弹出"I was there!"

            alert(event.source);  // a.comindex.htmlwindow对象的引用

                                  // 但由于同源策略,这里event.source不可以访问window对象

        }

    }, false);

</script>

6> CORS

CORS背后的思想,就是使用自定义的HTTP头部让浏览器与服务器进行沟通,从而决定请求或响应是应该成功,还是应该失败。

IE中对CORS的实现是xdr

1

2

3

4

5

6

7

var xdr = new XDomainRequest();

xdr.onload = function(){

    console.log(xdr.responseText);

}

xdr.open('get', 'http://www.baidu.com');

......

xdr.send(null);

其它浏览器中的实现就在xhr中

1

2

3

4

5

6

7

8

9

10

11

var xhr =  new XMLHttpRequest();

xhr.onreadystatechange = function () {

    if(xhr.readyState == 4){

        if(xhr.status >= 200 && xhr.status ){

            console.log(xhr.responseText);

        }

    }

}

xhr.open('get', 'http://www.baidu.com');

......

xhr.send(null);

实现跨浏览器的CORS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

function createCORS(method, url){

    var xhr = new XMLHttpRequest();

    if('withCredentials' in xhr){

        xhr.open(method, url, true);

    }else if(typeof XDomainRequest != 'undefined'){

        var xhr = new XDomainRequest();

        xhr.open(method, url);

    }else{

        xhr = null;

    }

    return xhr;

}

var request = createCORS('get', 'http://www.baidu.com');

if(request){

    request.onload = function(){

        ......

    };

    request.send();

}

7> JSONP

JSONP包含两部分:回调函数和数据。

回调函数是当响应到来时要放在当前页面被调用的函数。

数据就是传入回调函数中的json数据,也就是回调函数的参数了。

1

2

3

4

5

6

7

8

9

10

11

12

13

function handleResponse(response){

    console.log('The responsed data is: '+response.data);

}

var script = document.createElement('script');

script.src = 'http://www.baidu.com/json/?callback=handleResponse';

document.body.insertBefore(script, document.body.firstChild);

/*handleResonse({"data": "zhe"})*/

//原理如下:

//当我们通过script标签请求时

//后台就会根据相应的参数(json,handleResponse)

//来生成相应的json数据(handleResponse({"data": "zhe"}))

//最后这个返回的json数据(代码)就会被放在当前js文件中被执行

//至此跨域通信完成

jsonp虽然很简单,但是有如下缺点:

1)安全问题(请求代码中可能存在安全隐患)

2)要确定jsonp请求是否失败并不容易

8> web sockets

web sockets是一种浏览器的API,它的目标是在一个单独的持久连接上提供全双工、双向通信。(同源策略对web sockets不适用)

web sockets原理:在JS创建了web socket之后,会有一个HTTP请求发送到浏览器以发起连接。取得服务器响应后,建立的连接会使用HTTP升级从HTTP协议交换为web sockt协议。

只有在支持web socket协议的服务器上才能正常工作。

1

2

3

4

5

var socket = new WebSockt('ws://www.baidu.com');//http->ws; https->wss

socket.send('hello WebSockt');

socket.onmessage = function(event){

    var data = event.data;

}

   

来自 <http://web.jobbole.com/88524/>

   

在uni-app开发中,针对iOS App Store审核时可能会遇到的兼容性问题,尤其是与iOS 4.3相关的审核要求,开发者需要特别注意以下几点: ### 一、iOS 4.3 审核问题及兼容性要求 iOS App Store审核过程中,苹果公司会依据其《App Store Review Guidelines》对应用进行严格审查。对于使用uni-app开发的应用程序,需确保满足以下关键点: - **最低版本支持**:尽管iOS 4.3已较为老旧,但在某些特殊场景下,仍需关注其兼容性问题。uni-app默认支持现代iOS版本,因此建议明确声明支持的最低iOS版本,并避免使用过时的API[^1]。 - **隐私政策合规性**:从iOS 10开始,苹果加强了对用户隐私的保护,特别是在涉及位置、相机、相册等权限时。开发者需在`manifest.json`或原生配置文件中正确声明所需权限,并提供清晰的隐私政策说明,否则可能导致审核被拒[^1]。 - **UI适配问题**:iOS设备存在刘海屏、全面屏等不同形态,uni-app提供了安全区适配方案,通过设置`safeArea`属性可有效规避界面元素被系统控件遮挡的问题。同时,应避免使用硬编码尺寸,而采用动态适配方式[^1]。 - **性能优化**:苹果对应用启动速度、内存占用、帧率稳定性等方面有较高要求。uni-app项目可通过减少资源体积、合理使用懒加载、优化页面切换动画等方式提升性能表现[^1]。 ### 二、uni-app 中解决iOS审核问题的实践建议 - **权限请求时机控制**:在首次使用相关功能时再请求权限,而非启动即弹窗,以提升用户体验并符合审核规范。 - **使用官方推荐的UI框架**:uni-app支持多种UI库,如uView、Thor UI等,选择经过验证的组件库有助于减少兼容性问题和样式错乱风险[^1]。 - **WebView通信优化**:若项目中使用了`web-view`组件,需确保通过`postMessage`实现的安全通信机制,避免因或非法调用导致功能异常[^1]。 - **构建前的检查清单**: - 确保所有第三方SDK均兼容当前目标iOS版本; - 检查是否遗漏必要的App Store元数据(如截图、描述); - 验证是否启用了Bitcode(部分情况下需关闭); - 使用Xcode Archive工具进行归档构建,并通过App Store Connect提交测试飞行版进行预审。 ### 三、示例代码:uni-app iOS隐私权限配置 ```json // manifest.json 中配置权限声明 { "plus": { "distribute": { "ios": { "CFBundleShortVersionString": "1.0", "NSLocationWhenInUseUsageDescription": "本应用需要访问您的位置以提供附近服务", "NSCameraUsageDescription": "本应用需要访问您的摄像头以上传照片" } } } } ``` ### 四、常见iOS审核拒绝原因及对策 - **“Your app uses non-public APIs”**:确保未使用任何私有API或越狱检测逻辑。 - **“Missing Push Notification Entitlement”**:如果使用了推送功能,需在Apple Developer后台启用Push Notifications能力,并正确配置证书。 - **“Crash on Launch”**:在真实设备上进行全面测试,确保无初始化阶段崩溃问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值