#pragma mark ------返回webView可识别的NSURLRequest
-(NSURLRequest *)loadURLwithString:(NSString *)string{
if ([self getParamByString:string] == 0 || [self getParamByString:string] == 1) {
return [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
}else if ([self getParamByString:string] == 2){
// 1、截取第一个?前的字符串
NSString * str0 = [self subStringWithString:string andIndexStr:@"?" andIndex:1];
NSLog(@"webstr0 === %@",str0);
// 2、 截取?后面的字符串
NSString *str1 = [string substringWithRange:NSMakeRange([str0 length]+1, [string length]-[str0 length]-1)];
str1 = [str1 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// 3、 拼接 字符串
NSString *webUrlStr = [NSString stringWithFormat:@"%@?%@",str0,str1];
NSLog(@"webUrlStr === %@",webUrlStr);
// 4、将 NSString 转成 NSURL
NSURL *url = [NSURL URLWithString:webUrlStr];
NSLog(@"webUrl === %@",url);
// 5、将NSURL 转成 NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSLog(@"webrequest === %@",request);
return request;
}else{
[MBProgressHUD showSuccessMessage:@"暂时不支持该二维码"];
return nil;
}
}
#pragma mark =====对#和?号做处理
-(NSInteger)getParamByString:(NSString *)string {
NSArray *allElements = [string componentsSeparatedByString:@"#"];
NSArray *questionElements = [string componentsSeparatedByString:@"?"];
NSLog(@"allElements.count == %lu",(unsigned long)allElements.count);
NSLog(@"questionElements.count == %lu",(unsigned long)questionElements.count);
if (allElements.count >questionElements.count) {
return allElements.count-1;
}else if (allElements.count<questionElements.count){
return questionElements.count-1;
}else{
return allElements.count-1;
}
}
#pragma mark ------ 截取字符串
-(NSString *)subStringWithString:(NSString *)str andIndexStr:(NSString *)indexStr andIndex:(NSInteger)index{
NSString *firstStr = nil;
NSString *secondStr = nil;
BOOL isFound = NO;
NSInteger foundIndex = 0;
for (int i = 0; i<[str length]; i++) {
if (isFound == YES && foundIndex == index) {
return firstStr;
}
NSString *temp = [str substringWithRange:NSMakeRange(i, 1)];
if ([temp isEqualToString:indexStr]) {
firstStr = [str substringWithRange:NSMakeRange(0, i)];
secondStr = [str substringWithRange:NSMakeRange(i+1, [str length]-[firstStr length]-1)];
foundIndex =foundIndex +1;
isFound = YES;
}
// NSLog(@"第%ld个下标值为%@",(long)i,temp);
}
return nil;
}
本文只做记录使用,如果有更好的方法,希望各位不吝赐教!!!