NSURL包含中文或是不能识别的字符

NSString 中包含中文字符时转换为NSURL

由于url支持26个英文字母、数字和少数几个特殊字符,因此,对于url中包含非标准url的字符时,就需要对其进行编码。iOS中提供了函数stringByAddingPercentEscapesUsingEncoding对中文和一些特殊字符进行编码,但是stringByAddingPercentEscapesUsingEncoding的功能并不完善,对一些较为特殊的字符无效。而对这些字符则可以使用CFURLCreateStringByteAddingPercentEscapes函数,

 NSString *urlAsString =[NSString stringWithFormat:@"%@%@%@",Service_Url,url,dataString];

    urlAsString = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlAsString]];


iOS9用 stringByAddingPercentEncodingWithAllowedCharacters  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是 Objective-C 版本的代码实现: 1. 在 info.plist 文件中添加蓝牙权限: - NSBluetoothPeripheralUsageDescription:描述了应用程序需要使用蓝牙的原因。 - NSMicrophoneUsageDescription:描述了应用程序需要使用麦克风的原因。 2. 创建一个 AVAudioSession 实例并设置它的类别为 AVAudioSessionCategoryRecord: ``` AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *error; BOOL success = [audioSession setCategory:AVAudioSessionCategoryRecord error:&error]; if (!success) { NSLog(@"Failed to set audio session category: %@", error.localizedDescription); } ``` 3. 设置 AVAudioSession 实例的输入源和输出源: ``` NSArray<AVAudioSessionPortDescription *> *bluetoothInputs = [audioSession.availableInputs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(AVAudioSessionPortDescription * _Nullable input, NSDictionary<NSString *,id> * _Nullable bindings) { return input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothA2DP; }]]; if (bluetoothInputs.count > 0) { NSError *error; success = [audioSession setPreferredInput:bluetoothInputs.firstObject error:&error]; if (!success) { NSLog(@"Failed to set preferred input: %@", error.localizedDescription); } } AVAudioSessionPortDescription *currentOutput = audioSession.currentRoute.outputs.firstObject; success = [audioSession setPreferredOutput:currentOutput error:&error]; if (!success) { NSLog(@"Failed to set preferred output: %@", error.localizedDescription); } ``` 4. 初始化 AVAudioRecorder 对象并开始录音: ``` NSURL *audioURL = // Specify the URL where you want to save the recorded audio. NSDictionary *audioSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC), AVSampleRateKey: @(44100), AVNumberOfChannelsKey: @(2)}; NSError *error; AVAudioRecorder *audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioURL settings:audioSettings error:&error]; if (!audioRecorder) { NSLog(@"Failed to initialize audio recorder: %@", error.localizedDescription); } else { [audioRecorder prepareToRecord]; [audioRecorder record]; } ``` 这样就可以从蓝牙设备的麦克风录制音频了。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值