如何将设备的系统铃声设置为本地通知声音?

时间:2018-05-18 12:30:13

标签: ios uilocalnotification

我想将设备的系统铃声设置为来自我的应用的本地通知声音。我获取了设备中存在的系统铃声列表,但由于无法在应用程序包中访问铃声路径,因此无法将其设置为通知。有没有办法获得访问权限并能够将其设置为通知声音

1 个答案:

答案 0 :(得分:1)

经过大量研究后我可以解决我的问题,我只需要将系统/声音复制到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");
}

如果对其他人有帮助,请检查。

相关问题