![](/img/trans.png)
[英]Load specific ViewController from Storyboard in iOS 8 Today Extension
[英]Opening specific ViewController from Today Extension
今天的擴展程序上有兩個按鈕。 一個專用於打開MainViewController,第二個需要導航至第二個ViewController。
所以我做了一個appURL:
let appURL = NSURL(string: "StarterApplication://")
然后在第一個按鈕上調用:
self.extensionContext?.open(appURL! as URL, completionHandler:nil)
這將在MainViewController上打開應用。
輕按“今日”小部件上的第二個按鈕時,如何打開MainViewController並對SecondViewController進行performSegue?
我為該特定ViewController設置了第二個URL方案。 我在其他類似主題中看到可以通過調用來完成:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
}
在AppDelegate中,但不知道如何使用它。
使用一種URL方案。 您可以為不同的任務添加不同的路徑或參數。
例如,我有一個應用程序,可以在“今日”擴展程序中顯示多個項目。 如果點擊某個項目,則將打開該應用程序。
- (void)_openItem:(SPItem *)item
{
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"myapp://localhost/open_item?itemID=%@", item.itemID]];
if (url != nil)
{
[self.extensionContext openURL:url completionHandler:nil];
}
}
正如您在問題中已經提到的那樣,您需要實現- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
就我而言,它或多或少看起來像這樣:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
BOOL ret;
if ([url.scheme isEqualToString:@"myapp"])
{
if ([url.path isEqual:@"open_item"])
{
@try
{
NSDictionary *query = [url.query queryDictionary];
NSString* itemID = query[@"itemID"];
[self _gotoItemWithID:itemID completionHandler:nil];
}
@catch (NSException *exception) {
}
ret = YES;
}
}
else
{
ret = NO;
}
return ret;
}
如您所見,我首先檢查是否為url方案。 如果這是我所期望的,我會檢查路徑。 通過使用不同的路徑,我能夠實現不同的命令 ,今天的擴展能夠執行 。 每個命令可以具有不同的參數。 如果使用“ open_item”命令,則需要參數“ itemID”。
通過返回YES,您告訴iOS您能夠處理調用您的應用程序的URL。
在我的應用程序[self _gotoItemWithID:itemID completionHandler:nil]
執行了所有需要的任務以顯示項目。 在您的情況下,您需要一個函數來顯示第二個視圖控制器。
編輯:
我忘了提到queryDictionary
是NSString擴展中的方法。 它需要一個字符串( self
),並嘗試提取URL參數並將其作為字典返回。
- (NSDictionary*)queryDictionary
{
NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
NSScanner* scanner = [[NSScanner alloc] initWithString:self];
while (![scanner isAtEnd])
{
NSString* pairString;
[scanner scanUpToCharactersFromSet:delimiterSet
intoString:&pairString] ;
[scanner scanCharactersFromSet:delimiterSet intoString:NULL];
NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
if ([kvPair count] == 2)
{
NSString* key = [[kvPair objectAtIndex:0] stringByRemovingPercentEncoding];
NSString* value = [[kvPair objectAtIndex:1] stringByRemovingPercentEncoding];
[pairs setObject:value forKey:key] ;
}
}
return [NSDictionary dictionaryWithDictionary:pairs];
}
我找到了解決方案。 答案是深層鏈接。 這是我在appDelegate中的方法:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let urlPath : String = url.path as String!
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if(urlPath == "/Action"){
let ActionView: ActionViewController = mainStoryboard.instantiateViewController(withIdentifier: "ActionViewController") as! ActionViewController
self.window?.rootViewController = ActionView
} else if(urlPath == "/VoiceCommandView") {
let VoiceCommandView: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window?.rootViewController = VoiceCommandView
}
self.window?.makeKeyAndVisible()
return true
在TodayExtensionViewController中,我定義了兩個URL方案,它們具有相同的主機,但URL路徑不同。 並做了一個簡單的:
self.extensionContext?.open(startAppURL! as URL, completionHandler:nil)
對於第一個按鈕,但更改了第二個按鈕的urlPath。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.