Search code examples
objective-cmacosgoogle-chromesafariopera

How to set Homepage in safari, Chrome, Opera, Firefox in mac browser in objective C


How I can set homepage for Safari, Chrome, Opera and Firefox browsers in OSX with Objective-C?


Solution

  • Before set HomePage for all browsers you need to close particular browser.

    In safari you can do following way.

    NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/com.apple.Safari.plist"];
    
    NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    NSLog(@"Old HomePage %@",[plistDict objectForKey:@"HomePage"]);
    
    [plistDict setValue:@"https://google.co.in/" forKey:@"HomePage"];
    
    [plistDict writeToFile:path atomically: YES];
    
    NSLog(@"New HomePage %@",[plistDict objectForKey:@"HomePage"]);
    

    For CHROME

            NSError * error;
            NSString * stringFromFile;
            NSString * stringFilepath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/Google/Chrome/Default/Preferences/"];
    
            stringFromFile = [[NSString alloc] initWithContentsOfFile:stringFilepath
                                                             encoding:NSUTF8StringEncoding
                                                                error:&error];
    
            NSLog(@"stringFromFile %@ error %@",stringFromFile, error.description);
    
    
            NSArray *lines = [stringFromFile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
            for (NSString *line in lines) {
    
                if ([line rangeOfString:@"\"homepage\":"].location != NSNotFound) {
    
                    NSLog(@"string contains homepage!");
    
                    NSString *temp =@"\"homepage\": \"https://yahoo.com\",";
    
                    NSString *replacedString = [stringFromFile stringByReplacingOccurrencesOfString:line
                                                                                         withString:temp];
    
                    [replacedString writeToFile:stringFilepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
                }//ends if
    
            }//ends for
    

    For Firefox

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/Firefox/Profiles"];
    
    NSString *firefoxPath;
    
    NSArray *folderItems = [self listFilesAtPath:path];
    
    for (NSURL *item in folderItems) {
    
       NSNumber *isHidden = nil;
    
       [item getResourceValue:&isHidden forKey:NSURLIsDirectoryKey error:nil];
    
       if ([isHidden boolValue]) {
    
         firefoxPath =[NSString stringWithFormat:@"%@/%@/prefs.js",path,[item.path lastPathComponent]];
        }
     }
    
    NSError * error;
    NSString * stringFromFile;
    NSLog(@"firefox %@",firefoxPath);
    
    stringFromFile = [[NSString alloc] initWithContentsOfFile:firefoxPath
                                                                 encoding:NSUTF8StringEncoding
                                                                    error:&error];
    
    
    NSArray *lines = [stringFromFile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
    for (NSString *line in lines) {
    
       if ([line rangeOfString:@"user_pref(\"browser.startup.homepage\""].location != NSNotFound) {
    
           NSString *temp = [NSString stringWithFormat:@"user_pref(\"browser.startup.homepage\", \"%@\");",homePageURL];
    
           NSString *replacedString = [stringFromFile stringByReplacingOccurrencesOfString:line
                                                                                             withString:temp];
    
           [replacedString writeToFile:firefoxPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
          }//ends if
    
      }//ends for
    

    For Opera

    NSError * error;
    NSString * stringFilepath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/com.operasoftware.Opera/Preferences/"];
    
    NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:stringFilepath
                                                                           encoding:NSUTF8StringEncoding
                                                                              error:&error];                
    
    NSArray *lines = [stringFromFile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
    for (NSString *line in lines) {
    
      if ([line rangeOfString:@"\"urls_to_restore_on_startup\":"].location != NSNotFound) {
    
           NSString *temp = [NSString stringWithFormat:@"\"urls_to_restore_on_startup\": [ \"%@\" ]",homePageURL];
    
           NSString *replacedString = [stringFromFile stringByReplacingOccurrencesOfString:line
                                                                                             withString:temp];
    
           [replacedString writeToFile:stringFilepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
         }//ends if
    
     }//ends for
    

    CHEERS !!! :) :)