iOS自定义URL Scheme

4
我正在创建一个应用程序,使用自定义的URL方案。我已经设置好了一切,并且在打开应用程序时它可以正常工作。但是,现在我想要能够向URL添加一个单独的字符串,以便打开应用程序的人可以看到该字符串。我真的很困扰,请有人帮助我吗?
这是我的代码:
- (NSDictionary*)parseURLParams:(NSString *)query
{
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *pair in pairs)
{
    NSArray *kv = [pair componentsSeparatedByString:@"="];
    NSString *val = [[kv objectAtIndex:1]
                     stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [params setObject:val forKey:[kv objectAtIndex:0]];
}
return params;
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Send Challenge"])
    {        

        [FBWebDialogs presentRequestsDialogModallyWithSession:nil
                                                      message:[NSString stringWithFormat:@"I just scored %i points on this great game, called SumsUp. can you beat it?!", gameScore]
                                                        title:nil
                                                   parameters:nil
                                                      handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {

             if (error)
             {
                 // Error launching the dialog or sending the request.
                 NSLog(@"Error sending request.");
             }
             else
             {
                 if (result == FBWebDialogResultDialogNotCompleted)
                 {
                     // User clicked the "x" icon
                     NSLog(@"User canceled request.");
                 }
                 else
                 {
                     // Handle the send request callback
                     NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
                     if (![urlParams valueForKey:@"request"])
                     {
                         // User clicked the Cancel button
                         NSLog(@"User canceled request.");
                     }
                     else
                     {
                         // User clicked the Send button
                         NSString *requestID = [urlParams valueForKey:@"request"];
                         NSLog(@"Request ID: %@", requestID);
                     }
                 }
             }
         }];
    }

我已经在P-List中设置了自定义URL。 在我的AppDelegate中,我有:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    UIAlertView *alertView;
    NSString *text = [NSString stringWithFormat: @"url recieved: %@", url];
    alertView = [[UIAlertView alloc] initWithTitle:@"" message:text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

    return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication withSession:[PFFacebookUtils session]];

}

希望这有意义,有人能帮助我。如果需要更多信息,请让我知道。 谢谢Graham。

你还没有说它出了什么问题。请展示应用代理调用 parseURLParams: 的过程。 - Wain
谢谢,我已经编辑了问题以显示应用程序委托调用。这一切都很正常,并且不会引起任何问题,我只是不知道如何添加我想要发送到URL的字符串。 - Graham George
2个回答

2

我在iOS 9.x.x上尝试通过应用打开Youtube的URL时,遇到了与自定义URL Scheme相关的问题。当我浏览网络时,发现了一个有趣的事实。

iOS 9要求您的应用程序预先注册它打算调用的应用程序Scheme。

  • 打开您的YourApp-Info.plist文件,并添加键LSApplicationQueriesSchemes
  • LSApplicationQueriesSchemes下列出项时,添加一个值为youtube的新项(在我的情况下)。

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>youtube</string>
</array>


1

我的解决方案更简单。我希望你会发现它有用。

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    // Example URL: myapp://myapp.com/showString/yourString

    BOOL isMyAppURL = [[url scheme] isEqualToString:@"myapp"];

    if (isMyAppURL)
    {
        NSArray* pathComponents = [url pathComponents];
        NSString *command = pathComponents[0];

        // Check for showString command.
        if ([command isEqualToString:@"showString"])
        {
            NSString *stringToShow = [pathComponents[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"String to show: %@", stringToShow);
        }
    }

    return isMyAppURL;
}

这应该会指引你朝正确的方向。

谢谢您的回复,我刚刚尝试了那个方法。但我遇到的问题是,当我将我的URL从fb [my_facebook_app_ID]更改为fb???????????????:// myapp.com / showString / yourString(例如)在我的p-list中时,尝试打开应用程序链接到另一个我在应用商店中提供的应用程序!我是不是完全傻了? - Graham George

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