从URL打开iOS应用并传递参数

25

一个链接应该打开应用程序。我已经做到了。我只想知道如何传递参数。假设URL是“addappt:///?code=abc”。当视图控制器弹出时,代码字段应该有填充文本-等号后面的字母。我已经部分实现了这个功能。我使用以下代码(在app delegate.m中):

NSArray *elements = [url.query componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
          val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

顺便提一下: val在appdelegate.h中声明了。

我也能将val传递给视图控制器。我的问题只是如何填充名为'code'的文本字段。如何在应用程序通过链接打开时立即填充代码?

感谢您的帮助。


1
抱歉,我无法提供代码,因为您没有提供任何需要翻译的上下文或具体要求。请提供更多信息以便我能够帮助您。 - DHShah01
1
我知道如何填充它,只需要知道哪个方法。 - DHShah01
1
在AppDelegate中,有“viewController.code setText:val;”,假设code是一个文本字段,并且作为viewController的属性可用。 - Srikanth
2个回答

23

这是一篇关于在iOS中使用自定义URL Scheme的好教程

就像在教程中所述,您应该解析URL参数并将它们存储到此方法中以便在应用程序中使用:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  // Do something with the url here
}

1
@AmoghTalpallikar 很抱歉。感谢您指出这个问题。现在应该可以正常工作了。 - Nikola Kirev
是否可能创建这样的URL,如果应用未安装,则将用户发送到App Store。 - nr5
4
苹果建议实现方法 application:openURL:sourceApplication:annotation: 来处理自定义的URL。 - Juan Catalan
Swift 3,XCode 8,该方法为: func application(_ app: UIApplication, open url: URL, options: [String : AnyObject] = [:]) -> Bool 干杯 - Sasho
但是通过邮件中的URL无法打开,因为该URL被转换为chatapp/。 - lokesh

0
在 Xcode 12 上,这段代码完美运行。你可以想象它也是一个常规的 URL。在源应用程序中,您可以调用并打开带有参数的目标 URL。
    let url = URL(string: "DestinationApp:PATH?PARAMETER=11111")
           
    UIApplication.shared.open(url!) { (result) in
        if result {
            print(result)
           // The URL was delivered successfully!
        }
    }

目标应用程序可以使用此方法在AppDelegate中处理metod。警报是为了双重检查。

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
            // Determine who sent the URL.
            let sendingAppID = options[.sourceApplication]
            print("source application = \(sendingAppID ?? "Unknown")")
    
            // Process the URL.
            guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
                let path = components.path,
                let params = components.queryItems else {
                    print("Invalid URL or path missing")
                    return false
            }
    
            if let parameter = params.first(where: { $0.name == "PARAMETER" })?.value {
                print("path = \(path)")
                print("parameter = \(parameter)")
    
                let alert = UIAlertController(title: "Path = \(path)", message: "Parameter = \(parameter)", preferredStyle: .alert)
    
                let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    
                if var topController = keyWindow?.rootViewController {
                    while let presentedViewController = topController.presentedViewController {
                        topController = presentedViewController
                    }
    
                    topController.present(alert, animated: true, completion: nil)
                }
                return true
            } else {
                print("parameter is missing")
                return false
            }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接