2013-12-18 42 views

回答

1

集之前網頁,供您需要關閉特定的瀏覽器所有瀏覽器。

在safari中,您可以採用以下方法。

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

鍍鉻

 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 [email protected]"\"homepage\": \"https://yahoo.com\","; 

       NSString *replacedString = [stringFromFile stringByReplacingOccurrencesOfString:line 
                        withString:temp]; 

       [replacedString writeToFile:stringFilepath atomically:YES encoding:NSUTF8StringEncoding error:nil]; 

      }//ends if 

     }//ends 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 

對於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! :) :)

+0

。整個文件被損壞,所有的設置都會丟失。請現在更新或刪除Chrome的代碼爲惡意程序。 @蟒蛇 –

1

對於Safari瀏覽器,您需要更改com.apple.safari.plist中的「HomePage」字符串,您可以在〜/ Library/Preferences中找到它。

有對此處編輯這些文件的.plist一些很好的文檔:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html

+0

你可以提供相對的代碼,以便如何更改com.apple.safari.plist中的字符串嗎? – python

+0

我嘗試了下面的代碼工作,但safari退出並再次打開其值更改爲前一個。 NSString * path = [NSHomeDirectory()stringByAppendingPathComponent:@「/ Library/Preferences/com.apple.Safari.plist」]; NSMutableDictionary * plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path]; [plistDict setValue:@「https://news.google.co.in/」forKey:@「HomePage」]; [plistDict writeToFile:atomically path:YES];我在設置谷歌瀏覽器主頁時使用了 – python

相關問題