Edit Info.plist programmatically in a jailbroken a

2019-07-17 21:09发布

问题:

How can I edit the Info.plist file in a jailbroken app I'm writing? I know it's not normally possible but given the fact that this will be released in Cydia, I feel like there must be a way. I'm not savvy on file modifications in a jailbroken environment so any info is appreciated.

The reason I want to edit the Info.plist file is to register for a URL scheme programmatically. So if there's an alternative way to accomplish that, I'd love to hear it :-)

回答1:

If you want to programmatically edit your own app's Info.plist file as it runs, you can use this code:

- (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];
}

and if you call it like this:

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

then your app can be open with URLs like this:

stack://overflow

However, if you look at the Info.plist file permissions:

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

You see that you cannot write to that file as user mobile, which is how your app will run normally. So, one way to get around this is to give your app root privileges. See here for how to do that.

After you use this code, and give your app root privileges, it still might be necessary to respring before you see your custom URL scheme recognized. I didn't have time to test that part.



回答2:

Here is how I solved it for Facebook SDK in Swift

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)