2011-01-30 87 views
0

我有不同數量的查詢參數的URL:有沒有辦法在Three20中用查詢參數註冊url?

myapp://profile?username=1&status=2&title=3 

我想註冊像這樣與TTUrlMap

[map from:@"myapp://profile*" toViewController:[ProfileController class]]; 

而且我想Three20承認「的其餘部分網址」,要麼調用類似:

initWithOriginalUrl:(NSString*) originalUrl 

在那裏我可以再解析查詢參數或:

initWithQueryParams(NSDictionary*) queryParams 

TTNavigator已經識別出我的網址,將參數解析成地圖,然後調用我的控制器傳遞查詢參數?

這是支持嗎?我寧願不將編碼的URL作爲param傳遞,如下所示:Pass URL Question

回答

1

我要這個回答我 - 關鍵是瞭解initWithNavigatorURL - Three20將解析查詢參數,如果未在地圖中明確設置方法,則調用此方法並將解析的參數傳遞給它。所以,解決的辦法是把它添加到您的地圖:

[map from:@"myapp://profile" toViewController:[ProfileController class]]; 

並實現你的toViewController

@implementation ProfileController 

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query { 
    NSLog(@"ProfileController initWithNavigatorUrl %@, %@", URL, query); 
    .... 
3

是的,有一種方法可以做到這一點。如果目標類的URL的是,例如,ProfileController,然後註冊的網址是這樣的:

[map from:@"myapp://profile?originalURL=(initWithOriginalURL:)" 
    toViewController:[ProfileController class]]; 

正如你所看到的,命名originalURL查詢參數的值將作爲第一個參數的傳遞函數稱爲initWithOriginalURL:。因此,在ProfilerController,聲明功能:

- (id)initWithOriginalURL:(NSString*)originalURL { 
    // Initialize your controller. For example, you might do this: 
    if (self = [self initWithNibName:nil bundle:nil]) { 
     self.variableHeightRows = YES; 

     self.dataSource = 
      [TTListDataSource dataSourceWithObjects: 
      [TTTableLongTextItem itemWithText:[NSString stringWithFormat:@"Original URL is %@", originalURL]], 
      nil]; 
    } 

    return self; 
} 

所以,你可以打開的URL看起來像myapp://profile?originalURL=URL_GOES_HERE。請注意,就像互聯網上的URL一樣,對任何和所有查詢參數進行URL編碼都很重要。因此,這裏是將打開上面的ProfileController可的代碼示例:

// any URL goes here -- this is the query parameter we are going to 
// pass as the "originalURL=..." parameter. 
NSString* url = @"http://www.google.com/search?hl=en&q=stack+overflow"; 

// URL-encode it: turn most non-alphanumerics into %XX 
NSString * encodedURL = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL, 
    (CFStringRef)url, 
    NULL, 
    (CFStringRef)@"!*'\"();:@&=+$,/?%#[] ", 
    kCFStringEncodingUTF8); 

// Open the URL 
TTOpenURL([NSString stringWithFormat:@"myapp://profile?originalURL=%@", encodedURL]); 

在這種情況下,encodedURL最終會被:

myapp://profile?originalURL=http%3A%2F%2Fwww.google.com%2Fsearch%3Fhl%3Den%26q%3Dstack%2Boverflow 
+0

該解決方案使用與作爲參數傳遞原始URL的新URL魔術initWithNavigatorUrl方法。我正在尋找一種方法來使用原始網址並訪問其參數。 – 2011-02-10 05:40:53

相關問題