0

我的公司有一个直接与 Salesforce 交互的 iOS 应用程序,但有一个主要缺点。当用户尝试上传非法字符时,应用程序会挂起、返回错误值或导致其他一些不稳定的行为。

我在神秘的互联网中发现了一段非常方便的代码(感谢 MailDrop),它过滤字符串并用正斜杠转义那些非法字符。然后它返回一个字符串中的值,准备进行更新插入/查询。

不过,我真正遇到的一件事是让它与我们的应用程序一起使用。我们的数据存储在字典中,并以多种方式写入(查询服务器或手动输入),并在应用程序生命周期的不同时间写入。我不太确定如何以最有效的方法调用这个漂亮的“escapeSosl”。有没有办法可以在每次写入 Core Data 时转义这些字符?我最害怕的是通过应用程序并调用数百次。

下面是转义方法:

- (NSString *)escapeSosl:(NSString *)src {
        // from docs, these are all reserved
        NSArray *reserved = [NSArray arrayWithObjects:@"\\", @"&", @"|", @"!", @"{", @"}", @"[", @"]", @"^", @"~", @"*:", @":", @"'" ,@"\"", @"+", @"-", nil];
        NSMutableString *s = [NSMutableString stringWithString:src];
        NSString *r;
        NSEnumerator *e = [reserved objectEnumerator];
        while (r = [e nextObject])
            [s replaceOccurrencesOfString:r withString:[NSString stringWithFormat:@"\\%@", r] options:NSLiteralSearch range:NSMakeRange(0, [s length])];
        return s;
    }

我发现复杂的是这些转义的时间,以及如何将其应用于每种情况。例如,应用程序使用手动输入或通过反向地理编码找到的位置向 Salesforce 查询帐户。以下是应用程序从 Salesforce 服务器请求数据的示例:

-(void)checkServerForLeadMatchingAddressOnAccount:(SFAccount *)account {

currentAccount = account;

//Parse the account location data, removing the last word in the `street` object
NSString *street = account.shippingStreetAddress;
NSMutableArray *streetArray = [[street componentsSeparatedByString:@" "] mutableCopy];
[streetArray removeLastObject];
street = [streetArray componentsJoinedByString:@" "];
street = [street stringByAppendingString:@"_%"];

NSString *city = account.shippingCity;
NSString *state = account.shippingState;
NSString *zip = account.shippingZIP;

//Check for a matching lead on the server. User the callback methods to proceed (either create new data on the server or download to the device

NSLog(@"is connected: %i", self.isConnectedToServer);

if (self.isConnectedToServer){
    requestCheckForLead = [[SFRestAPI sharedInstance] requestForQuery:[NSString stringWithFormat:@"SELECT ID, Company, Name, Street, City, State, PostalCode, Phone FROM Lead WHERE PostalCode = '%@' AND Street LIKE '%@' AND City = '%@' AND State = '%@'", zip, street, city, state]];
    [[SFRestAPI sharedInstance] send:requestCheckForLead delegate:self];
    openTasks++;
} else {
    NSLog(@"I can't connect");
    [self.serverDelegate serverObjectManager:self communicationSuccessful:NO withResponse:@"Could not connect to the server."];
}

我可能会转义字符的另一个区域是在开始上传到服务器之前。上传了几项内容,包括帐户、帐户的联系人(1:1 相对)、帐户的机会(1:1 相对)和机会的当前服务水平(1:many rel)。

下面是与帐户关联的联系人的示例 - 它同时使用字符串和字典引用,但有一些额外的逻辑用于从姓氏中解析名字。我想以一种我不需要担心这些操作的时间的方式来转义数据。

NSMutableDictionary *contactDict = [NSMutableDictionary new];

if (myAccount.contact.email) {
    [contactDict setObject:myAccount.contact.email forKey:@"Email"];
} else {
    [contactDict setObject:@"" forKey:@"Email"];
}

if (myAccount.contact.name) {
    //Split name in two
    NSArray *nameArray = [myAccount.contact.name componentsSeparatedByString:@" "];

    NSString *firstName = [nameArray objectAtIndex:0];
    NSString *lastName = [nameArray lastObject];


    [contactDict setObject:firstName forKey:@"firstName"];
    [contactDict setObject:lastName forKey:@"lastName"];
} else {
    [contactDict setObject:@"" forKey:@"firstName"];
    [contactDict setObject:@"" forKey:@"lastName"];
}

if (myAccount.contact.phone){
    [contactDict setObject:myAccount.contact.phone forKey:@"Phone"];
} else {
    [contactDict setObject:@"" forKey:@"Phone"];
}

updateContactRequest = [[SFRestAPI sharedInstance] requestForUpdateWithObjectType:@"Contact" objectId:myAccount.createdContactID fields:contactDict];
[[SFRestAPI sharedInstance] send:updateContactRequest delegate:self];

所以这似乎是一个很大的问题,但是有没有人有一些关于我如何在这些不同的字典中转义字符的指针,使用类似于我上面包含的“escapeSosl”代码的东西,每当事情被写入核心数据时?

4

1 回答 1

0

在这一行中,您构建您的查询

requestCheckForLead = [[SFRestAPI sharedInstance] requestForQuery:[NSString stringWithFormat:@"SELECT ID, Company, Name, Street, City, State, PostalCode, Phone FROM Lead WHERE PostalCode = '%@' AND Street LIKE '%@' AND City = '%@' AND State = '%@'", zip, street, city, state]];

而不是street作为参数传递[self escapeSosl:street],其他参数以此类推。

于 2013-03-13T22:38:12.530 回答