URL 方案不适用于本机 iOS 应用程序
URL Scheme not working from native iOS app
我有一个自定义的 URL 方案和一个注册此方案以供识别的目标应用程序。
当我在移动版 Safari 中启动 WEB 应用程序并在 WEB 应用程序中按下按钮时,按钮会提供 URL link,并启动专用的 TARGET 应用程序 -这是期望的行为。
但是,如果我启动本机 SOURCE 应用程序,并在 UIButton 上执行一个操作,然后我调用 appdelegate 来打开URL 并传递来自网络的相同 url应用程序,目标应用程序未启动。
[UIApplication canOpenURL] 检查甚至 returns 否,这很奇怪,因为 TARGET App DID 正确注册了自定义 URL 方案,否则它将无法在网络应用程序中运行。
有什么帮助吗?
PS:SOURCE 和 TARGET 只是 SO 的方便名称。
更新:
- (IBAction)handleAction:(id)sender
{
NSString *urlString = @"nda://nda.undernda/actionundernda?someparamters..";
BOOL isURLSchemeRegistered = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]];
if (isURLSchemeRegistered == YES)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
else
{
//go to appstore
}
}
好的,所以问题是他 url 的字符没有使用 UTF8 编码进行转义。
解决方案:
NSString *urlString = @"sorry..can't show this :(";
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url =[ NSURL URLWithString:escapedUrlString];
[[UIApplication sharedApplication] openURL:url];
我有一个自定义的 URL 方案和一个注册此方案以供识别的目标应用程序。
当我在移动版 Safari 中启动 WEB 应用程序并在 WEB 应用程序中按下按钮时,按钮会提供 URL link,并启动专用的 TARGET 应用程序 -这是期望的行为。
但是,如果我启动本机 SOURCE 应用程序,并在 UIButton 上执行一个操作,然后我调用 appdelegate 来打开URL 并传递来自网络的相同 url应用程序,目标应用程序未启动。 [UIApplication canOpenURL] 检查甚至 returns 否,这很奇怪,因为 TARGET App DID 正确注册了自定义 URL 方案,否则它将无法在网络应用程序中运行。
有什么帮助吗?
PS:SOURCE 和 TARGET 只是 SO 的方便名称。
更新:
- (IBAction)handleAction:(id)sender
{
NSString *urlString = @"nda://nda.undernda/actionundernda?someparamters..";
BOOL isURLSchemeRegistered = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]];
if (isURLSchemeRegistered == YES)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
else
{
//go to appstore
}
}
好的,所以问题是他 url 的字符没有使用 UTF8 编码进行转义。
解决方案:
NSString *urlString = @"sorry..can't show this :(";
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url =[ NSURL URLWithString:escapedUrlString];
[[UIApplication sharedApplication] openURL:url];