简体   繁体   English

iPhone App自定义网址方案问题

[英]iPhone App Custom Url Scheme Issue

I am trying to get a custom url scheme to open a url inside the application. 我正在尝试获取自定义网址方案,以在应用程序内部打开网址。 So the url scheme would be "example://confirm?1234" which I would like to use the query string as part of a url for a UIWebView. 因此,URL方案将是“ example:// confirm?1234”,我想将查询字符串用作UIWebView的URL的一部分。 So that would make the UIWebView open " http://www.example.com/confirm?1234 ". 这样可以使UIWebView打开“ http://www.example.com/confirm?1234 ”。 This is being done in the AppDelegate so I am also having an issue controlling the FirstViewController UIWebView. 这是在AppDelegate中完成的,所以我在控制FirstViewController UIWebView方面也遇到了问题。 Any help would be loved. 任何帮助都会被爱。

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
        sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {
    NSLog(@"String: %@", [url query]);  
    NSString *web = @"http://www.example.com/confirm?[url query]";
    NSURL *url = [NSURL URLWithString:web];
    NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestUrl];
}

You need a string format to build the new URL: 您需要一种字符串格式来构建新的URL:

NSLog(@"String: %@", [url query]);  
NSString *web = [NSString stringWithFormat:@"http://www.example.com/confirm?%@", [url query]];
NSURL *url = [NSURL URLWithString:web];

I figured it out. 我想到了。 In case anyone else have a similar issues the code below helps. 如果其他人有类似的问题,下面的代码会有所帮助。

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
 if ([[url host] isEqualToString:@"confirm"]) {
 NSLog(@"String: %@", [url query]);
 NSString *urlAddress = [NSString stringWithFormat:@"http://www.example.com/confirm.php?key=%@", [url query]];
NSURL *url2 = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
 [webView loadRequest:requestObj];
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM