ios - 如何在 iOS 中管理被调用应用程序内部的 openUrl 方法?

标签 ios objective-c query-string openurl

我想这是重复的,但我想不通。

我必须使用 openUrl 方法从我的 iOS 应用调用其他应用。完成工作后,另一个应用程序必须使用相同的方法返回到我的应用程序。我想出了如何调用其他应用程序并打开我的应用程序。我的问题是如何拦截返回到我的应用程序。我需要检查查询字符串中的值。

我发现 handleOpenURL 方法拦截返回,我可以处理我的查询字符串。

我被卡住了 - 如何在我的 ViewController 中使用该信息?我在 viewDidLoad 中设置了断点,但没有命中。我必须使用哪种方法?

编辑:

我的代码是(在 AppDelegate 内):

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"url recieved: %@", url);
    NSLog(@"query string: %@", [url query]);
    NSLog(@"host: %@", [url host]);
    NSLog(@"url path: %@", [url path]);
    NSDictionary *dict = [self parseQueryString:[url query]];
    NSLog(@"query dict: %@", dict);

    return YES;
}

- (NSDictionary *)parseQueryString:(NSString *)query {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:6];
    NSArray *pairs = [query componentsSeparatedByString:@"&"];

    for (NSString *pair in pairs) {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [dict setObject:val forKey:key];
    }
    return dict;
}

效果很好。

在我的 ViewController (VC) 中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNeedsStatusBarAppearanceUpdate];

    // Instantiate App singleton
    singApp = [PESsingApplication sharedInstance];

    @try {

        // Localize resources using currently saved setting for language
        [self setLocalizedResources];

        // Init visual buttons
        [self baseInit];

        // Add code for keyboard management
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        _screenHeight = screenRect.size.height;
        _screenWidth = screenRect.size.width;

    }
    @catch (NSException *exception) {
        [self throwUnknownException:exception];
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

我的网址:

URL 标识符:xx.mydomain.MyUrlScheme

URL 方案:MyUrlScheme

我的 VC 中有断点(在上面显示的每个方法上)。

我使用以下字符串调用其他应用程序:@"otherApp://openApp?param1=value1&callbackUrl=MyUrlScheme";

他们使用 callbackUrl 参数从 otherApp 调用我。

最佳答案

您需要制作自己的自定义网址,请看下面

How to implement Custom URL Scheme

定义应用程序的自定义 URL 方案全部在 Info.plist 文件中完成。单击文件中的最后一行,然后单击右侧的“+”号以添加新行。为新项目选择 URL 类型。添加后,单击“URL 类型”旁边的灰色箭头以显示“项目 0”。将您的 URL 标识符设置为唯一的字符串 - 类似于 com.yourcompany.yourappname。

设置 URL 标识符后,选择该行并再次单击“+”号,然后为 URL Schemes 添加一个新项目。然后单击“URL 方案”旁边的灰色箭头以显示“项目 0”。将项目 0 的值设置为您的 URL 方案名称。

enter image description here

处理自定义 URL 调用

为了让您的应用程序在收到自定义 URL 调用时做出响应,您必须在应用程序委托(delegate)类中实现 application:handleOpenURL 方法:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    // your code
}

解析自定义 URL

一个 URL 有几个部分:

scheme://host/path?query

可以通过传递给 application:handleOpenURL 方法的 NSURL 对象来检索 URL 的各个部分。如果您有一个相当简单的 URL 命名方案并希望允许访问特定的页面/键,您可以只使用主机名:

[url host] 的自定义 URL 值:

myapp://page1   page1
myapp://page2   page2
myapp://otherPage   otherPage

要将数据传递到您的应用中,您需要使用查询字符串。这是从 url 解析查询字符串的简单方法:

- (NSDictionary *)parseQueryString:(NSString *)query {
    NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease];
    NSArray *pairs = [query componentsSeparatedByString:@"&"];

    for (NSString *pair in pairs) {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [dict setObject:val forKey:key];
    }
    return dict;
}

测试自定义 URL

您可以在模拟器中轻松测试您的 URL 方案。只需向您的其中一个 View 添加一个测试按钮,并按如下方式为其实现 IBAction 方法:

- (IBAction)getTest:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myappscheme://test_page/one?token=12345&domain=foo.com"]];
}

然后在您的应用委托(delegate)中,实现 application:handleOpenURL 方法:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    NSLog(@"url recieved: %@", url);
    NSLog(@"query string: %@", [url query]);
    NSLog(@"host: %@", [url host]);
    NSLog(@"url path: %@", [url path]);
    NSDictionary *dict = [self parseQueryString:[url query]];
    NSLog(@"query dict: %@", dict);
    return YES;
}

最后,如果您正在寻找可以在任何地方接收数据的方法,您可以使用这两种方案。 您可以简单地使用 Local notificationNSUserDefault

NSUserDefault

- (BOOL)application:(UIApplication *)application handleopenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
     NSUserDefaults *userDefaults=[[NSUserDefaults alloc] init];
     [userDefaults synchronize];
     NSString *status = [defaults stringForKey:@"any status"];
}

本地通知

- (BOOL)application:(UIApplication *)application handleopenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:VAL, @"value", nil];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

关于ios - 如何在 iOS 中管理被调用应用程序内部的 openUrl 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580791/

相关文章:

asp.net - 在 asp.net 4 中通过登录控件传递查询字符串?

ios - 如何在 iOS 上使用 google cloud vision OCR (swift)

ios - 显示提要中的评论数

iphone - 如何将下载的数据保存到设备的文件系统

iphone - Objective-C 表单框架

c# - 为什么 System.Uri 无法识别本地文件路径的查询参数?

ios - 可以为基于自定义 View 的 UIBarButtonItem 使用色调颜色吗?

ios - 如何查找指纹 Touch ID 计数?

objective-c - 懒惰的图像绘制

c# - 基于查询字符串参数名的路由