2013-06-14 13 views
6

Qual è il modo corretto per estrarre e rimuovere il nome dello schema e :// da un NSURL?Come estrarre e rimuovere il nome dello schema da NSURL?

Ad esempio:

note://Hello -> @"Hello" 
calc://3+4/5 -> @"3+4/5" 

così

NSString *scheme = @"note://"; 
NSString *path = @"Hello"; 

per un uso successivo in:

[[NSNotificationCenter defaultCenter] postNotificationName:scheme object:path]; 
+1

NSString * str = [urURL absoluteString]; NSString * string = [str stringByReplacingOccurrencesOfString: @ ": //" withString: @ ""] // prova questo – the1pawan

+0

Dai un'occhiata a [questo] (https://github.com/joeldev/JLRoutes/blob/master /JLRoutes/JLRoutes.m#L82). Analizza l'intero schema degli URL, ma ne ottieni l'essenza. – CodaFi

risposta

11

Potete guardare come questo (codice in gran parte non testato, ma si ottiene l'idea):

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
    NSLog(@"scheme: %@", [url scheme]); 
    NSLog(@"url: %@", url); 
    NSLog(@"query: %@", [url query]); 
    NSLog(@"host: %@", [url host]); 
    NSLog(@"path: %@", [url path]); 

    NSDictionary * dict = [self parseQueryString:[url query]]; 
    NSLog(@"query dict: %@", dict); 
} 

Così si può fare questo:

NSString * strNoURLScheme = 
[strMyURLWithScheme stringByReplacingOccurrencesOfString:[url scheme] withString:@""]; 

NSLog(@"URL without scheme: %@", strNoURLScheme); 

parseQueryString

- (NSDictionary *)parseQueryString:(NSString *)query 
{ 
    NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease]; 
    NSArray *pairs = [query componentsSeparatedByString:@"&"]; 

    for (NSString *pair in pairs) { 
     NSArray *elements = [pair componentsSeparatedByString:@"="]; 
     NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];   
     [dict setObject:val forKey:key]; 
    } 
    return dict; 
} 
+1

grazie, ottima risposta – Andres

+0

informazioni utili. Grazie – NightFury

5

La mia inclinazione è quello di rotolare il proprio stringa di ricerca di questo:

NSRange dividerRange = [urlString rangeOfString:@"://"]; 
NSUInteger divide = NSMaxRange(dividerRange); 
NSString *scheme = [urlString substringToIndex:divide]; 
NSString *path = [urlString substringFromIndex:divide]; 

Questo fa quello che si Ho chiesto in un modo abbastanza letterale, dividendo l'URL a metà intorno al suo schema me. Per una gestione più avanzata, dovrai fornire maggiori dettagli.

1

Ricorda, non combattere le strutture soprattutto quando si tratta di NSURL. Questa risposta SO ha una buona ripartizione delle sue capacità. https://stackoverflow.com/a/3693009/142358

di NSURL scheme e path proprietà sono esattamente quello che vuoi che (assumendo che il resto del tuo URL appare come un percorso) ti lascia con questo: ricerca necessaria

NSString *schemeWithDelimiter = [NSString stringWithFormat:@"%@://",[myURL scheme]]; 
[[NSNotificationCenter defaultCenter] postNotificationName: schemeWithDelimiter object:[myURL path]; 

No stringa!

Problemi correlati