iPhone SDK: Check validity of URLs with NSURL not working?

前端 未结 5 2178
心在旅途
心在旅途 2020-12-12 03:52

I\'m trying to check if a given URL is valid and i\'m doing it like this:

- (BOOL)urlIsValid:(NSString *)address {
    NSURL *testURL = [NSURL URLWithString:         


        
5条回答
  •  孤街浪徒
    2020-12-12 04:32

    In my experience, the NSURL creation routines usually throw an exception instead of returning nil. However, the real question in this case is what constitutes a malformed URL? Are you checking whether a resource exists, or checking whether the structure of the string conforms to the relevant RFCs?

    With regards to the first issue I mentioned, when creating URLs that I don't manually enter myself I usually do this:

    @try
    {
        NSURL * url = [NSURL URLWithString: theString];
        // use the URL
    }
    @catch (NSException * e)
    {
        NSLog( @"URL creation error? %@ - %@", [e name], [e reason] );
        @throw;  // throw the exception again, to hopefully get your attention
    }
    

提交回复
热议问题