How to check if iOS application uses 'StoreKit.framework'? - ios

I write SDK for iOS and I want to validate if StoreKit.framework is linked to application that uses my SDK, so I run:
if ([SKStoreProductViewController class]) {
SKStoreProductViewController *storeController =
[[ SKStoreProductViewController alloc ] init ];
// ...
}
However even if StoreKit.framework is not linked [SKStoreProductViewController class still returns true.
How to solve this problem?
Edit 1
as #x4h1d pointed I created new empty project and added to default Controller:
BOOL isStoreKitAvailable =
(NSClassFromString(#"SKStoreProductViewController") != nil);
// => YES (there is no linked frameworks at all, why I get YES?)
Edit 2
My Provisioning profile has In-App Purchase enabled (not a project itself)
from iOS App IDs:
However from Xcode:
Maybe this is a reason why even empty application has build-in StoreKit?

You can check the storekit availibility using following code.
func checkStoreKitAvailibility() -> Bool {
for bundle in Bundle.allFrameworks {
if ((bundle.classNamed("SKStoreProductViewController")) != nil) {
return true;
}
}
return false;
}
Edit:
For Objective-C you can use:
- (BOOL)checkStoreKitAvailibility {
for (NSBundle *bundle in NSBundle.allFrameworks) {
if ([bundle classNamed:#"SKStoreProductViewController"]) {
return YES;
}
}
return NO;
}

In the Linked Frameworks and Libraries make it Optional instead of Required. So, that if the application developers wants it to implement he will mark the framework as Required in the application. Now if you use the [SKStoreProductViewController class]. it may crash use the NSStringFromClass(#"SKStoreProductViewController") to determine if its safe to use it.

In Xcode,
when we enable the In-App purchase under Capabilities, Xcode automatically links StoreKit.framework and Add the In-App purchase feature to our App ID. Similarly if our App ID already have In-App purchase enabled the same happens.
So by doing simply,
BOOL isStoreKitAvailable = (NSClassFromString(#"SKStoreProductViewController") != nil);
I hope this might help you.

//SWIFT
class func allFrameworks() -> [AnyObject]
//OBJECTIVE-C
+ (NSArray *)allFrameworks
From NSBundle
+allFrameworks
Returns an array of all of the application’s bundles that represent frameworks.
Return Value
An array of all of the application’s bundles that represent frameworks. Only frameworks with one or more Objective-C classes in them are included.
Import Statement
import Foundation
Availability
Available in iOS 2.0 and later.
How to Use
(NSBundle.allFrameworks() -> Return Array of All framework use in project.
You can Check Using for loop apllication contain storeKit.framework or Not as Below.
Swift :---
func isStoreKitAvailable() -> Bool {
for frameWorkName in Bundle.allFrameworks {
if ((frameWorkName.classNamed("SKStoreProductViewController")) != nil) {
return true;
}
}
return false;
}
Objective C :---
- (BOOL)isStoreKitAvailable {
for (NSBundle *frameWorkName in NSBundle.allFrameworks) {
if ([frameWorkName classNamed:#"SKStoreProductViewController"]) {
return YES;
}
}
return NO;
}
Find more reference from here

Related

Compile-time test for availability of UIApplication.shared?

I have some shared code that needs to work in both iOS apps and app extensions, and needs to set UIApplication.shared.isNetworkActivityIndicatorVisible — but only if the code is used in an app.
In an app extension, UIApplication.shared gives this compile error:
'shared' is unavailable: Use view controller based solutions where appropriate instead
That’s fine; I don’t want to use it in the app extension. However, I’m unable to find a way to disable that code at compile time. Sadly, if #available doesn’t seem to do the trick; it shuts off the code path, but the compiler still doesn’t like it:
if #available(iOSApplicationExtension 0, *) {
print("This is an extension")
} else {
print("This is an app")
print(UIApplication.shared) // Unreachable in extension, but still doesn’t compile
}
I don’t see any #if check that handles this.
Is there any way in Swift to conditionally compile the code that requires UIApplication.shared?
A possible solution is to avoid explicit usage of UIApplication.shared and use Objective-C selector wrap instead.
Here is an extension that might help (based on https://github.com/ephread/Instructions/issues/21)
extension UIApplication {
static var safeShared: UIApplication? {
guard UIApplication.responds(to: Selector(("sharedApplication"))) else {
return nil
}
guard let unmanagedSharedApplication = UIApplication.perform(Selector(("sharedApplication"))) else {
return nil
}
return unmanagedSharedApplication.takeRetainedValue() as? UIApplication
}
}
Usage
if let app = UIApplication.safeShared {
result = app.applicationState == .active
}
Happy Coding 👨‍💻

How to detect whether custom keyboard is activated from the keyboard's container app?

I was wondering if there is a method that would allow me to detect from the keyboard container app whether the associated keyboard has been activated in the the device's Settings app.
For example, I am interested in adding a simple "steps" feature inside the container app where step 1 would be "activate the keyboard", and step 2 would be contingent on step 1's completion. As such, I am interested in figuring out whether there is a way to detect whether the keyboard extension is activated?
Thanks!
Here is a method I have used in one of my projects. I think it is what you asked for, hope it helps you.
- (BOOL)isCustomKeyboardEnabled {
NSString *bundleID = #"com.company.app.customkeyboard"; // Replace this string with your custom keyboard's bundle ID
NSArray *keyboards = [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] objectForKey:#"AppleKeyboards"]; // Array of all active keyboards
for (NSString *keyboard in keyboards) {
if ([keyboard isEqualToString:bundleID])
return YES;
}
return NO;
}
Just in case here is Swift version of Kurt's brilliant and awesome answer:
func isKeyboardExtensionEnabled() -> Bool {
guard let appBundleIdentifier = Bundle.main.bundleIdentifier else {
fatalError("isKeyboardExtensionEnabled(): Cannot retrieve bundle identifier.")
}
guard let keyboards = UserDefaults.standard.dictionaryRepresentation()["AppleKeyboards"] as? [String] else {
// There is no key `AppleKeyboards` in NSUserDefaults. That happens sometimes.
return false
}
let keyboardExtensionBundleIdentifierPrefix = appBundleIdentifier + "."
for keyboard in keyboards {
if keyboard.hasPrefix(keyboardExtensionBundleIdentifierPrefix) {
return true
}
}
return false
}
The current documentation states By default, your extension and its containing app have no direct access to each other’s containers.
It is also stating that the container app can share data with the keyboard in the following fashion:
// Create and share access to an NSUserDefaults object.
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc]
initWithSuiteName:#"com.example.domain.MyShareExtension"];
// Use the shared user defaults object to update the user's account.
[mySharedDefaults setObject:theAccountName forKey:#"lastAccountName"];
Read more on this: Communicating and persisting data between apps with App Groups
Obstacle no 1: According to the documentation, for this to work, the RequestsOpenAccess in the plist needs to be set to YES as it would gain the following capability:
Option to use a shared container with the keyboard’s containing app,
which enables features such as providing a custom lexicon management
UI in the containing app
Requesting full access for a simple case like this is definitely not preferred on my side.
Obstacle no 2: Using this knowledge of setting a NSUserDefault, leaves me to think of a method where this can be set in place. But there's no public method indicating an extension is installed. So this is a dead end for now.
--
[Update 1]
Not super relevant but still worth stating: the shouldAllowExtensionPointIdentifier app delegate method in combination with the constant UIApplicationKeyboardExtensionPointIdentifier can deal with disallowing custom keyboards. The extension point identifiers are not unique identifiers of the extension but of their type.
Read more on this: Can I disable custom keyboards (iOS8) for my app?
--
[Update 2]
Another question with same issue, but w/o solution: How to detect an app extension is enabled in containing app on iOS 8?
--
This is a work-in-progress answer stating my findings so far which I hope to be updating coming days should I find a solution.
You can use this function (Swift 3 and 4) to check your custom keyboard extension have open access or not:
func isOpenAccessGranted() -> Bool{
if #available(iOS 10.0, *) {
let originalString = UIPasteboard.general.string
UIPasteboard.general.string = "Sour LeangChhean"
if UIPasteboard.general.hasStrings {
UIPasteboard.general.string = originalString ?? ""
return true
}else{
UIPasteboard.general.string = ""
return false
}
} else {
// Fallback on earlier versions
if UIPasteboard.general.isKind(of: UIPasteboard.self) {
return true
}else{
return false
}
}
}

Check for class existence in Swift

I want to use NSURLQueryItem in my Swift iOS app. However, that class is only available since iOS 8, but my app should also run on iOS 7. How would I check for class existence in Swift?
In Objective-C you would do something like:
if ([NSURLQueryItem class]) {
// Use NSURLQueryItem class
} else {
// NSURLQueryItem is not available
}
Related to this question is: How do you check for method or property existence of an existing class?
There is a nice section in https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW4 called Supporting Multiple Versions of iOS, which explains different techniques for Objective-C. How can these be translated to Swift?
Swift 2.0 provides us with a simple and natural way to do this.It is called API Availability Checking.Because NSURLQueryItem class is only available since iOS8.0,you can do in this style to check it at runtime.
if #available(iOS 8.0, *) {
// NSURLQueryItem is available
} else {
// Fallback on earlier versions
}
Simplest way I know of
if NSClassFromString("NSURLQueryItem") != nil {
println("NSURLQueryItem exists")
}else{
println("NSURLQueryItem does not exists")
}
Try this:
if objc_getClass("NSURLQueryItem") != nil {
// iOS 8
} else {
// iOS 7
}
I've also done it like this too:
if let theClass: AnyClass = NSClassFromString("NSURLQueryItem") {
// iOS 8
} else {
// iOS 7
}
Or, you can also check system version like so, but this isn't the best practice for iOS dev - really you should check if a feature exists. But I've used this for a few iOS 7 hacks... pragmatism over purity.
switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
iOS7 = false
case .OrderedAscending:
iOS7 = true
}

Detect BOOL availability to support multiple iOS versions?

I am trying to find out how one can detect if an external BOOL is available so that I can support iOS 7 and 8. New in iOS 8 is a BOOL you can use to find out if Reduce Transparency is enabled, and I want to implement that check in an if statement, but this will crash on iOS 7 without first checking if the extern BOOL is available. I was surprised I could not find the answer from my web searches.
Here is the BOOl definition:
UIKIT_EXTERN BOOL UIAccessibilityIsReduceTransparencyEnabled() NS_AVAILABLE_IOS(8_0);
And the location I'm using it:
if (UIAccessibilityIsReduceTransparencyEnabled()) {
NSLog(#"transparency is disabled");
}
Please read the SDK Compatibility Guide.
What you need to do is check if the function UIAccessibilityIsReduceTransparencyEnabled exists:
if (UIAccessibilityIsReduceTransparencyEnabled != NULL) {
// function exists, use it
if (UIAccessibilityIsReduceTransparencyEnabled()) {
NSLog(#"transparency is disabled");
}
} else {
// function doesn't exist, do something else
}

How to retrieve iPhone IDFA from API?

I would like to get the device IDFA. How to get this info from iOS official API ?
First, you have to ask permission from the user to use their IDFA:
#import <AppTrackingTransparency/AppTrackingTransparency.h>
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
// Tracking authorization completed. Start loading ads here.
}];
This permission flow will only run once, the first time it is called, even if you re-start the app and/or call it again. If you want to answer differently, you'll have to delete the app off the device or simulator completely and reinstall it. Note that iOS simulators return blanked IDFAs (all zeroes) no matter what the answer to the permission flow. See https://developer.apple.com/documentation/apptrackingtransparency for details, including how to customize the message shown to users when asking to track them. Note that many advertising SDKs have their own consent flow calls that you can use.
To actually get the IDFA once you have permission:
#import <AdSupport/ASIdentifierManager.h>
If you would like to get it as an NSString, use:
[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]
So your code might look like this:
NSString *idfaString = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSLog (#"IDFA: %#", idfaString);
You first have to check if user user has decided to opt out from ad tracking. Only if he allowed it you can use the IDFA.
You can check it by calling isAdvertisingTrackingEnabled method of ASIdentifierManager.
isAdvertisingTrackingEnabled
Check the value of this property before performing any advertising
tracking. If the value is NO, use the advertising identifier only for
the following purposes: frequency capping, conversion events,
estimating the number of unique users, security and fraud detection,
and debugging.
The following code snippet shows how to obtain a string value of IDFA.
ObjC
#import AdSupport;
- (NSString *)identifierForAdvertising {
// Check whether advertising tracking is enabled
if([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
NSUUID *identifier = [[ASIdentifierManager sharedManager] advertisingIdentifier];
return [identifier UUIDString];
}
// Get and return IDFA
return nil;
}
Swift
import AdSupport
func identifierForAdvertising() -> String? {
// Check whether advertising tracking is enabled
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
return nil
}
// Get and return IDFA
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
IDFA - Identifier for Advertising
isAdvertisingTrackingEnabled -> trackingAuthorizationStatus
From iOS v14 Apple deprecated isAdvertisingTrackingEnabled and moved the logic into AppTrackingTransparency Framework. Now user has to grand a permission to read idfa(in the same way as Location permission)
User can control it via:
#iOS 13
#AdSupport
#ASIdentifierManager.shared().isAdvertisingTrackingEnabled
Settings -> Privacy -> Advertising -> Limit Ad Tracking
#iOS 14
#AppTrackingTransparency
#ATTrackingManager.trackingAuthorizationStatus
#a global flag
Settings -> Privacy -> Tracking -> `Allow Apps to Request to Track`
#or select an app from list to control every app separately
#a local flag
Settings -> <app_name> -> Allow Tracking
Implementation
import AppTrackingTransparency
import AdSupport
func getIDFA() -> String? {
// Check whether advertising tracking is enabled
if #available(iOS 14, *) {
if ATTrackingManager.trackingAuthorizationStatus != ATTrackingManager.AuthorizationStatus.authorized {
return nil
}
} else {
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled == false {
return nil
}
}
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
ASIdentifierManager is the official way to garner the Advertising Identification Number from a device running iOS 6+. You can use -[[ASIdentifierManager sharedManager] advertisingIdentifier]; to get it.
Get IDFA in Swift:
import AdSupport
...
let myIDFA: String?
// Check if Advertising Tracking is Enabled
if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
// Set the IDFA
myIDFA = ASIdentifierManager.sharedManager().advertisingIdentifier.UUIDString
} else {
myIDFA = nil
}
Beginning in iOS 10, when a user enables “Limit Ad Tracking,” the OS will send along the advertising identifier with a new value of “00000000-0000-0000-0000-000000000000.”
As per this article: https://fpf.org/2016/08/02/ios-10-feature-stronger-limit-ad-tracking/
Here's a commented helper class in Swift that will give you a nil object for the identifier if the user has turned advertisement tracking off:
import AdSupport
class IDFA {
// MARK: - Stored Type Properties
static let shared = IDFA()
// MARK: - Computed Instance Properties
/// Returns `true` if the user has turned off advertisement tracking, else `false`.
var limited: Bool {
return !ASIdentifierManager.shared().isAdvertisingTrackingEnabled
}
/// Returns the identifier if the user has turned advertisement tracking on, else `nil`.
var identifier: String? {
guard !limited else { return nil }
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
}
Just add it to your project (for example in a file named IDFA.swift) and link the AdSupport.framework in your target via the "Linked Frameworks and Libraries" section in the General settings tab.
Then you can use it like this:
if let identifier = IDFA.shared.identifier {
// use the identifier
} else {
// put any fallback logic in here
}
Swift 3 & 4
var IDFA = String()
if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
IDFA = ASIdentifierManager.shared().advertisingIdentifier
}
Please pay attention that in iOS 14, ASIdentifierManager.shared().isAdvertisingTrackingEnabled is deprecated. please use ATTrackingManager.trackingAuthorizationStatus == .authorized instead.
import AdSupport
import AppTrackingTransparency
extension ASIdentifierManager {
//NOTE: if the user has enabled Limit Ad Tracking, this IDFA will be all zeros on a physical device
static var identifierForAdvertising: String {
// Check whether advertising tracking is enabled
if #available(iOS 14, *) {
guard ATTrackingManager.trackingAuthorizationStatus == .authorized else {
return ""
}
} else {
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
return ""
}
}
// Get and return IDFA
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
}
A nicer approach to get the IDFA or nil if tracking is disabled via iOS Setting is using a (private) extension:
import AdSupport
class YourClass {
func printIDFA() {
print(ASIdentifierManager.shared().advertisingIdentifierIfPresent)
}
}
private extension ASIdentifierManager {
/// IDFA or nil if ad tracking is disabled via iOS system settings
var advertisingIdentifierIfPresent: String? {
if isAdvertisingTrackingEnabled {
return advertisingIdentifier.uuidString
}
return nil
}
}
Just to extend Amro's Swift answer, here's similar code wrapped in a method:
import AdSupport
...
func provideIdentifierForAdvertisingIfAvailable() -> String? {
if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
return ASIdentifierManager.sharedManager().advertisingIdentifier?.UUIDString ?? nil
} else {
return nil
}
}
Swift 5 with encapsulation:
import AdSupport
struct ID{
static var advertising: String? {
// Firstly, Check whether advertising tracking is enabled
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else {
return nil
}
// Then, Get and return IDFA
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
}

Resources