如何将NSString与Push Notification Payload中的Variable进行比较

时间:2012-09-10 14:09:22

标签: iphone objective-c cordova

我有一个Phonegap应用程序,我收到推送通知。我在有效负载中发送一个“Pagekey”,告诉应用程序在应用程序启动收到的推送通知后打开哪个HTML页面。

我在- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions(片段)

中执行以下操作
NSDictionary* extras = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *key = ((NSString*)[extras objectForKey:@"pagekey"]);//get the Pagekey
NSLog(@"key: %@", key); //prints out "2" (2 without the "")

//Load the HTML Page depending on the pagekey
self.viewController.wwwFolderName = @"www";
if([key isEqual:@"2"]){
    NSLog(@"Im in the 2 branch");
    self.viewController.startPage = @"winnings.html";
}else{
    NSLog(@"Im in the else branch");//this branch is executed(unexpected behaviour)
    self.viewController.startPage = @"index.html";
}

pagekey最初是从推送通知服务器发送的JSON数据。任何人都知道如何正确比较字符串,以便执行正确的分支?非常感谢!

2 个答案:

答案 0 :(得分:1)

尝试if([key isEqualToString:@"2"])...

有关isEqual vs isEqualToString

的信息,请参阅this主题

答案 1 :(得分:1)

使用- (NSComparisonResult)compare:(NSNumber *)decimalNumber类的NSDecimalNumber

if ([key compare:[NSNumber numberWithInt:2]]==NSOrderedSame) {
    //Equal
}