在越狱应用程序中以编程方式编辑Info.plist

如何在我正在编写的越狱应用程序中编辑Info.plist文件? 我知道这通常不可能,但鉴于这将在Cydia发布,我觉得必须有办法。 我对在越狱环境中修改文件并不精通,所以任何信息都会受到赞赏。

我想编辑Info.plist文件的原因是以编程方式注册URL方案。 因此,如果有另一种方法可以实现这一点,我很乐意听到它:-)

如果要在运行时以编程方式编辑自己的应用程序的Info.plist文件,可以使用以下代码:

- (BOOL) registerForScheme: (NSString*) scheme { NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; NSMutableDictionary* plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath]; NSDictionary* urlType = [NSDictionary dictionaryWithObjectsAndKeys: @"com.mycompany.myscheme", @"CFBundleURLName", [NSArray arrayWithObject: scheme], @"CFBundleURLSchemes", nil]; [plist setObject: [NSArray arrayWithObject: urlType] forKey: @"CFBundleURLTypes"]; return [plist writeToFile: plistPath atomically: YES]; } 

如果你这样称呼它:

 BOOL succeeded = [self registerForScheme: @"stack"]; 

然后您的应用可以使用以下URL打开:

 stack://overflow 

但是,如果您查看Info.plist文件权限:

 -rw-r--r-- 1 root wheel 1167 Oct 26 02:17 Info.plist 

您看到无法将该文件写入用户mobile ,这就是您的应用正常运行的方式。 因此,解决此问题的一种方法是为您的应用提供root权限。 请参阅此处了解如何执行此操作 。

使用此代码并授予应用程序root权限后,在您看到自定义URL方案被识别之前,可能仍需要重新启动 。 我没有时间测试那部分。

以下是我在Swift中为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)