如何使用Objective-C爲OSX中的Safari,Chrome,Opera和Firefox瀏覽器設置主頁?如何在目標C中設置safari,Chrome,Opera,Firefox在mac瀏覽器中的主頁C
回答
集之前網頁,供您需要關閉特定的瀏覽器所有瀏覽器。
在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! :) :)
對於Safari瀏覽器,您需要更改com.apple.safari.plist中的「HomePage」字符串,您可以在〜/ Library/Preferences中找到它。
有對此處編輯這些文件的.plist一些很好的文檔:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html
你可以提供相對的代碼,以便如何更改com.apple.safari.plist中的字符串嗎? – python
我嘗試了下面的代碼工作,但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
- 1. Safari瀏覽器/ Opera/Chrome中的1px「padding」
- 2. CSS過渡會在Safari瀏覽器,Chrome瀏覽器和Firefox瀏覽器中閃爍,但不會瀏覽Opera
- 3. 如何刪除像chrome,safari,opera這樣的webkit瀏覽器中的c fakepath?
- 4. 從瀏覽器中獲取mac地址,包括chrome,windows,safari,firefox
- 5. 未在Firefox,Opera和IE瀏覽器中加載CSS - Chrome和Safari工作正常
- 6. .swf在瀏覽器中不加載,chrome在Safari瀏覽器中,Firefox?
- 7. 在C#瀏覽器上設置「主頁」頁面
- 8. Firefox和Safari瀏覽器的 「意外 '*' 標記」,無法在Chrome
- 9. %的在不同的瀏覽器(Firefox,Safari,Opera或鉻)
- 10. selenium使用python for safari瀏覽器在mac中設置
- 11. 如何在Opera中設置一個if的CSS瀏覽器?
- 12. 如何創建瀏覽器擴展?與IE,Chrome,Firefox,Safari和Opera兼容?
- 13. 如何用C#設置默認瀏覽器主頁(IE)?
- 14. 選擇未正確放置在Firefox中,在Chrome瀏覽器/ safari中工作
- 15. 如何在Safari瀏覽器/ Chrome/Firefox中在<canvas>中繪製對角線?
- 16. JavaScript在Safari瀏覽器中工作,但不是Chrome或Firefox瀏覽器
- 17. 在Safari瀏覽器,Chrome瀏覽器和IE瀏覽器中顯示圖片div,但沒有在Firefox中顯示
- 18. 預設Safari瀏覽器書籤和主頁Mac
- 19. CSS rotateX在Chrome瀏覽器中反轉比Safari,IE,Edge,Firefox
- 20. 如何在瀏覽器中呈現CSS屬性「不透明」(IE,Chrome,Firefox,Opera)?
- 21. 如何在C#中將默認瀏覽器打開到主頁?
- 22. IE,Firefox,Chrome,Opera和Safari
- 23. 如何在Plone中設置瀏覽器頁面的標題
- 24. 在Safari瀏覽器中的頁邊距顯着不同於Chrome和Firefox
- 25. 在Chrome瀏覽器和/或Safari瀏覽器中啓用XSS
- 26. 查看和設置Safari瀏覽器/ Chrome的HTTP標頭
- 27. 按鈕不工作在IE瀏覽器,罰款Firefox/Opera/Chrome
- 28. 鏈接在firefox或Safari瀏覽器中無法正常工作,但在opera,chrome中正常工作,即
- 29. 在Chrome瀏覽器與Firefox,IE瀏覽器和Safari瀏覽器中,相同的字體看起來不同
- 30. 如何在MAC OSX中設置背景色目標c
。整個文件被損壞,所有的設置都會丟失。請現在更新或刪除Chrome的代碼爲惡意程序。 @蟒蛇 –