Objective-C - Finding a URL within a string

后端 未结 3 1596
半阙折子戏
半阙折子戏 2020-12-14 03:32

Given a large string, what is the best way to create an array of all valid urls which are contained within the string?

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 04:24

    This is best way to extract url link.

    NSString *url_ = @"dkc://name.com:8080/123;param?id=123&pass=2#fragment";
    
    NSURL *url = [NSURL URLWithString:url_];
    
    NSLog(@"scheme: %@", [url scheme]);
    
    NSLog(@"host: %@", [url host]);
    
    NSLog(@"port: %@", [url port]);
    
    NSLog(@"path: %@", [url path]);
    
    NSLog(@"path components: %@", [url pathComponents]);
    
    NSLog(@"parameterString: %@", [url parameterString]);
    
    NSLog(@"query: %@", [url query]);
    
    NSLog(@"fragment: %@", [url fragment]);
    

    Output:

    scheme: dkc

    host: name.com

    port: 8080

    path: /12345

    path components: ( "/", 123 ) parameterString: param

    query: id=1&pass=2

    fragment: fragment

提交回复
热议问题