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。

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

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

}

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

谢谢格雷厄姆

4

2 回答 2

2

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

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

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

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

于 2015-10-27T11:44:16.260 回答
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;
}

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

于 2014-03-01T11:16:18.607 回答