2017-10-21 72 views
0

我已经得到了我今天的扩展两个按钮。一个专门打开MainViewController,第二个需要导航到第二个ViewController。打开特定视图控制器从今天扩展

所以我做了一个appURL:

let appURL = NSURL(string: "StarterApplication://") 

,然后在第一个按钮,我呼吁:

 self.extensionContext?.open(appURL! as URL, completionHandler:nil) 

其中关于MainViewController打开的应用程序。

当点击Today Widget上的第二个按钮时,如何打开MainViewController并执行Segue到我的SecondViewController?

我为那个特定的ViewController做了第二个URL方案。我在其他simmilar话题看到,这可以通过调用来完成:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
} 
中的AppDelegate

,但没有任何想法如何使用它。

回答

-1

是的,你去好ü需要是在AppDelegate中返回到像这样的网址:

func application(_ app: UIApplication, open url: URL, options: 
[UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 

if _ = url { 

return true 
} 
return false 
} 

编辑:

你要配合捆绑标识的URL方案(应用程序的你的开放)在你的试图打开应用程序的plist中。

+0

你能提供更多的细节吗?在这个if语句中需要指定什么? – Maxxer

+0

我想说如果明确的应用程序试图打开应用程序。我正在检查url是否为零。而已。 –

+0

检查编辑。 –

0

使用一个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]; 
} 
1

我找到了解决方案。答案是深层链接。下面是我在的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。

相关问题