7. iOS ਉੱਤੇ ਪਾਇਥਨ ਦੀ ਵਰਤੋਂ¶
- ਲੇਖਕ:
Russell Keith-Magee (2024-03)
iOS ਉੱਤੇ ਪਾਇਥਨ ਦੀ ਵਰਤੋਂ ਡੈਸਕਟਾਪ ਪਲੇਟਫਾਰਮਾਂ ਉੱਤੇ ਪਾਇਥਨ ਦੀ ਵਰਤੋਂ ਤੋਂ ਵੱਖਰੀ ਹੈ। ਡੈਸਕਟਾਪ ਪਲੇਟਫਾਰਮ ਉੱਤੇ ਪਾਇਥਨ ਆਮ ਤੌਰ 'ਤੇ ਇੱਕ ਸਿਸਟਮ ਸਰੋਤ ਵਜੋਂ ਇੰਸਟਾਲ ਕੀਤਾ ਜਾਂਦਾ ਹੈ ਜਿਸਦੀ ਵਰਤੋਂ ਉਸ ਕੰਪਿਊਟਰ ਦਾ ਕੋਈ ਵੀ ਉਪਭੋਗਤਾ ਕਰ ਸਕਦਾ ਹੈ। ਇਸ ਤੋਂ ਬਾਅਦ ਉਪਭੋਗਤਾ python ਐਗਜ਼ਿਕਿਊਟੇਬਲ ਚਲਾ ਕੇ ਅਤੇ ਇੰਟਰਐਕਟਿਵ ਪ੍ਰੌਮਪਟ ਉੱਤੇ ਕਮਾਂਡਾਂ ਦਰਜ ਕਰਕੇ ਜਾਂ ਪਾਇਥਨ ਸਕ੍ਰਿਪਟ ਚਲਾ ਕੇ ਪਾਇਥਨ ਨਾਲ ਕੰਮ ਕਰਦੇ ਹਨ।
iOS ਉੱਤੇ ਸਿਸਟਮ ਸਰੋਤ ਵਜੋਂ ਇੰਸਟਾਲ ਕਰਨ ਦੀ ਕੋਈ ਧਾਰਣਾ ਨਹੀਂ ਹੈ। ਸਾਫਟਵੇਅਰ ਵੰਡ ਦੀ ਇਕੋ ਇਕ ਇਕਾਈ "ਐਪ" ਹੈ। ਇੱਥੇ ਕੋਈ ਐਸਾ ਕੰਸੋਲ ਵੀ ਨਹੀਂ ਹੁੰਦਾ ਜਿੱਥੇ ਤੁਸੀਂ python ਐਗਜ਼ਿਕਿਊਟੇਬਲ ਚਲਾ ਸਕੋ ਜਾਂ Python REPL ਨਾਲ ਇੰਟਰਐਕਟ ਕਰ ਸਕੋ।
ਨਤੀਜੇ ਵਜੋਂ, iOS ਉੱਤੇ ਪਾਇਥਨ ਦੀ ਵਰਤੋਂ ਕਰਨ ਦਾ ਇਕੋ ਤਰੀਕਾ ਐਮਬੈਡਡ ਮੋਡ ਹੈ — ਅਰਥਾਤ ਇੱਕ ਮੂਲ iOS ਐਪਲੀਕੇਸ਼ਨ ਲਿਖਣਾ, libPython ਦੀ ਵਰਤੋਂ ਕਰਕੇ ਪਾਇਥਨ ਇੰਟਰਪ੍ਰੀਟਰ ਨੂੰ ਐਮਬੈਡ ਕਰਨਾ ਅਤੇ Python embedding API ਦੀ ਵਰਤੋਂ ਕਰਕੇ ਪਾਇਥਨ ਕੋਡ ਨੂੰ ਚਲਾਉਣਾ। ਫਿਰ ਪੂਰਾ ਪਾਇਥਨ ਇੰਟਰਪ੍ਰੀਟਰ, ਮਿਆਰੀ ਲਾਇਬ੍ਰੇਰੀ ਅਤੇ ਤੁਹਾਡਾ ਸਾਰਾ ਪਾਇਥਨ ਕੋਡ ਇੱਕ ਸੁਤੰਤਰ bundle ਦੇ ਰੂਪ ਵਿੱਚ ਪੈਕੇਜ ਕੀਤਾ ਜਾਂਦਾ ਹੈ ਜਿਸਨੂੰ iOS App Store ਰਾਹੀਂ ਵੰਡਿਆ ਜਾ ਸਕਦਾ ਹੈ।
If you're looking to experiment for the first time with writing an iOS app in Python, projects such as BeeWare and Kivy will provide a much more approachable user experience. These projects manage the complexities associated with getting an iOS project running, so you only need to deal with the Python code itself.
7.1. iOS ਉੱਤੇ ਰਨਟਾਈਮ ਦੌਰਾਨ ਪਾਇਥਨ¶
7.1.1. iOS ਸੰਸਕਰਣ ਅਨੁਕੂਲਤਾ¶
ਸਮਰਥਿਤ ਘੱਟੋ-ਘੱਟ iOS ਸੰਸਕਰਣ ਕੰਪਾਈਲ ਸਮੇਂ configure ਦੀ --host ਵਿਕਲਪ ਰਾਹੀਂ ਨਿਰਧਾਰਤ ਕੀਤਾ ਜਾਂਦਾ ਹੈ। ਮੂਲ ਰੂਪ ਵਿੱਚ, iOS ਲਈ ਕੰਪਾਈਲ ਕੀਤੇ ਜਾਣ 'ਤੇ ਪਾਇਥਨ ਨੂੰ ਘੱਟੋ-ਘੱਟ 13.0 iOS ਸੰਸਕਰਣ ਦੇ ਸਮਰਥਨ ਨਾਲ ਕੰਪਾਈਲ ਕੀਤਾ ਜਾਂਦਾ ਹੈ। ਕਿਸੇ ਹੋਰ ਘੱਟੋ-ਘੱਟ iOS ਸੰਸਕਰਣ ਦੀ ਵਰਤੋਂ ਕਰਨ ਲਈ ਸੰਸਕਰਣ ਨੰਬਰ ਨੂੰ --host ਆਰਗਿਊਮੈਂਟ ਦੇ ਹਿੱਸੇ ਵਜੋਂ ਦਿਓ — ਉਦਾਹਰਨ ਵਜੋਂ, --host=arm64-apple-ios15.4-simulator ਇੱਕ ARM64 simulator build ਨੂੰ 15.4 deployment target ਨਾਲ ਕੰਪਾਈਲ ਕਰੇਗਾ।
7.1.2. ਪਲੇਟਫਾਰਮ ਪਛਾਣ¶
ਜਦੋਂ iOS ਉੱਤੇ ਚਲਾਇਆ ਜਾਂਦਾ ਹੈ, sys.platform ਦਾ ਮੁੱਲ ios ਹੋਵੇਗਾ। ਇਹ ਮੁੱਲ iPhone ਜਾਂ iPad ਦੋਹਾਂ ਉੱਤੇ ਵਾਪਸ ਕੀਤਾ ਜਾਵੇਗਾ, ਭਾਵੇਂ ਐਪ simulator ਉੱਤੇ ਚੱਲ ਰਹੀ ਹੋਵੇ ਜਾਂ ਅਸਲ ਡਿਵਾਈਸ ਉੱਤੇ।
ਖਾਸ ਰਨਟਾਈਮ ਵਾਤਾਵਰਣ ਬਾਰੇ ਜਾਣਕਾਰੀ, ਜਿਸ ਵਿੱਚ iOS ਸੰਸਕਰਣ, ਡਿਵਾਈਸ ਮਾਡਲ ਅਤੇ ਇਹ ਕਿ ਡਿਵਾਈਸ simulator ਹੈ ਜਾਂ ਨਹੀਂ, platform.ios_ver() ਦੀ ਵਰਤੋਂ ਕਰਕੇ ਪ੍ਰਾਪਤ ਕੀਤੀ ਜਾ ਸਕਦੀ ਹੈ। platform.system() ਡਿਵਾਈਸ ਦੇ ਅਨੁਸਾਰ iOS ਜਾਂ iPadOS ਦਰਸਾਏਗਾ।
os.uname() ਕਰਨਲ ਪੱਧਰ ਦੀ ਜਾਣਕਾਰੀ ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ; ਇਹ Darwin ਨਾਮ ਦਰਸਾਏਗਾ।
7.1.3. ਮਿਆਰੀ ਲਾਇਬ੍ਰੇਰੀ ਦੀ ਉਪਲਬਧਤਾ¶
iOS ਉੱਤੇ ਪਾਇਥਨ ਦੀ ਮਿਆਰੀ ਲਾਇਬ੍ਰੇਰੀ ਵਿੱਚ ਕੁਝ ਮਹੱਤਵਪੂਰਨ ਪਾਬੰਦੀਆਂ ਅਤੇ ਗੈਰਹਾਜ਼ਰ ਵਿਸ਼ੇਸ਼ਤਾਵਾਂ ਹਨ। ਵੇਰਵਿਆਂ ਲਈ API availability guide for iOS ਵੇਖੋ।
7.1.4. ਬਾਈਨਰੀ ਐਕਸਟੈਂਸ਼ਨ ਮੋਡੀਊਲ¶
iOS ਪਲੇਟਫਾਰਮ ਦੀ ਇੱਕ ਮਹੱਤਵਪੂਰਨ ਵਿਸ਼ੇਸ਼ਤਾ ਇਹ ਹੈ ਕਿ App Store ਰਾਹੀਂ ਵੰਡ ਲਈ ਐਪਲੀਕੇਸ਼ਨ ਦੀ ਪੈਕੇਜਿੰਗ ਸੰਬੰਧੀ ਸਖ਼ਤ ਲੋੜਾਂ ਹੁੰਦੀਆਂ ਹਨ। ਇਨ੍ਹਾਂ ਵਿੱਚੋਂ ਇੱਕ ਲੋੜ ਇਹ ਨਿਰਧਾਰਤ ਕਰਦੀ ਹੈ ਕਿ ਬਾਈਨਰੀ ਐਕਸਟੈਂਸ਼ਨ ਮੋਡੀਊਲ ਕਿਵੇਂ ਵੰਡੇ ਜਾਣਗੇ।
iOS App Store ਦੀ ਲੋੜ ਹੈ ਕਿ iOS ਐਪ ਵਿੱਚ ਮੌਜੂਦ ਸਾਰੇ ਬਾਈਨਰੀ ਮੋਡੀਊਲ dynamic libraries ਹੋਣ, ਉਚਿਤ metadata ਵਾਲੇ framework ਵਿੱਚ ਸ਼ਾਮਲ ਹੋਣ ਅਤੇ ਪੈਕੇਜ ਕੀਤੀ ਐਪ ਦੇ Frameworks ਫੋਲਡਰ ਵਿੱਚ ਸਟੋਰ ਕੀਤੇ ਜਾਣ। ਹਰ framework ਵਿੱਚ ਕੇਵਲ ਇੱਕ ਬਾਈਨਰੀ ਹੋ ਸਕਦੀ ਹੈ ਅਤੇ Frameworks ਫੋਲਡਰ ਤੋਂ ਬਾਹਰ ਕੋਈ executable ਬਾਈਨਰੀ ਸਮੱਗਰੀ ਨਹੀਂ ਹੋ ਸਕਦੀ।
ਇਹ ਬਾਈਨਰੀਆਂ ਨੂੰ ਵੰਡਣ ਦੇ ਆਮ ਪਾਇਥਨ ਤਰੀਕੇ ਨਾਲ ਟਕਰਾਉਂਦਾ ਹੈ, ਜਿਸ ਵਿੱਚ ਬਾਈਨਰੀ ਐਕਸਟੈਂਸ਼ਨ ਮੋਡੀਊਲ ਨੂੰ sys.path ਉੱਤੇ ਕਿਸੇ ਵੀ ਸਥਾਨ ਤੋਂ ਲੋਡ ਕੀਤਾ ਜਾ ਸਕਦਾ ਹੈ। App Store ਨੀਤੀਆਂ ਦੀ ਪਾਲਣਾ ਯਕੀਨੀ ਬਣਾਉਣ ਲਈ iOS ਪ੍ਰੋਜੈਕਟ ਨੂੰ Python ਪੈਕੇਜਾਂ ਦੀ post-processing ਕਰਨੀ ਪੈਂਦੀ ਹੈ, ਜਿਸ ਵਿੱਚ .so ਬਾਈਨਰੀ ਮੋਡੀਊਲਾਂ ਨੂੰ ਉਚਿਤ metadata ਅਤੇ signing ਵਾਲੇ ਵੱਖਰੇ frameworks ਵਿੱਚ ਤਬਦੀਲ ਕੀਤਾ ਜਾਂਦਾ ਹੈ। ਇਸ post-processing ਨੂੰ ਕਿਵੇਂ ਕਰਨਾ ਹੈ, ਇਸ ਬਾਰੇ ਵੇਰਵਿਆਂ ਲਈ adding Python to your project ਮਾਰਗਦਰਸ਼ਿਕਾ ਵੇਖੋ।
ਪਾਇਥਨ ਨੂੰ ਨਵੇਂ ਸਥਾਨ ਉੱਤੇ ਬਾਈਨਰੀਆਂ ਖੋਜਣ ਵਿੱਚ ਮਦਦ ਕਰਨ ਲਈ, sys.path ਉੱਤੇ ਮੌਜੂਦ ਅਸਲ .so ਫਾਈਲ ਨੂੰ .fwork ਫਾਈਲ ਨਾਲ ਬਦਲਿਆ ਜਾਂਦਾ ਹੈ। ਇਹ ਇੱਕ ਟੈਕਸਟ ਫਾਈਲ ਹੁੰਦੀ ਹੈ ਜਿਸ ਵਿੱਚ framework ਬਾਈਨਰੀ ਦਾ ਸਥਾਨ, ਐਪ bundle ਦੇ ਅਨੁਸਾਰ, ਦਰਜ ਹੁੰਦਾ ਹੈ। Framework ਨੂੰ ਮੁੜ ਅਸਲ ਸਥਾਨ ਤੱਕ ਪਹੁੰਚਣ ਦੇ ਯੋਗ ਬਣਾਉਣ ਲਈ framework ਵਿੱਚ ਇੱਕ .origin ਫਾਈਲ ਹੋਣੀ ਚਾਹੀਦੀ ਹੈ ਜਿਸ ਵਿੱਚ .fwork ਫਾਈਲ ਦਾ ਸਥਾਨ, ਐਪ bundle ਦੇ ਅਨੁਸਾਰ, ਦਰਜ ਹੋਵੇ।
ਉਦਾਹਰਨ ਲਈ, from foo.bar import _whiz ਇੰਪੋਰਟ ਦੇ ਮਾਮਲੇ ਨੂੰ ਵੇਖੋ, ਜਿੱਥੇ _whiz ਨੂੰ sources/foo/bar/_whiz.abi3.so ਬਾਈਨਰੀ ਮੋਡੀਊਲ ਨਾਲ ਲਾਗੂ ਕੀਤਾ ਗਿਆ ਹੈ ਅਤੇ sources ਐਪ bundle ਦੇ ਅਨੁਸਾਰ sys.path ਉੱਤੇ ਦਰਜ ਸਥਾਨ ਹੈ। ਇਹ ਮੋਡੀਊਲ ਲਾਜ਼ਮੀ ਤੌਰ ਉੱਤੇ Frameworks/foo.bar._whiz.framework/foo.bar._whiz ਵਜੋਂ ਵੰਡਿਆ ਜਾਣਾ ਚਾਹੀਦਾ ਹੈ (framework ਦਾ ਨਾਮ ਮੋਡੀਊਲ ਦੇ ਪੂਰੇ import path ਤੋਂ ਬਣਾਇਆ ਜਾਂਦਾ ਹੈ), ਅਤੇ .framework ਡਾਇਰੈਕਟਰੀ ਵਿੱਚ ਇੱਕ Info.plist ਫਾਈਲ ਹੋਣੀ ਚਾਹੀਦੀ ਹੈ ਜੋ ਬਾਈਨਰੀ ਨੂੰ framework ਵਜੋਂ ਪਛਾਣਦੀ ਹੋਵੇ। ਅਸਲ ਸਥਾਨ ਉੱਤੇ foo.bar._whiz ਮੋਡੀਊਲ ਨੂੰ sources/foo/bar/_whiz.abi3.fwork marker ਫਾਈਲ ਦੁਆਰਾ ਦਰਸਾਇਆ ਜਾਵੇਗਾ, ਜਿਸ ਵਿੱਚ Frameworks/foo.bar._whiz/foo.bar._whiz ਮਾਰਗ ਹੋਵੇਗਾ। Framework ਵਿੱਚ Frameworks/foo.bar._whiz.framework/foo.bar._whiz.origin ਵੀ ਹੋਵੇਗੀ, ਜਿਸ ਵਿੱਚ .fwork ਫਾਈਲ ਦਾ ਮਾਰਗ ਦਰਜ ਹੋਵੇਗਾ।
ਜਦੋਂ iOS ਉੱਤੇ ਚਲਾਇਆ ਜਾਂਦਾ ਹੈ, ਪਾਇਥਨ ਇੰਟਰਪ੍ਰੀਟਰ AppleFrameworkLoader ਇੰਸਟਾਲ ਕਰੇਗਾ ਜੋ .fwork ਫਾਈਲਾਂ ਨੂੰ ਪੜ੍ਹ ਅਤੇ ਇੰਪੋਰਟ ਕਰ ਸਕਦਾ ਹੈ। ਇੰਪੋਰਟ ਹੋਣ ਤੋਂ ਬਾਅਦ ਬਾਈਨਰੀ ਮੋਡੀਊਲ ਦਾ __file__ ਗੁਣ .fwork ਫਾਈਲ ਦਾ ਸਥਾਨ ਦਰਸਾਏਗਾ। ਹਾਲਾਂਕਿ ਲੋਡ ਕੀਤੇ ਮੋਡੀਊਲ ਦਾ ModuleSpec ਆਪਣਾ origin framework ਫੋਲਡਰ ਵਿੱਚ ਮੌਜੂਦ ਬਾਈਨਰੀ ਦੇ ਸਥਾਨ ਵਜੋਂ ਦਰਸਾਏਗਾ।
7.1.5. ਕੰਪਾਈਲਰ ਸਟਬ ਬਾਈਨਰੀਆਂ¶
Xcode iOS ਲਈ ਸਿੱਧੇ ਕੰਪਾਈਲਰ ਪ੍ਰਦਾਨ ਨਹੀਂ ਕਰਦਾ; ਇਸ ਦੀ ਬਜਾਏ ਇਹ xcrun ਸਕ੍ਰਿਪਟ ਵਰਤਦਾ ਹੈ ਜੋ ਪੂਰੇ ਕੰਪਾਈਲਰ ਮਾਰਗ ਦਾ ਨਿਰਧਾਰਣ ਕਰਦੀ ਹੈ (ਉਦਾਹਰਨ ਵਜੋਂ, iPhone ਡਿਵਾਈਸ ਲਈ clang ਪ੍ਰਾਪਤ ਕਰਨ ਲਈ xcrun --sdk iphoneos clang)। ਹਾਲਾਂਕਿ ਇਸ ਸਕ੍ਰਿਪਟ ਦੀ ਵਰਤੋਂ ਨਾਲ ਦੋ ਸਮੱਸਿਆਵਾਂ ਪੈਦਾ ਹੁੰਦੀਆਂ ਹਨ:
xcrunਦਾ ਆਉਟਪੁੱਟ ਮਸ਼ੀਨ-ਵਿਸ਼ੇਸ਼ ਮਾਰਗਾਂ ਨੂੰ ਸ਼ਾਮਲ ਕਰਦਾ ਹੈ, ਜਿਸ ਕਾਰਨ ਬਣਿਆ sysconfig ਮੋਡੀਊਲ ਵੱਖ-ਵੱਖ ਉਪਭੋਗਤਾਵਾਂ ਵਿਚਕਾਰ ਸਾਂਝਾ ਨਹੀਂ ਕੀਤਾ ਜਾ ਸਕਦਾ; ਅਤੇਇਸ ਨਾਲ
CC/CPP/LD/ARਪਰਿਭਾਸ਼ਾਵਾਂ ਵਿੱਚ ਖਾਲੀ ਥਾਵਾਂ (spaces) ਸ਼ਾਮਲ ਹੋ ਜਾਂਦੀਆਂ ਹਨ। C ecosystem ਦੇ ਬਹੁਤ ਸਾਰੇ ਸੰਦ ਇਹ ਮੰਨਦੇ ਹਨ ਕਿ ਪਹਿਲੀ ਖਾਲੀ ਥਾਂ ਤੱਕ ਦੀ ਜਾਣਕਾਰੀ ਕੰਪਾਈਲਰ executable ਦਾ ਮਾਰਗ ਹੈ;xcrunਦੀ ਵਰਤੋਂ ਕਰਦੇ ਸਮੇਂ ਇਹ ਧਾਰਣਾ ਸਹੀ ਨਹੀਂ ਰਹਿੰਦੀ।
ਇਨ੍ਹਾਂ ਸਮੱਸਿਆਵਾਂ ਤੋਂ ਬਚਣ ਲਈ ਪਾਇਥਨ ਇਨ੍ਹਾਂ ਸੰਦਾਂ ਲਈ stubs ਪ੍ਰਦਾਨ ਕਰਦਾ ਹੈ। ਇਹ stubs ਅਸਲ xcrun ਸੰਦਾਂ ਦੇ shell script wrappers ਹੁੰਦੇ ਹਨ ਅਤੇ ਕੰਪਾਈਲ ਕੀਤੇ iOS framework ਦੇ ਨਾਲ ਵੰਡੇ ਜਾਣ ਵਾਲੇ bin ਫੋਲਡਰ ਵਿੱਚ ਸ਼ਾਮਲ ਹੁੰਦੇ ਹਨ। ਇਹ ਸਕ੍ਰਿਪਟਾਂ relocatable ਹੁੰਦੀਆਂ ਹਨ ਅਤੇ ਹਮੇਸ਼ਾਂ ਉਚਿਤ ਸਥਾਨਕ ਸਿਸਟਮ ਮਾਰਗਾਂ ਨੂੰ ਲੱਭ ਲੈਂਦੀਆਂ ਹਨ। Framework ਦੇ ਨਾਲ ਦਿੱਤੇ bin ਫੋਲਡਰ ਵਿੱਚ ਇਨ੍ਹਾਂ ਸਕ੍ਰਿਪਟਾਂ ਨੂੰ ਸ਼ਾਮਲ ਕਰਨ ਨਾਲ sysconfig ਮੋਡੀਊਲ ਦੀ ਸਮੱਗਰੀ ਉਪਭੋਗਤਾਵਾਂ ਲਈ ਆਪਣੇ ਮੋਡੀਊਲ ਕੰਪਾਈਲ ਕਰਨ ਵਿੱਚ ਲਾਭਕਾਰੀ ਬਣ ਜਾਂਦੀ ਹੈ। iOS ਲਈ ਤੀਜੇ-ਪੱਖ ਦੇ ਪਾਇਥਨ ਮੋਡੀਊਲ ਕੰਪਾਈਲ ਕਰਦੇ ਸਮੇਂ ਇਹ ਯਕੀਨੀ ਬਣਾਓ ਕਿ ਇਹ stub binaries ਤੁਹਾਡੇ path ਉੱਤੇ ਮੌਜੂਦ ਹਨ।
7.2. iOS ਉੱਤੇ ਪਾਇਥਨ ਇੰਸਟਾਲ ਕਰਨਾ¶
7.2.1. iOS ਐਪਾਂ ਬਣਾਉਣ ਲਈ ਸੰਦ¶
iOS ਲਈ ਬਿਲਡ ਕਰਨ ਵਾਸਤੇ Apple ਦੇ Xcode ਸੰਦਾਂ ਦੀ ਲੋੜ ਹੁੰਦੀ ਹੈ। ਇਹ ਬਹੁਤ ਜ਼ੋਰ ਨਾਲ ਸਿਫਾਰਸ਼ ਕੀਤੀ ਜਾਂਦੀ ਹੈ ਕਿ ਤੁਸੀਂ Xcode ਦਾ ਸਭ ਤੋਂ ਨਵਾਂ ਸਥਿਰ ਸੰਸਕਰਣ ਵਰਤੋ। ਇਸ ਲਈ ਆਮ ਤੌਰ ਉੱਤੇ macOS ਦੇ ਸਭ ਤੋਂ ਨਵੇਂ ਜਾਂ ਦੂਜੇ ਸਭ ਤੋਂ ਨਵੇਂ ਸੰਸਕਰਣ ਦੀ ਲੋੜ ਪਵੇਗੀ, ਕਿਉਂਕਿ Apple ਪੁਰਾਣੇ macOS ਸੰਸਕਰਣਾਂ ਲਈ Xcode ਦੀ ਸੰਭਾਲ ਨਹੀਂ ਕਰਦਾ। iOS ਵਿਕਾਸ ਲਈ Xcode Command Line Tools ਕਾਫ਼ੀ ਨਹੀਂ ਹਨ; ਤੁਹਾਨੂੰ ਪੂਰੀ Xcode ਇੰਸਟਾਲੇਸ਼ਨ ਦੀ ਲੋੜ ਹੈ।
ਜੇ ਤੁਸੀਂ ਆਪਣਾ ਕੋਡ iOS simulator ਉੱਤੇ ਚਲਾਉਣਾ ਚਾਹੁੰਦੇ ਹੋ, ਤਾਂ ਤੁਹਾਨੂੰ ਇੱਕ iOS Simulator Platform ਵੀ ਇੰਸਟਾਲ ਕਰਨਾ ਪਵੇਗਾ। ਜਦੋਂ ਤੁਸੀਂ ਪਹਿਲੀ ਵਾਰ Xcode ਚਲਾਉਂਦੇ ਹੋ, ਤਾਂ ਤੁਹਾਨੂੰ iOS Simulator Platform ਚੁਣਨ ਲਈ ਕਿਹਾ ਜਾਣਾ ਚਾਹੀਦਾ ਹੈ। ਵਿਕਲਪਕ ਤੌਰ ਉੱਤੇ, ਤੁਸੀਂ Xcode Settings ਪੈਨਲ ਦੇ Platforms ਟੈਬ ਤੋਂ iOS Simulator Platform ਸ਼ਾਮਲ ਕਰ ਸਕਦੇ ਹੋ।
7.2.2. ਇੱਕ iOS ਪ੍ਰੋਜੈਕਟ ਵਿੱਚ ਪਾਇਥਨ ਸ਼ਾਮਲ ਕਰਨਾ¶
ਪਾਇਥਨ ਨੂੰ Swift ਜਾਂ Objective C ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹੋਏ ਕਿਸੇ ਵੀ iOS ਪ੍ਰੋਜੈਕਟ ਵਿੱਚ ਸ਼ਾਮਲ ਕੀਤਾ ਜਾ ਸਕਦਾ ਹੈ। ਹੇਠਾਂ ਦਿੱਤੇ ਉਦਾਹਰਨ Objective C ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹਨ; ਜੇ ਤੁਸੀਂ Swift ਵਰਤ ਰਹੇ ਹੋ ਤਾਂ PythonKit ਵਰਗੀ ਲਾਇਬ੍ਰੇਰੀ ਤੁਹਾਡੇ ਲਈ ਲਾਭਕਾਰੀ ਹੋ ਸਕਦੀ ਹੈ।
iOS Xcode ਪ੍ਰੋਜੈਕਟ ਵਿੱਚ ਪਾਇਥਨ ਸ਼ਾਮਲ ਕਰਨ ਲਈ:
Build or obtain a Python
XCFramework. See the instructions in iOS/README.rst (in the CPython source distribution) for details on how to build a PythonXCFramework. At a minimum, you will need a build that supportsarm64-apple-ios, plus one of eitherarm64-apple-ios-simulatororx86_64-apple-ios-simulator.XCframeworkਨੂੰ ਆਪਣੇ iOS ਪ੍ਰੋਜੈਕਟ ਵਿੱਚ ਖਿੱਚ ਕੇ ਸ਼ਾਮਲ ਕਰੋ। ਹੇਠਾਂ ਦਿੱਤੀਆਂ ਹਦਾਇਤਾਂ ਵਿੱਚ ਅਸੀਂ ਮੰਨਦੇ ਹਾਂ ਕਿ ਤੁਸੀਂXCframeworkਨੂੰ ਆਪਣੇ ਪ੍ਰੋਜੈਕਟ ਦੀ ਰੂਟ ਡਾਇਰੈਕਟਰੀ ਵਿੱਚ ਰੱਖਿਆ ਹੈ; ਹਾਲਾਂਕਿ ਤੁਸੀਂ ਮਾਰਗਾਂ ਨੂੰ ਲੋੜ ਅਨੁਸਾਰ ਬਦਲ ਕੇ ਕੋਈ ਹੋਰ ਸਥਾਨ ਵੀ ਵਰਤ ਸਕਦੇ ਹੋ।Drag the
iOS/Resources/dylib-Info-template.plistfile into your project, and ensure it is associated with the app target.ਆਪਣੇ Xcode ਪ੍ਰੋਜੈਕਟ ਵਿੱਚ ਆਪਣਾ ਐਪਲੀਕੇਸ਼ਨ ਕੋਡ ਇੱਕ ਫੋਲਡਰ ਵਜੋਂ ਸ਼ਾਮਲ ਕਰੋ। ਹੇਠਾਂ ਦਿੱਤੀਆਂ ਹਦਾਇਤਾਂ ਵਿੱਚ ਅਸੀਂ ਮੰਨਦੇ ਹਾਂ ਕਿ ਤੁਹਾਡਾ ਕੋਡ ਪ੍ਰੋਜੈਕਟ ਦੀ ਰੂਟ ਵਿੱਚ
appਨਾਮਕ ਫੋਲਡਰ ਵਿੱਚ ਹੈ; ਤੁਸੀਂ ਮਾਰਗਾਂ ਨੂੰ ਲੋੜ ਅਨੁਸਾਰ ਬਦਲ ਕੇ ਕੋਈ ਹੋਰ ਸਥਾਨ ਵੀ ਵਰਤ ਸਕਦੇ ਹੋ। ਇਹ ਯਕੀਨੀ ਬਣਾਓ ਕਿ ਇਹ ਫੋਲਡਰ ਤੁਹਾਡੇ ਐਪ target ਨਾਲ ਸੰਬੰਧਿਤ ਹੈ।ਆਪਣੇ Xcode ਪ੍ਰੋਜੈਕਟ ਦੇ root node ਨੂੰ ਚੁਣ ਕੇ ਅਤੇ ਫਿਰ sidebar ਵਿੱਚ ਦਿਖਾਈ ਦੇਣ ਵਾਲੇ target ਨਾਮ ਨੂੰ ਚੁਣ ਕੇ ਐਪ target ਦੀ ਚੋਣ ਕਰੋ।
"General" ਸੈਟਿੰਗਾਂ ਵਿੱਚ, "Frameworks, Libraries and Embedded Content" ਦੇ ਅਧੀਨ
Python.xcframeworkਸ਼ਾਮਲ ਕਰੋ ਅਤੇ "Embed & Sign" ਚੁਣੋ।"Build Settings" ਟੈਬ ਵਿੱਚ ਹੇਠ ਲਿਖੀਆਂ ਸੈਟਿੰਗਾਂ ਸੋਧੋ:
ਬਿਲਡ ਵਿਕਲਪ
User Script Sandboxing: ਨਹੀਂ
Enable Testability: ਹਾਂ
ਖੋਜ ਮਾਰਗ
Framework Search Paths:
$(PROJECT_DIR)Header Search Paths:
"$(BUILT_PRODUCTS_DIR)/Python.framework/Headers"
Apple Clang - ਚੇਤਾਵਨੀਆਂ - ਸਾਰੀਆਂ ਭਾਸ਼ਾਵਾਂ
Quoted Include In Framework Header: ਨਹੀਂ
Add a build step that copies the Python standard library into your app. In the "Build Phases" tab, add a new "Run Script" build step before the "Embed Frameworks" step, but after the "Copy Bundle Resources" step. Name the step "Install Target Specific Python Standard Library", disable the "Based on dependency analysis" checkbox, and set the script content to:
set -e mkdir -p "$CODESIGNING_FOLDER_PATH/python/lib" if [ "$EFFECTIVE_PLATFORM_NAME" = "-iphonesimulator" ]; then echo "Installing Python modules for iOS Simulator" rsync -au --delete "$PROJECT_DIR/Python.xcframework/ios-arm64_x86_64-simulator/lib/" "$CODESIGNING_FOLDER_PATH/python/lib/" else echo "Installing Python modules for iOS Device" rsync -au --delete "$PROJECT_DIR/Python.xcframework/ios-arm64/lib/" "$CODESIGNING_FOLDER_PATH/python/lib/" fi
Note that the name of the simulator "slice" in the XCframework may be different, depending the CPU architectures your
XCFrameworksupports.Add a second build step that processes the binary extension modules in the standard library into "Framework" format. Add a "Run Script" build step directly after the one you added in step 8, named "Prepare Python Binary Modules". It should also have "Based on dependency analysis" unchecked, with the following script content:
set -e install_dylib () { INSTALL_BASE=$1 FULL_EXT=$2 # The name of the extension file EXT=$(basename "$FULL_EXT") # The location of the extension file, relative to the bundle RELATIVE_EXT=${FULL_EXT#$CODESIGNING_FOLDER_PATH/} # The path to the extension file, relative to the install base PYTHON_EXT=${RELATIVE_EXT/$INSTALL_BASE/} # The full dotted name of the extension module, constructed from the file path. FULL_MODULE_NAME=$(echo $PYTHON_EXT | cut -d "." -f 1 | tr "/" "."); # A bundle identifier; not actually used, but required by Xcode framework packaging FRAMEWORK_BUNDLE_ID=$(echo $PRODUCT_BUNDLE_IDENTIFIER.$FULL_MODULE_NAME | tr "_" "-") # The name of the framework folder. FRAMEWORK_FOLDER="Frameworks/$FULL_MODULE_NAME.framework" # If the framework folder doesn't exist, create it. if [ ! -d "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER" ]; then echo "Creating framework for $RELATIVE_EXT" mkdir -p "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER" cp "$CODESIGNING_FOLDER_PATH/dylib-Info-template.plist" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist" plutil -replace CFBundleExecutable -string "$FULL_MODULE_NAME" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist" plutil -replace CFBundleIdentifier -string "$FRAMEWORK_BUNDLE_ID" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/Info.plist" fi echo "Installing binary for $FRAMEWORK_FOLDER/$FULL_MODULE_NAME" mv "$FULL_EXT" "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/$FULL_MODULE_NAME" # Create a placeholder .fwork file where the .so was echo "$FRAMEWORK_FOLDER/$FULL_MODULE_NAME" > ${FULL_EXT%.so}.fwork # Create a back reference to the .so file location in the framework echo "${RELATIVE_EXT%.so}.fwork" > "$CODESIGNING_FOLDER_PATH/$FRAMEWORK_FOLDER/$FULL_MODULE_NAME.origin" } PYTHON_VER=$(ls -1 "$CODESIGNING_FOLDER_PATH/python/lib") echo "Install Python $PYTHON_VER standard library extension modules..." find "$CODESIGNING_FOLDER_PATH/python/lib/$PYTHON_VER/lib-dynload" -name "*.so" | while read FULL_EXT; do install_dylib python/lib/$PYTHON_VER/lib-dynload/ "$FULL_EXT" done # Clean up dylib template rm -f "$CODESIGNING_FOLDER_PATH/dylib-Info-template.plist" echo "Signing frameworks as $EXPANDED_CODE_SIGN_IDENTITY_NAME ($EXPANDED_CODE_SIGN_IDENTITY)..." find "$CODESIGNING_FOLDER_PATH/Frameworks" -name "*.framework" -exec /usr/bin/codesign --force --sign "$EXPANDED_CODE_SIGN_IDENTITY" ${OTHER_CODE_SIGN_FLAGS:-} -o runtime --timestamp=none --preserve-metadata=identifier,entitlements,flags --generate-entitlement-der "{}" \;
Embedded ਮੋਡ ਵਿੱਚ ਪਾਇਥਨ ਇੰਟਰਪ੍ਰੀਟਰ ਨੂੰ ਸ਼ੁਰੂ ਕਰਨ ਅਤੇ ਵਰਤਣ ਲਈ Objective C ਕੋਡ ਸ਼ਾਮਲ ਕਰੋ। ਇਹ ਯਕੀਨੀ ਬਣਾਓ ਕਿ:
UTF-8 ਮੋਡ (
PyPreConfig.utf8_mode) ਸਮਰਥਿਤ ਹੋਵੇ;Buffered stdio (
PyConfig.buffered_stdio) ਅਸਮਰਥਿਤ ਹੋਵੇ;Bytecode ਲਿਖਣਾ (
PyConfig.write_bytecode) ਅਸਮਰਥਿਤ ਹੋਵੇ;Signal handlers (
PyConfig.install_signal_handlers) ਸਮਰਥਿਤ ਹੋਣ;ਇੰਟਰਪ੍ਰੀਟਰ ਲਈ
PYTHONHOMEਤੁਹਾਡੇ ਐਪ bundle ਦੇpythonਉਪ-ਫੋਲਡਰ ਵੱਲ ਸੰਰਚਿਤ ਹੋਵੇ; ਅਤੇਇੰਟਰਪ੍ਰੀਟਰ ਲਈ
PYTHONPATHਵਿੱਚ ਹੇਠ ਲਿਖੇ ਸ਼ਾਮਲ ਹੋਣ:
ਤੁਹਾਡੇ ਐਪ bundle ਦਾ
python/lib/python3.Xਉਪ-ਫੋਲਡਰ,ਤੁਹਾਡੇ ਐਪ bundle ਦਾ
python/lib/python3.X/lib-dynloadਉਪ-ਫੋਲਡਰ, ਅਤੇਤੁਹਾਡੇ ਐਪ bundle ਦਾ
appਉਪ-ਫੋਲਡਰਤੁਹਾਡੇ ਐਪ bundle ਦਾ ਸਥਾਨ
[[NSBundle mainBundle] resourcePath]ਦੀ ਵਰਤੋਂ ਕਰਕੇ ਪਤਾ ਕੀਤਾ ਜਾ ਸਕਦਾ ਹੈ।
Steps 8, 9 and 10 of these instructions assume that you have a single folder of
pure Python application code, named app. If you have third-party binary
modules in your app, some additional steps will be required:
You need to ensure that any folders containing third-party binaries are either associated with the app target, or copied in as part of step 8. Step 8 should also purge any binaries that are not appropriate for the platform a specific build is targeting (i.e., delete any device binaries if you're building an app targeting the simulator).
Any folders that contain third-party binaries must be processed into framework form by step 9. The invocation of
install_dylibthat processes thelib-dynloadfolder can be copied and adapted for this purpose.If you're using a separate folder for third-party packages, ensure that folder is included as part of the
PYTHONPATHconfiguration in step 10.ਜੇ ਤੀਜੇ-ਪੱਖ ਦੇ ਪੈਕੇਜਾਂ ਵਾਲੇ ਕਿਸੇ ਵੀ ਫੋਲਡਰ ਵਿੱਚ
.pthਫਾਈਲਾਂ ਹੋਣਗੀਆਂ, ਤਾਂ ਤੁਹਾਨੂੰ ਉਸ ਫੋਲਡਰ ਨੂੰ ਸਿੱਧੇPYTHONPATHਜਾਂsys.pathਵਿੱਚ ਜੋੜਨ ਦੀ ਬਜਾਏ ਇੱਕ site directory ਵਜੋਂ (site.addsitedir()ਦੀ ਵਰਤੋਂ ਕਰਕੇ ) ਸ਼ਾਮਲ ਕਰਨਾ ਚਾਹੀਦਾ ਹੈ।
7.2.3. ਪਾਇਥਨ ਪੈਕੇਜ ਦੀ ਜਾਂਚ¶
The CPython source tree contains a testbed project that is used to run the CPython test suite on the iOS simulator. This testbed can also be used as a testbed project for running your Python library's test suite on iOS.
After building or obtaining an iOS XCFramework (See iOS/README.rst for details), create a clone of the Python iOS testbed project by running:
$ python iOS/testbed clone --framework <path/to/Python.xcframework> --app <path/to/module1> --app <path/to/module2> app-testbed
You will need to modify the iOS/testbed reference to point to that
directory in the CPython source tree; any folders specified with the --app
flag will be copied into the cloned testbed project. The resulting testbed will
be created in the app-testbed folder. In this example, the module1 and
module2 would be importable modules at runtime. If your project has
additional dependencies, they can be installed into the
app-testbed/iOSTestbed/app_packages folder (using pip install --target
app-testbed/iOSTestbed/app_packages or similar).
ਇਸ ਤੋਂ ਬਾਅਦ ਤੁਸੀਂ ਆਪਣੇ ਐਪ ਦੀ test suite ਚਲਾਉਣ ਲਈ app-testbed ਫੋਲਡਰ ਦੀ ਵਰਤੋਂ ਕਰ ਸਕਦੇ ਹੋ। ਉਦਾਹਰਨ ਲਈ, ਜੇ module1.tests ਤੁਹਾਡੀ test suite ਦਾ entry point ਹੈ, ਤਾਂ ਤੁਸੀਂ ਹੇਠ ਲਿਖੀ ਕਮਾਂਡ ਚਲਾ ਸਕਦੇ ਹੋ:
$ python app-testbed run -- module1.tests
ਇਹ ਡੈਸਕਟਾਪ Python build ਉੱਤੇ python -m module1.tests ਚਲਾਉਣ ਦੇ ਬਰਾਬਰ ਹੈ। -- ਤੋਂ ਬਾਅਦ ਦਿੱਤੇ ਸਾਰੇ ਆਰਗਿਊਮੈਂਟ testbed ਨੂੰ ਉਸੇ ਤਰ੍ਹਾਂ ਭੇਜੇ ਜਾਣਗੇ ਜਿਵੇਂ ਉਹ ਡੈਸਕਟਾਪ ਮਸ਼ੀਨ ਉੱਤੇ python -m ਨੂੰ ਦਿੱਤੇ ਗਏ ਹੋਣ।
ਤੁਸੀਂ ਹੇਠ ਲਿਖੀ ਕਮਾਂਡ ਚਲਾ ਕੇ testbed project ਨੂੰ Xcode ਵਿੱਚ ਵੀ ਖੋਲ੍ਹ ਸਕਦੇ ਹੋ:
$ open app-testbed/iOSTestbed.xcodeproj
ਇਸ ਨਾਲ ਤੁਸੀਂ debugging ਲਈ Xcode ਦੇ ਪੂਰੇ ਸੰਦ-ਸਮੂਹ ਦੀ ਵਰਤੋਂ ਕਰ ਸਕੋਗੇ।
Test suite ਚਲਾਉਣ ਲਈ ਵਰਤੇ ਜਾਣ ਵਾਲੇ ਆਰਗਿਊਮੈਂਟ test plan ਦੇ ਹਿੱਸੇ ਵਜੋਂ ਪਰਿਭਾਸ਼ਿਤ ਹੁੰਦੇ ਹਨ। Test plan ਨੂੰ ਸੋਧਣ ਲਈ project tree ਵਿੱਚ test plan node ਚੁਣੋ (ਇਹ root node ਦਾ ਪਹਿਲਾ child ਹੋਣਾ ਚਾਹੀਦਾ ਹੈ) ਅਤੇ "Configurations" ਟੈਬ ਚੁਣੋ। ਟੈਸਟਿੰਗ ਆਰਗਿਊਮੈਂਟ ਬਦਲਣ ਲਈ "Arguments Passed On Launch" ਮੁੱਲ ਸੋਧੋ।
The test plan also disables parallel testing, and specifies the use of the
iOSTestbed.lldbinit file for providing configuration of the debugger. The
default debugger configuration disables automatic breakpoints on the
SIGINT, SIGUSR1, SIGUSR2, and SIGXFSZ signals.
7.3. App Store ਅਨੁਕੂਲਤਾ¶
The only mechanism for distributing apps to third-party iOS devices is to submit the app to the iOS App Store; apps submitted for distribution must pass Apple's app review process. This process includes a set of automated validation rules that inspect the submitted application bundle for problematic code.
ਪਾਇਥਨ ਮਿਆਰੀ ਲਾਇਬ੍ਰੇਰੀ ਵਿੱਚ ਕੁਝ ਐਸਾ ਕੋਡ ਸ਼ਾਮਲ ਹੈ ਜੋ ਇਨ੍ਹਾਂ ਆਟੋਮੈਟਿਕ ਨਿਯਮਾਂ ਦੀ ਉਲੰਘਣਾ ਕਰਦਾ ਹੈ। ਹਾਲਾਂਕਿ ਇਹ ਉਲੰਘਣਾਵਾਂ ਅਕਸਰ false positives ਲੱਗਦੀਆਂ ਹਨ, ਪਰ Apple ਦੇ review ਨਿਯਮਾਂ ਨੂੰ ਚੁਣੌਤੀ ਨਹੀਂ ਦਿੱਤੀ ਜਾ ਸਕਦੀ; ਇਸ ਲਈ App Store review ਪਾਸ ਕਰਨ ਲਈ ਪਾਇਥਨ ਮਿਆਰੀ ਲਾਇਬ੍ਰੇਰੀ ਵਿੱਚ ਸੋਧ ਕਰਨੀ ਲਾਜ਼ਮੀ ਹੈ।
Python source tree ਵਿੱਚ a patch file ਸ਼ਾਮਲ ਹੈ ਜੋ App Store review ਪ੍ਰਕਿਰਿਆ ਵਿੱਚ ਸਮੱਸਿਆ ਪੈਦਾ ਕਰਨ ਵਾਲੇ ਜਾਣੇ-ਪਹਿਚਾਣੇ ਕੋਡ ਨੂੰ ਹਟਾ ਦਿੰਦੀ ਹੈ। iOS ਲਈ build ਕਰਦੇ ਸਮੇਂ ਇਹ patch ਆਪਣੇ ਆਪ ਲਾਗੂ ਕੀਤੀ ਜਾਂਦੀ ਹੈ।