Skip to content

Instantly share code, notes, and snippets.

@keijiro
Created September 26, 2011 11:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keijiro/1242050 to your computer and use it in GitHub Desktop.
Save keijiro/1242050 to your computer and use it in GitHub Desktop.
PostprocessBuildPlayer for Unity iOS: modifies Info.plist to use Facebook URL-scheme
#!/usr/bin/env python
import sys, os.path
install_path = sys.argv[1]
target_platform = sys.argv[2]
if target_platform != "iPhone": sys.exit()
info_plist_path = os.path.join(install_path, 'Info.plist')
file = open(info_plist_path, 'r')
plist = file.read()
file.close()
elements_to_add = '''
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbXXXXXXXXXXXXXXX</string>
</array>
</dict>
</array>
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>ja</string>
</array>
'''
plist = plist.replace('<key>', elements_to_add + '<key>', 1)
file = open(info_plist_path, 'w')
file.write(plist)
file.close()
@brunogama
Copy link

I've done something similar but I used the plistLib module:

def set_facebook_url(xcode_project_path):
    info_plist_path = os.path.join(xcode_project_path, 'Info.plist')
    pl = plistlib.readPlist(info_plist_path)
    new_settings = {
        "CFBundleURLSchemes": ["fbXXXXXXXXXXXXXXX"]
    }
    if "CFBundleURLTypes" in pl:
        pl["CFBundleURLTypes"].extend(new_settings)
    else:
        pl["CFBundleURLTypes"] = [new_settings]

    plistlib.writePlist(pl, info_plist_path)

@jiecuoren
Copy link

@brunogama Thanks for your hint.

If many url schemes you want to add, can do as following:

   def addUrlScheme(plist, sns, sns_short, id):
    scheme_value = sns_short+id
    new_dict = {'CFBundleTypeRole': 'Editor', 'CFBundleURLName': sns, 'CFBundleURLSchemes': [scheme_value]}
    if "CFBundleURLTypes" in plist:
        plist["CFBundleURLTypes"].append(new_dict.copy())
    else:
        plist["CFBundleURLTypes"] = [new_dict]

e.g.

        addUrlScheme(pl, 'facebook', 'fb', fb_appid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment