繁体   English   中英

从Today扩展中打开特定的ViewController

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM