我正在从另一个应用中启动应用并传递值。我可以将值传入AppDelegate,但我似乎无法从ViewController访问它。这是我正在使用的代码:
从应用程序1(按钮触摸调用):
NSString* str360URL = @"customer360://?customerNum=70044321";
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:str360URL]];
if (canOpenURL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str360URL]];
}
来自App 2 AppDelegate:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([[url scheme] isEqualToString:@"customer360"]) {
//check to see if we are passing in a customer number
if ([[url query] rangeOfString:@"customerNum"].location != NSNotFound) {
//get all the query string items (there should be only the customer num as of 6/01/14 but just in case they want to expand what is passed in
NSArray* arrQueryComponents = [[url query] componentsSeparatedByString:@"&"];
//loop
for(NSString* strThisKVPair in arrQueryComponents) {
//split the kv pairs into parts
NSArray* arrKVPair = [strThisKVPair componentsSeparatedByString:@"="];
//get the value from the kv pair
self.strCustomerNum = [arrKVPair objectAtIndex:1];
}
}
...
此处有更多代码用于注册目的。我可以在这里弹出一个UIAlertView并在属性self.strCustomerNum上看到正确的值,所以我知道appDelegate将它存储在它的属性中。
在ViewController中我有这段代码:
//pass in the customer number
self.strCustomerNum = ((AppDelegate *)[UIApplication sharedApplication].delegate).strCustomerNum;
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"App View Controller" message:self.strCustomerNum delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
我的提醒不会显示客户编号。正在导入AppDelegate.h。关于我在哪里出错的任何想法?
答案 0 :(得分:1)
我最好的猜测是视图控制器中的代码在应用委托代理之前执行,因此strCustomerNum
的值为零。
您可以通过在视图控制器和应用代理application:openURL:sourceApplication:annotation: