Change a NSURL's scheme

前端 未结 6 2007
無奈伤痛
無奈伤痛 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 10:27

    I did it like this, using a variable resourceSpecifier in NSURL

    SWIFT

    var resourceSpecifier: String? { get } 
    

    OBJECTIVE-C

    @property(readonly, copy) NSString *resourceSpecifier Discussion 
    

    This property contains the resource specifier. For example, in the URL http://www.example.com/index.html?key1=value1#jumplink, the resource specifier is //www.example.com/index.html?key1=value1#jumplink (everything after the colon).

      -(NSURL*) URLByReplacingScheme
        {
            NSString *newUrlString = kHttpsScheme;
    
            if([self.scheme isEqualToString:kEmbeddedScheme])
                newUrlString = kHttpScheme;
    
            newUrlString = [newUrlString stringByAppendingString:[NSString stringWithFormat:@":%@", self.resourceSpecifier]];
    
            return [NSURL URLWithString:newUrlString];
        }
    

提交回复
热议问题