Coder Social home page Coder Social logo

Comments (7)

karosLi avatar karosLi commented on July 26, 2024

嗯,你这个我要先测试下

from kkjsbridge.

karosLi avatar karosLi commented on July 26, 2024

你好,我不太清楚,你是需要ajax请求一个本地资源吗,这是什么样的场景?

from kkjsbridge.

ZhangKejun avatar ZhangKejun commented on July 26, 2024

你好,我不太清楚,你是需要ajax请求一个本地资源吗,这是什么样的场景?

我这边的场景是浏览器加载网页,网页可能是本地地址,也可能是网络地址;然后就遇到下面这两种情况,目前你的代码满足不了,所以我做了调整。上次的代码有缺陷,这次又做了一些调整;

NSURL *nativeURL = [NSURL URLWithString:url];
if (!nativeURL.scheme) {
    /*------------兼容的代码 start------------*/
    // 说明是相对路径,一般是资源文件

// 示例1
// {
// async = 1;
// host = "m.feng1.com";
// href = "https://m.feng1.com/mall/app/index.html?channel=xxx&ddsds=ewew2222";
// id = 632;
// method = GET;
// port = "";
// referer = "";
// scheme = "https:";
// url = "/api/mall/channelConfig/getChannelThemeConfig?channel=SFIM&_=1578133077103";
// useragent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A366";
// }
//
// 示例2
// {
// async = 1;
// host = "";
// href = "file:///Users/xxxx/Library/Developer/CoreSimulator/Devices/B9133CAF-51E9-456E-92F6-FAC6EFC676AB/data/Containers/Data/Application/5BCBB3E7-A35F-4BCD-89B7-A79474C0E2C8/Documents/c6a5a31f3f1d9e446353e0425ab55c3e/2e6cda38b47d8c6ec672a1ed4f66cfe8/b8c37e33defde51cf91e1e03e51657da/UnityFile/Cache/serviceApp/packages/salary/f99bb68cfd2680c098a287fca1182030/www/index.html#/dsdsds/ddd?key=xxx&eewwe";
// id = 496;
// method = GET;
// port = "";
// referer = "";
// scheme = "file:";
// url = "views/salaryInquiry/ssf-account-edit.html";
// useragent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A366";
// }

    // 先按照?截取,然后按照#截取
    NSString* path = [href componentsSeparatedByString:@"?"][0]; // 按“?”进行分割,获取第一个字符串
    path = [path componentsSeparatedByString:@"#"][0]; // 按“#”进行分割,获取第一个字符串
    path = [path stringByDeletingLastPathComponent]; // 删除最后一个“/”之后的内容,获取到当前文件的相对路径

    if ([scheme hasPrefix:@"file:"]) {
        // 本地相对路径
        if (![url hasPrefix:@"/"]){
            path = [NSString stringWithFormat:@"%@/",path];
        }
        // 完整路径
        url = [NSString stringWithFormat:@"%@%@",path,url];
    }else{
        // 网络相对路径
        if (![url hasPrefix:@"/"]){
           url = [NSString stringWithFormat:@"%@%@",path,url];
        }else {
           // 网络路径,拼接host
           NSString *tmpPort = port.length > 0 ? [NSString stringWithFormat:@":%@", port] : @"";
           url = [NSString stringWithFormat:@"%@//%@%@%@",scheme, host, tmpPort, url];
        }
    }
    /*-----------兼容的代码 end-------------*/
    /*-----------下面是你原来的代码--------*/

// NSFileManager* fm = [NSFileManager defaultManager];
//
// if (nativeURL.pathComponents > 0) {
// if (nativeURL.host) {
// url = [NSString stringWithFormat:@"%@%@",scheme, url];
// } else {
// NSString *tmpPath = [url hasPrefix:@"/"] ? url : [NSString stringWithFormat:@"/%@", url];
// NSString *tmpPort = port.length > 0 ? [NSString stringWithFormat:@":%@", port] : @"";
// url = [NSString stringWithFormat:@"%@//%@%@%@",scheme, host, tmpPort, tmpPath];
// }
// } else {
// url = href;
// }

}

from kkjsbridge.

karosLi avatar karosLi commented on July 26, 2024

嗯,你提一个PR吧

from kkjsbridge.

ZhangKejun avatar ZhangKejun commented on July 26, 2024

嗯,你提一个PR吧

已经提交,请查收。顺带提交了一些其他的内容

from kkjsbridge.

karosLi avatar karosLi commented on July 26, 2024

嗯,好的,不好意思,这么久才回复。我review了下,你的提交,也给了下建议,你看看呢。

from kkjsbridge.

karosLi avatar karosLi commented on July 26, 2024

现在没有路径问题了

from kkjsbridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.