Dheeraj Gupta
Dheeraj Gupta

Reputation: 380

How to set system ringtone of device as local notification sound?

I want to set the system ringtones of device as local notification sound from my app. I get the list of system ringtones present in my device but not able to set it for notification as the ringtone path is not accessible in the application bundle. Is there any way to get the access and able to set it as notification sound

Upvotes: 3

Views: 492

Answers (1)

Dheeraj Gupta
Dheeraj Gupta

Reputation: 380

After lots of research I could solve my problem, I just had to copy the System/Sounds to the Library/Sounds/notification.caf Like this:

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");
}

Please check, if its helpful to anyone else.

Upvotes: 1

Related Questions