1. 程式人生 > >[iOS]通過JS調用iOS函數時的URL編碼問題

[iOS]通過JS調用iOS函數時的URL編碼問題

tst table ofo alloc objc allocator ati rds http

在前面的文章:[iOS]在WebApp中怎樣使用JS調用iOS的函數 中,提到了怎樣使用JS通過改動URL調用iOS的內部函數。


當中會遇到一個問題,就是編碼問題。比方通過URL調用彈窗,在裏面寫上內容:你好汪海。


那鏈接大概就是這種:http://xxx.com#ios?action=alert&param=你好汪海


可是在iOS中接收到的時候會出現中文的亂碼:

http://xxx.com#ios?action=alert&param=%25E6%2596%2587%25E4

遇到這個問題主要是URL在轉化中的編碼問題。解決方式感謝這篇博文:iOS中的編碼問題。


將轉碼函數封裝:

// 將URL編碼
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
    NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)input,
                                            NULL,
                                            (CFStringRef)@"!*‘();:@&=+$,/?

%#[]", kCFStringEncodingUTF8)); return outputStr; } // 將URL解碼 - (NSString *)decodeFromPercentEscapeString: (NSString *) input { NSMutableString *outputStr = [NSMutableString stringWithString:input]; [outputStr replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [outputStr length])]; return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; }



演示一下上面的封裝函數:

    NSString * testUrl = @"http://search.google.com?keywords=($# it‘s {a*123})00!*‘();:@&=+$,/?%#[]";
    NSLog(@"original: %@", testUrl);


    NSString * encodeStr = [self encodeToPercentEscapeString:testUrl];
    NSLog(@"encoded: %@", encodeStr);
    
    NSString * encodeStr2 = [testUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"encoded2:%@", encodeStr2);
    
    NSString * decodeStr = [self decodeFromPercentEscapeString:encodeStr];
    NSLog(@"decoded: %@", decodeStr);



結果例如以下:

>> original: http://search.google.com?

keywords=($# it‘s {a*123})00!*‘();:@&=+$,/?%#[]
>> encoded: http%3A%2F%2Fsearch.google.com%3Fkeywords%3D%28%24%23%20it%27s%20%7Ba%2A123%7D%2900%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5D
>> encoded2: http://search.google.com?keywords=($%23%20it‘s%20%7Ba*123%7D)00!*‘();:@&=+$,/?

%25%23%5B%5D
>> decoded: http://search.google.com?keywords=($# it‘s {a*123})00!*‘();:@&=+$,/?

%#[]




演示一下自帶的URL轉碼測試代碼:

  NSString* string1 = @"https://www.cloudsafe.com/目錄";
    
    NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString* string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString* string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString* string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString* string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString* string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSLog(@"原始數據%@",string1);
    NSLog(@"一層編碼%@",string2);
    NSLog(@"兩層編碼%@",string3);
    NSLog(@"一層編碼的一層解碼%@",string4);
    NSLog(@"兩層編碼的一層解碼%@",string5);
    NSLog(@"一層編碼的兩層解碼%@",string6);
    NSLog(@"兩層編碼的兩層解碼%@",string7);
    



打印結果:

2014-06-10 15:00:02.425 DareWayApp[7400:671651] 原始數據https://www.cloudsafe.com/目錄

2014-06-10 15:00:02.426 DareWayApp[7400:671651] 一層編碼https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 兩層編碼https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一層編碼的一層解碼https://www.cloudsafe.com/目錄

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 兩層編碼的一層解碼https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 一層編碼的兩層解碼https://www.cloudsafe.com/目錄

2014-06-10 15:00:02.427 DareWayApp[7400:671651] 兩層編碼的兩層解碼https://www.cloudsafe.com/目錄





假設server用的是GBK編碼,僅僅要把上面的UTF改成以下的encoding就能夠了:

 NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:gbkEncoding];
      



[iOS]通過JS調用iOS函數時的URL編碼問題