是的,有一種方法可以做到這一點。如果目標類的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
該解決方案使用與作爲參數傳遞原始URL的新URL魔術initWithNavigatorUrl方法。我正在尋找一種方法來使用原始網址並訪問其參數。 – 2011-02-10 05:40:53