如何将设备的系统铃声设置为本地通知音?
How to set system ringtone of device as local notification sound?
我想将设备的系统铃声设置为来自我的应用程序的本地通知声音。我得到了我的设备中存在的系统铃声列表,但无法将其设置为通知,因为在应用程序包中无法访问铃声路径。有没有办法获得访问权限并能够将其设置为通知声音
经过大量研究我可以解决我的问题,我只需要将 System/Sounds 复制到 Library/Sounds/notification.caf
像这样:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *lib = [libraryDir objectAtIndex:0];
NSString *directoryPath = [lib stringByAppendingString:@"/Sounds"];
@try {
[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:TRUE attributes:nil error:nil];
NSString *systemSoundPath = fromPath; // eg. /System/Library/Audio/UISounds/Tink.caf
NSString *notificationSoundPath = [directoryPath stringByAppendingString:@"/notification.caf"];
BOOL fileExist = [fileManager fileExistsAtPath: notificationSoundPath];
if(fileExist){
[fileManager removeItemAtPath:notificationSoundPath error:nil];
}
[fileManager copyItemAtPath:systemSoundPath toPath:notificationSoundPath error:nil];
} @catch (NSException *exception) {
NSLog(@"in catch");
} @finally {
NSLog(@"in catch");
}
如果对其他人有帮助,请检查。
我想将设备的系统铃声设置为来自我的应用程序的本地通知声音。我得到了我的设备中存在的系统铃声列表,但无法将其设置为通知,因为在应用程序包中无法访问铃声路径。有没有办法获得访问权限并能够将其设置为通知声音
经过大量研究我可以解决我的问题,我只需要将 System/Sounds 复制到 Library/Sounds/notification.caf 像这样:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray *libraryDir = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *lib = [libraryDir objectAtIndex:0];
NSString *directoryPath = [lib stringByAppendingString:@"/Sounds"];
@try {
[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:TRUE attributes:nil error:nil];
NSString *systemSoundPath = fromPath; // eg. /System/Library/Audio/UISounds/Tink.caf
NSString *notificationSoundPath = [directoryPath stringByAppendingString:@"/notification.caf"];
BOOL fileExist = [fileManager fileExistsAtPath: notificationSoundPath];
if(fileExist){
[fileManager removeItemAtPath:notificationSoundPath error:nil];
}
[fileManager copyItemAtPath:systemSoundPath toPath:notificationSoundPath error:nil];
} @catch (NSException *exception) {
NSLog(@"in catch");
} @finally {
NSLog(@"in catch");
}
如果对其他人有帮助,请检查。