CFBundleURLName(URL Identifier) A string containing the abstract name of the URL scheme. To ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com.acme.myscheme.The string you specify is also used as a key in your app’s InfoPlist.strings file. The value of the key is the human-readable scheme name.
CFBundleURLSchemes(URL Schemes) An array of strings containing the URL scheme names—for example, http, mailto, tel, and sms.
可以設置多個(URL Schemes),設置成功後如下:
調用方的部分代碼如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. CGRect rectTextView= CGRectMake(10.0f, 30.0f, 300.0f, 100.0f); self.textView = [[UITextView alloc] initWithFrame:rectTextView]; [self.textView.layer setBorderColor:[UIColor lightGrayColor].CGColor]; [self.textView.layer setBorderWidth:0.5f]; [self.textView setText:@"username=WT&password=123456&callback=invoking"]; [self.view addSubview:self.textView]; CGRect rect = CGRectMake(10.0f, 150.0f, 300.0f, 40.0f); UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = rect; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [button setTitle:@"Login" forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor blueColor]]; [button addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside]; [button.layer setMasksToBounds:YES]; [button.layer setCornerRadius:5.0f]; [self.view addSubview:button]; } - (void)handle:(id)sender { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"invoked://com.hello/path?%@", self.textView.text]]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"message" message:[NSString stringWithFormat:@"%@", url] delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alertView show]; } }
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { NSLog(@"%@", url); if ([[url scheme] isEqualToString:@"invoked"]) { if ([[url host] isEqualToString:@"com.hello"]) { NSString *query = [url query]; NSArray *array = [query componentsSeparatedByString:@"&"]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10]; for (NSString *item in array) { NSArray *valueArray = [item componentsSeparatedByString:@"="]; [dic setValue:[valueArray objectAtIndex:1] forKey:[valueArray objectAtIndex:0]]; } [self application:application didFinishLaunchingWithOptions:dic]; } return YES; } return NO; }
參考文獻:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW20
http://blog.csdn.net/likendsl/article/details/7553605
http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html