【问题标题】:iOS custom url schemeiOS 自定义 url 方案
【发布时间】:2014-04-02 12:34:30
【问题描述】:

我正在创建一个使用自定义 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。

在我的应用委托中,我有:

- (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]];

}

我希望这是有道理的,有人可以帮助我。如果需要更多信息,请告诉我?

感谢格雷厄姆

【问题讨论】:

  • 你还没有说它做错了什么。显示应用委托调用parseURLParams:
  • 谢谢,我已编辑问题以显示应用委托调用。这一切都有效并且不会引起任何问题,我只是不知道如何将要发送到 url 的字符串添加。

标签: ios objective-c custom-url


【解决方案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;
}

这应该为您指明正确的方向。

【讨论】:

  • 感谢您的回复,我只是尝试过。我面临的问题是当我将我的网址从 fb[my_facebook_app_ID] 更改为 fb???????????????://myapp.com/showString/yourString (例如)我的 p 列表,试图打开应用程序链接到我在应用程序商店中可用的另一个应用程序!我是不是完全傻了?
【解决方案2】:

我在 ios v. 9.x.x 上遇到了自定义 URL 方案的问题。当我尝试通过 App 打开 Youtube URL 时。当我通过网络浏览时,我发现了一个有趣的事实。

iOS 9 要求您的应用预先注册它打算调用的应用方案。

  • 打开您的 YourApp-Info.plist 文件并添加密钥 LSApplicationQueriesSchemes
  • LSApplicationQueriesSchemes下的列表项,添加一个 价值为 youtube 的新项目(以我为例)。

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

【讨论】: