Conditional Statements for App Extensions - swift

I am trying to add Today Widget to my app and it needs to access the same file the main iOS app accesses. But in this file there's code the widget doesn't need access but needs to stay in that file. Is there some type of conditional I can use that checks if it's building for the widget or anything else like that?
I want to be able to do something like how it is when checking for macCatalyst like:
#if !TODAY_EXTENSION
codeForMainApp()
#endIf
I found this answer that says to just update the your debug and release xcconfig file, but my only xcconfig file for release and debug are the ones for my CocoaPods. Is it safe to update this?

Related

Flutter - how to create a framework where i can have Debug and Prod versions where debug version has some way to choose test environment

I need to create two different versions of the app.
- debug
-prod
for debug, version i need user to select the test environment before anything else begins. i need some way to know what environment user has selected and then load API endpoints config file accordingly.
what's the best way to handle this in flutter?
I have seen in some apps that for iOS, debug options are available in app settings under the standard iOS settings menu , select the app and then see those options in there.
You might want to use Flutter Flavours. As the name suggests, you can practically make flavours of the same app based on your need - debug, test, production etc. And not only API end-points, but you can also configure everything else such as app icon, different labels on the screen etc.
As suggested in this link, you can have different main.dart file for each flavour. You can read different configuration from JSON file (such as API end-point) and rest of your app will remain same. For example, see below :
This is another helpful link.

Where is "containerEnvironment" in CloudKit? (Sandbox vs Production indicator)

The ToString() method of
CKContainer.FromIdentifier("iCloud.com.My-Company").PrivateCloudDatabase
results in this string
<CKDatabase: 0x7ca335b0; databaseScope=Public,
container=<CKContainer: 0x7fd630c0;
containerID=<CKContainerID: 0x7ca33910;
containerIdentifier=iCloud.com.getvalid.CloudKitAtlas,
containerEnvironment="Sandbox">>>
I'm unable to locate "ContainerEnviornment" in any child or parent object.
Where is ContainerEnviornment located?
I want to have some custom logic behave one way in SandBox, another in Production.
The CloudKit container environment is determined at run-time using your app's com.apple.developer.icloud-container-environment entitlement.
There is no easy, built-in way to query this on any of the CloudKit objects.
If you'd like to browse your CloudKit data, you can use CloudKit Dashboard.
If you want to display some additional/different data in your debug builds, you can detect whether your app is a debug or release build, and condition your debug-only display on that.
For Swift:
Open your Target's Build Settings > Swift Compiler - Custom Flags.
Add "-DDEBUG" to the Debug section under "Other Swift Flags".
Then, surround your Debug-only code with:
#if DEBUG
// debug only code
#endif

Create an App within an App

I am being presented with a very interesting project. The task that I must complete is to figure out a way to allow a partner to be involved in an app without giving up their source code. The code will be included in the main bundle of the app so it is not dynamically stored. The partner has a fully functional app that is needed to be ran in a window within the main app at the appropriate time. I know having the partners create a web app would be ideal so it is treated like a webpage but I am more concerned with codes that must be written natively in iOS.
My question is what is the best way to go about solving this? In theory it is like an App within an App. Is there a way if they gave up their .app file I can include this in the bundle and then run it when I catch a certain event? Should I have the partners create their code in a framework and then import into the shell project? What is the best way to approach this problem?
If your 2nd-party doesn't want to provide you with the source code, why doesn't he compile it to object code then let you simply link it to your app?
By the way, at least on official (non-jailbroken) iDevices, apps can't 'embed' or 'open' one another in such a way - you can open an app programmatically if 1. it's a separate app 2. it has a registered special URL associated to its bundle.
Is there a way if they gave up their .app file I can include this in
the bundle and then run it when I catch a certain event?
No, you'll want to have them create a library instead. You can then include that library in your project.
Creating a library is as simple as:
Choose File->New...->Project... in Xcode.
Select the "Cocoa Touch Static Library" project template.
Add your code.
Build.
The result is a static library that you can add to your application(s). The library will contain the compiled code that you added, but doesn't include the source code. The library developer should provide whatever header files are necessary to use the code in the library.
An App within an App is possible however it requires a common data framework that allows one app to reference the same data without confusing the the source of and destination of the data.
Such a framework allows one app to interact with another app referencing the same data.

How to check if not available methods are used if deployment target < base sdk?

I would like to know how you check that your code do not call not available methods when the deployment target is inferior to base SDK ?
It is possible to run the application on a device with the SDK equal to deployment target, but I search a way more 'automatic'. Any idea ?
Regards,
Quentin
The easiest way to do this is to use the __IPHONE_OS_VERSION_MAX_ALLOWED preprocessor define.
You do this by adding
__IPHONE_OS_VERSION_MAX_ALLOWED=__IPHONE_4_2
or something similar to your "Preprocessor Macros" option in Build Settings of your target. You can look up versions available in <Availability.h>.
Unfortunately if you add this define it will cause mismatch errors with your precompiled header. So, to fix that you need to turn off the "Precompile Prefix Header" option in your build settings as well.
Once you do this you'll get a bunch of errors for classes that don't exist on your targeted SDK (for instance NSOrderedSet doesn't exist in iOS 4.2). If you're trying to go back pre-iOS 4 you'll probably get so many errors that the compiler bails--I don't know of a workaround for this. In any case, ignore the errors about missing classes in the UIKit headers, and go to the bottom of the error list; there you should find an error for each time you use a method or class that isn't included in the SDK pointed to by __IPHONE_OS_VERSION_MAX_ALLOWED. Make sure each of these methods is enclosed in an
if( [targetObject respondsToSelector:#selector(thePossiblyMissingSelector:)]
and you should be safe. Classes that may be missing should be tested as well
if ([NSOrderedSet class] != nil)
These settings aren't something you want to accidentally forget to flip back however. To make this an automatic option for testing, do the following:
Create a new build configuration called something like "Old SDK Testing".
Define __IPHONE_OS_VERSION_MAX_ALLOWED and the precompiled head option only for this configuration (hit the disclosure arrow beside each line in Build Settings to access per configuration settings).
Duplicate your current Scheme and set its name to something like "Old SDK Check".
Set the Build Configuration of the Run item in this new scheme to the build configuration you created in step 1.
Select the new Scheme and build.
Notes:
I make no guarantee that this will catch any/all of your issues.
Anything outside of UIKit will not be caught by this check.
This is not a substitute for testing your code on the versions of iOS you
plan to support.
use NSClassFromString();
Class cls = NSClassFromString(#"YourClass");
if (cls == nil)
is this you are looking for?
best way to do that which i found: compile code with old SDK :) link which can help
I think this question is releated with next
I belive that someday Apple allow to compile project for old SDK by simple defining #define __IPHONE_OS_VERSION_MAX_ALLOWED __IPHONE_3_0
upd: I found solution here
4.3 5.0 and 5.1 SDK just fail to compile after trying to redefine this macro
Are you looking for something like
- (BOOL)respondsToSelector:(SEL)aSelector
If you have an instance of a class, you can use the following to see if it understands the method you want to call:
if ([mipmapBrowserView respondsToSelector:#selector(setBackgroundColor:)]) {
// set the background layer since IKImageView supports it
}
Here, mipmapBrowserView is an instance of IKImageView, which was first introduced in Mac OS X 10.5. The setBackgroundColor: method of IKImageView was only added in 10.6, however, so I need to check before I call it. This allows me to build against the 10.6 SDK, and take advantage of the new features, yet still support OS X 10.5 as well. While this example involves OS X rather than iOS, the same method (pun intended?) works in iOS as well.
Note that things are slightly different when you are subclassing a class, and you want to know whether the superclass responds to a certain selector:
"You cannot test whether an object inherits a method from its superclass by sending respondsToSelector: to the object using the super keyword. This method will still be testing the object as a whole, not just the superclass’s implementation. Therefore, sending respondsToSelector: to super is equivalent to sending it to self. Instead, you must invoke the NSObject class method instancesRespondToSelector: directly on the object’s superclass...."

How to populate Info.plist values dynamically in Xcode?

Can't figure out how to populate CFBundleVersion dynamically with ${BUNDLE_VERSION} which I would like to define as
BUNDLE_VERSION=`date "+%y%m%d"`
If you're doing command-line builds with xcodebuild, you can do something like
xcodebuild -target MyApp -configuration AppStore BUNDLE_VERSION=`date "+%y%m%d"`
However, I advise against doing this. An App Store app has three versions:
The iTunes Connect version number (this is the only one normally shown to the user)
CFBundleVersion
CFBundleShortVersionString
I think they're all supposed to be of the form [0-9]+.[0-9]+(.[0-9]+)?. To avoid confusion, I set them all to the same thing for App Store builds (we include CFBundleVersion/CFBundleShortVersionString in bug reports, and it's nice if they match CFBundleVersion). Non-App Store builds can include more info, since they don't need to be submitted.
I don't know if iTunes Connect lets you submit an app with CFBundleVersion that doesn't contain a ".", but I haven't extensively tested this.
You’ll need an Xcode configuration file and a configuration variable that you set at build time. This is described in some detail at Diego Massanti’s blog. You’ll need to modify the build phase he describes to set the variable to the current date instead of incrementing the existing value.