Change a NSURL's scheme

前端 未结 6 2008
無奈伤痛
無奈伤痛 2021-02-02 09:34

Is there an easy way to change the scheme of a NSURL? I do realize that NSURL is immutable. My goal is to change the scheme of an URL to \"https\" if t

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 10:27

    NSString *newUrlString =  [NSString stringWithFormat:@"https://%@%@", 
                              inURL.host, inURL.path];
    if (inURL.query) {
        newUrlString = [newUrlString stringByAppendingFormat:@"?%@", inURL.query];
    }
    return [NSURL URLWithString:newUrl];
    

    [NOTE] Code related to port and other fields handling are removal for simplicity.

提交回复
热议问题