2014-03-01 66 views
4

我正在創建一個使用自定義網址方案的應用程序,我已經設置了所有設置,並且在打開應用程序時有效,但是,現在我希望能夠將單個字符串添加到網址,以便打開應用的人可以看到該字符串。我真的很困難,有人可以幫我嗎?iOS自定義網址方案

這裏是我的代碼

- (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中的自定義網址設置。

在我的應用程序委託我:

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

} 

我希望這是有道理的,有人能幫助我。如果需要更多信息,請告訴我?

謝謝格雷厄姆

+0

你還沒有說什麼它錯了。顯示應用程序委託調用'parseURLParams:' – Wain

+0

謝謝,我編輯了問題以顯示應用程序委託調用。這一切工作,並沒有造成任何問題,我只是不能解決如何添加我想要發送到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; 
} 

這應該指向您正確的方向。

+0

謝謝你的迴應,我只是嘗試了。我面臨的問題是當我從fb [my_facebook_app_ID]更改我的url到fb ???????????????://myapp.com/showString/yourString(例如)in我的名單,試圖打開應用程序鏈接到我在應用程序商店中可用的另一個應用程序!我是完全愚蠢的嗎? –

2

我在ios v。9.x.x上的自定義URL方案有問題。當我試圖通過應用程序打開YouTube的URL。當我瀏覽網絡時,我發現了一個有趣的事實。

iOS 9要求您的應用程序預先註冊其打算調用的應用程序方案。

  • 打開YourApp-的Info.plist文件,並添加鍵,LSApplicationQueriesSchemes。下LSApplicationQueriesSchemes
  • 列表項,與價值的YouTube(對我來說)添加 新項目。

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