2012-12-25 23 views
3

正如我們所知的Facebook SDK,我們應該在App Plist中提供URL類型FacebookAppID鍵。如何以編程方式存儲Facebook AppID密鑰

我的問題是如何以編程方式存儲它們,而無需在plist中手動輸入它們,例如在UIApplicationDelegate方法didFinishLaunchingWithOptions中。

我嘗試使用NSUserDefaults設置它,但它保留給這個錯誤:

No AppID provided; either pass an AppID to init, or add a string valued key with the appropriate id named FacebookAppID to the bundle *.plist' 

我不知道爲什麼,但我猜的Xcode店在其他地方使用NSUserDefaults默認應用程序plist中鍵。

有誰知道如何解決這一問題?

回答

1

由於某種原因,它不安全。它很容易得到您的應用程序plist文件並閱讀所有信息。你應該保持你的應用程序私鑰安全。但如果你真的想這樣做,你可以從你的plist文件解析數據:

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPlistFileName" ofType:@"plist"]; 

NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path]; 

NSLog(@"You app key is %@",[dict objectForKey:@"yourKeyFromPlist"]); 

來存儲所有應用程序中共享的東西,最簡單的方法是使用NSUserDefaults的。但在做任何更改後,請不要忘記撥打同步Here is NSUserDefaults documentation.我不認爲這是任何數據寫入到您的plist一個很好的解決方案,但如果你真的想,這裏是一個代碼:

NSString* plistPath = nil; 
NSFileManager* manager = [NSFileManager defaultManager]; 
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"yourPlistFileName.plist"])) 
{ 
    if ([manager isWritableFileAtPath:plistPath]) 
    { 
     NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
     [infoDict setObject:@"your key value" forKey:@"Your Key"]; 
     [infoDict writeToFile:plistPath atomically:NO]; 
     [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil]; 
    } 
} 
+0

謝謝@kokoko,但我需要寫不讀,並且我不知道爲什麼當我使用[[[NSBundle mainBundle] infoDictionary] objectForKey:@「FacebookAppID 「]? –

+0

我編輯了我的答案。 – kokoko

+0

我想我不能在plist(默認應用程序plist)上寫字,因爲[manager isWritableFileAtPath:plistPath]是NO ,,,我也嘗試過在plist上創建它來做一些測試並且它可以工作! –

7

這似乎爲我工作,要設置應用程序ID :

[FBSettings setDefaultAppID: @"123456789"]; 

我不知道是否有針對URL類型類似的東西。不過,我可以想象,註冊URL類型是iOS在安裝應用程序時執行的操作,所以如果是這種情況,可能需要在plist文件中而不是在代碼中。

+1

對於SDK的當前版本(截至2016年9月),現在這個版本是'[FBSDKSettings setAppID: @ 「123456789」]' –

0

這是我如何解決它爲Facebook SDK斯威夫特:

var appid: NSMutableDictionary = ["FacebookAppID": "123456789"] 
var plistPath = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") 
appid.writeToFile(plistPath!, atomically: true) 

var appName: NSMutableDictionary = ["FacebookDisplayName": "AppName-Test"] 
appName.writeToFile(plistPath!, atomically: true) 

var urlStuff = NSMutableDictionary(contentsOfFile: plistPath!) 
var urlType = NSDictionary(objectsAndKeys: "com.appprefix.AppName", "CFBundleURLName", NSArray(object: "fb123456789"), "CFBundleURLSchemes") 
urlStuff?.setObject(NSArray(object: urlType), forKey: "CFBundleURLTypes") 
urlStuff?.writeToFile(plistPath!, atomically: true) 

你應該改變我用自己的FB的東西進入了佔位符值。

相關問題