RealityKit app and lower iOS deployment target - swift

I have an iOS app with deployment target iOS 10+, I need to add some features that depend only on RealityKit to appear with users whom their iOS version is 13+, the app compiles and runs successfully on real device but the problem is when archiving for upload to AppStore it generates a Swift file and says:
// "No such module RealityKit"
Sure the reason is related to iOS versions <13.0 but I can't edit that file (to add canImport to RealityKit) it's read-only.
My question is how to cross this problem and make it archive successfully with lower versions support?
Here is a demo that shows the problem when archiving Demo.

Firstly :
Do not include Reality Composer's .rcproject files in your archive for distribution. .rcproject bundles contain the code with iOS 13.0+ classes, structs and enums. Instead, supply your project with USDZ files.
Secondly :
To allow iOS 13+ users to use RealityKit features, but still allow non-AR users to run this app starting from iOS 10.0, use the following code (CONSIDER, IT'S A SIMULATOR VERSION):
import UIKit
#if canImport(RealityKit) && TARGET_OS_SIMULATOR
import RealityKit
#available(iOS 13.0, *)
class ViewController: UIViewController {
var arView = ARView(frame: .zero)
override func viewDidLoad() {
super.viewDidLoad()
arView.frame = self.view.frame
self.view.addSubview(arView)
let entity = ModelEntity(mesh: .generateBox(size: 0.1))
let anchor = AnchorEntity(world: [0,0,-2])
anchor.addChild(entity)
arView.scene.anchors.append(anchor)
}
}
#else
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
#endif
Deployment target is iOS 10.0:
Thirdly :
When publishing to the AppStore (in case we have a deployment target lower than iOS 13.0), we must make the import of this framework weakly linked in the build settings (that's because RealityKit is deeply integrated in iOS and Xcode).
So, go to Build Settings –> Linking -> Other linker Flags.
Double-click it, press +, and paste the following command:
-weak_framework RealityKit -weak_framework Combine
P.S. In Xcode 13.3, there's a project setting that also could help
OTHER_LDFLAGS = -weak_framework RealityFoundation
Fourthly :
So, go to Build Settings –> Framework Search Paths.
Then type there the following command:
$(SRCROOT)
it must be recursive.
Fifthly
The archives window:

Related

How play music in SceneKit

I tried to play music in my SceneKit game, but it crashes without a reason.
So I try to add this 3 lines of code in the standard Apple game template (at the end of viewDidLoad):
let music = SCNAudioSource(fileNamed: "Music.mp3")
let action = SCNAction.playAudio(music!, waitForCompletion: false)
ship.runAction(action)
and, at runtime, Xcode show me this message:
com.apple.scenekit.scnview-renderer (8): breakpoint 1.2
Where is my mistake?
I tried to compile and run my code in two different Mac: on my MacBookPro Retina it runs ok with the sound, in my iMac 21,5 it crashes.
So, my code is correct, probably I will fill a radar to Apple.
Note that both Macs are installed with OS Mojave in beta (same version) and also Xcode used is the Beta.
You have to put the audio file into an SCNAssets catalog (with .scnassets extension), like the art.scnassets catalog in the template. Then play the music like that:
if let source = SCNAudioSource(fileNamed: "art.scnassets/Music.mp3") {
let action = SCNAction.playAudio(source, waitForCompletion: false)
ship.runAction(action)
} else {
print("cannot find file")
}
I think you are looking for something like this...
ship.runAction(SKAction.playSoundFileNamed("Music.mp3",waitForCompletion:false));

On macOS Mojave, in a cocoa application, how to use AVSpeechSynthesizer?

AVSpeechSynthesizer is marked as available on macOS Mojave beta.
Previously it was only available on iOS, tvOS and watchOS. But if I prepare a small macOS test project in Xcode 10, it gives me an error "Use of unresolved identifier 'AVSpeechSynthesizer'". on the top, I have:
import Cocoa
import NaturalLanguage
import AVFoundation
My code is:
let string = "Mickey mouse went to town"
let recognizer = NLLanguageRecognizer()
recognizer.processString(string)
let language = recognizer.dominantLanguage!.rawValue
let speechSynthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: language)
speechSynthesizer.speak(utterance)
It is exactly the same code as on iOS, but on iOS it works, on macOS it gives the error. Any help is much appreciated. Thanks
Currently AVFAudio.h on Mojave has this:
#if TARGET_OS_IPHONE
#import <AVFAudio/AVAudioSession.h>
#import <AVFAudio/AVSpeechSynthesis.h>
#endif
And AVSpeechSynthesizer and friends are declared in AVSpeechSynthesis.h. This is the technical reason why you're seeing that error, those headers are only included when compiling for iOS.
But I've tried importing that header manually and I don't think AVSpeechSynthesizer is working correctly on Mojave even if that #if wasn't there. Trying to create an AVSpeechUtterance on Mojave always returns nil.
So this is either unfinished work on Mojave, or the headers are incorrectly annotated and speech synthesis is not available via AVFoundation on Mojave. Note that NSSpeechSynthesizer is still there and has not been marked deprecated.

ARKit crashing on iPad Air 1

So I wanted to try ARKit. I installed iOS 11 on my iPad air but it keeps crashing.
Here's the code in my view controller
import UIKit
import ARKit
class ViewController: UIViewController {
#IBOutlet weak var sceneView: ARSCNView!
#IBOutlet weak var counterLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let scene = SCNScene()
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
let configuration = ARSessionConfiguration()
sceneView.session.run(configuration)
}
}
So I searched up a bit and I came into this:
https://developer.apple.com/documentation/arkit/building_a_basic_ar_experience
which basically says that for devices that has an older chip than the A9, should use ARSessionConfiguration instead of ARWorldTrackingSessionConfiguration however I still keep getting crashes.
I tried the ARKit Demp provided by Apple, same thing.
I also tried sceneView.antialiasingMode = .none but it didn't help either.
Here's the console log I get when it crashes
2017-06-26 21:44:16.539469+0200 ARKitGame[562:56168] [DYMTLInitPlatform] platform initialization successful
2017-06-26 21:44:18.630888+0200 ARKitGame[562:55915] Metal GPU Frame Capture Enabled
2017-06-26 21:44:18.633276+0200 ARKitGame[562:55915] Metal API Validation Enabled
2017-06-26 21:44:19.625366+0200 ARKitGame[562:56176] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-06-26 21:44:19.628963+0200 ARKitGame[562:56176] [MC] Reading from public effective user settings.
2017-06-26 21:44:22.706910+0200 ARKitGame[562:56176] -[MTLTextureDescriptorInternal validateWithDevice:], line 778: error 'MTLTextureDescriptor has invalid pixelFormat (520).'
-[MTLTextureDescriptorInternal validateWithDevice:]:778: failed assertion `MTLTextureDescriptor has invalid pixelFormat (520).'
(lldb)
Apple changed the ARKit documentation with beta 2: it now unequivocally says that ARKit as a whole -- not just world tracking -- requires A9.
Perhaps that explains why even the basic session configuration seemed to never actually work on devices below A9...
I have downloaded the latest beta and run it on iPad Air 1 and still crashes and as Apple mentioned here : https://developer.apple.com/documentation/arkit
Important ARKit requires an iOS device with an A9 or later processor.
To make your app available only on devices supporting ARKit, use the
arkit key in the UIRequiredDeviceCapabilities section of your app's
Info.plist. If augmented reality is a secondary feature of your app,
use the isSupported property to determine whether the current device
supports the session configuration you want to use.
So it seems it only works on A9 processors.
ARKit only works on A9 chips onward: http://www.iphonehacks.com/2017/06/list-iphone-ipad-compatible-arkit-ios-11.html
iPad Air 1 has an A7 chip, so i would think maybe that's the crash you are seeing.
Updated: Didnt notice your comment about older chips using ARSessionConfiguration instead of ARWorldTrackingSessionConfiguration. Maybe you could try changing this settings on the ARKit demo app.

Do Swift-based applications work on OS X 10.9/iOS 7 and lower?

Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower?
For example, I have a machine running OS X 10.8 (Mountain Lion), and I am wondering if an application I write in Swift will run on it.
Or what should I have to create a Swift application using Mac OS?
I just tested it for you, Swift applications compile into standard binaries and can be run on OS X 10.9 and iOS 7.
Simple Swift application used for testing:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var controller = UIViewController()
var view = UIView(frame: CGRectMake(0, 0, 320, 568))
view.backgroundColor = UIColor.redColor()
controller.view = view
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "I'am a test label"
controller.view.addSubview(label)
self.window!.rootViewController = controller
self.window!.makeKeyAndVisible()
return true
}
Swift code can be deployed to OS X 10.9 and iOS 7.0. It will usually crash at launch on older OS versions.
Apple has announced that Swift apps will be backward compatible with iOS 7 and OS X Mavericks. The WWDC app is written in Swift.
Update - As per Xcode 6 Beta 4
iOS 7 and OS X 10.9 minimum deployment target
The Swift compiler and Xcode now enforce a minimum deployment target of iOS 7 or OS X
Mavericks. Setting an earlier deployment target results in a build failure.
From Xcode 6 release note
So my previous answer(Shown below) will not be applicable to any further development. Swift will no longer available for iOS6 and below
A Swift application can be run on iOS 6. Even though many people are saying that Swift will support only iOS 7+ and OS X 10.9+, from my experience it's not.
I have tested a simple application written completely in Swift in an iOS 6 device. It works perfectly fine. As Apple says, Swift code is binary compatible with Objective-C code. It uses the same compiler and runtime to create the binary.
Here is the code I have tested:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonTapped(sender: UIButton!) {
println("buttonTapped")
}
}
It is a simple application, just adding a button programmatically. My application contains only two files, AppDelegate.swift and ViewController.swift.
So if you are not using any new APIs added as part of the iOS 8 SDK or some Swift specific APIs (corresponding API is not available for Objective-C) your application will seamlessly work on iOS 6 or later (tested and working), even on iOS 5 (not tested). Most of the APIs in Swift are just the replacement of the existing Objective-C APIs. In fact they are the same in binary.
Note: As per Xcode 6 beta 4 for swift apps deployment target should be iOS 7 or OS X 10.9(see the above update). So swift will no longer available for iOS6 and below
In brief:
Swift based applications can target back to OS X Mavericks or iOS 7 with that same app.
How is it possible ?
Xcode embeds a small Swift runtime library within your app’s bundle. Because the library is embedded, your app uses a consistent version of Swift that runs on past, present, and future OS releases.
Why should I trust this answer ?
Because I am not saying this answer as one apple guy told me in twitter or I wrote hello world and tested it.
I took it from apple developer blog.
so you can trust this.
Swift uses the same runtime as Objective-C and can even live side-by-side Objective-C in the same application (as per the WWDC 2014 keynote).
This will need to be checked/verified using Xcode 6 and the new SDK to find a final answer.
I read all answers that said: No, Swift does not work with less than iOS 7. But I said YES, I just created a Swift project that does run in Xcode 5 with 6.0 deployment target.
I just create a demo project in Xcode 6 BETA with the Swift programming language selected.
Close Xcode 6 beta, and I open this demo project in Xcode 5 with deployment target 6.0
And also select simulator 6.1.
Then that project runs well in simulator 6.1. My MacOS X is 10.9.3, so I said yes, that runs in lower than iOS 7. with 10.9.3 Mac OS X.
Here it is a screenshot of the simulator:
Here is a demo as well
While code confirms it, Apple itself has said that Swift will be compatible on iOS 7 and Mavericks in their technical keynote (State of the platforms, session 102, around the 34 min 00 sec mark) at WWDC 2014.
Swift applications are supported on iOS 7 and above as stated in Beta 4 release notes. iOS 6.0, 6.1, 7.0, 7.1, 8.0 in Xcode 6 Beta
Swift applications are supported on platforms OS X 10.9 and above. OS X 10.4 to 10.10 in Deployment Target. I have tested on targeting 10.5 to 10.10, and running on 10.9.3
It appears Swift applications don't run on OS X 10.7. I just created a simple GUI application (a view, a lable, a button) which runs fine on Mavericks. The base SDK is set to 10.9 and the deployment target to 10.7. I copied that application from the DerivedData folder over to my 10.7 virtual machine, and it crashs when starting, show this error:
Crashed Thread: 0
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Application Specific Information:
dyld: launch, loading dependent libraries
Dyld Error Message:
Library not loaded: /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics
Referenced from: /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/../Frameworks/libswiftAppKit.dylib
Reason: image not found
Binary Images:
0x109c65000 - 0x109c6afff +private.Swift-Test (1.0 - 1) <649695D0-58FD-3D02-9176-2D40D4E711F2> /Users/USER/Desktop/Swift-Test.app/Contents/MacOS/Swift-Test
0x109c83000 - 0x109dbffff +libswift_stdlib_core.dylib (1.0 - 600.0.34.4.5) <10AAC369-9404-321D-A892-49F65856D7AF> /Users/USER/Desktop/Swift-Test.app/Contents/Frameworks/libswift_stdlib_core.dylib
...
However this message is irritating, since there's certainly a CoreGraphics library in this VM. Other applications that make heavy use of CoreGraphics work just fine.
This is the post I read from apple Swift blog, might be helpful:
App Compatibility:
If you write a Swift app you can trust that your app will work well into the future. In fact, you can target back to OS X Mavericks or iOS 7 with that same app. This is possible because Xcode embeds a small Swift runtime library within your app's bundle. Because the library is embedded, your app uses a consistent version of Swift that runs on past, present, and future OS releases.
Binary Compatibility and Frameworks:
While your app's runtime compatibility is ensured, the Swift language itself will continue to evolve, and the binary interface will also change. To be safe, all components of your app should be built with the same version of Xcode and the Swift compiler to ensure that they work together.
This means that frameworks need to be managed carefully. For instance, if your project uses frameworks to share code with an embedded extension, you will want to build the frameworks, app, and extensions together. It would be dangerous to rely upon binary frameworks that use Swift — especially from third parties. As Swift changes, those frameworks will be incompatible with the rest of your app. When the binary interface stabilizes in a year or two, the Swift runtime will become part of the host OS and this limitation will no longer exist.
I tested running a bare-bone, Swift-based app on an iPod Touch (3rd gen) device. It appears Swift-based apps don't work with iOS 5.x but do work with iOS 6.x.
Here's what shows up in the debug log when I tried to launch the test app with iOS 5.0.1:
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftCoreGraphics.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftDarwin.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftDispatch.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftFoundation.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftObjectiveC.dylib with errno=1
dyld: F_ADDFILESIGS failed for /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswiftUIKit.dylib with errno=1
dyld: Symbol not found: _OBJC_CLASS_$_NSObject
Referenced from: /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib
Expected in: /usr/lib/libobjc.A.dylib
in /private/var/mobile/Applications/59E31E79-9525-43B0-9DF6-8FEF3C0080F1/SwiftTestApp.app/Frameworks/libswift_stdlib_core.dylib
For iOS 6.1.6, the app runs fine without displaying those error messages.
There seems to be a lot of old answers here so I just wanted to post the official response from the Swift team. Swift is backwards compatible with OS X Mavericks and iOS 7
Apple developer swift blog : Objective-C id as Swift Any
Jul 11, 2014
Compatibility
One of the most common questions we heard at WWDC was, “What is the compatibility story for Swift?”. This seems like a great first topic.
App Compatibility
Simply put, if you write a Swift app today and submit it to the App Store this Fall when iOS 8 and OS X Yosemite are released, you can trust that your app will work well into the future. In fact, you can target back to OS X Mavericks or iOS 7 with that same app. This is possible because Xcode embeds a small Swift runtime library within your app’s bundle. Because the library is embedded, your app uses a consistent version of Swift that runs on past, present, and future OS releases.
I also tried a very simple app on 10.8 (one button, sets text on a label). It crashed at startup, as Greg Parker stated:
Dyld Error Message:
Symbol not found: __dispatch_source_type_memorypressure
Referenced from: /Volumes/*/SwifTest.app/Contents/MacOS/../Frameworks/libswiftDispatch.dylib
Expected in: /usr/lib/libSystem.B.dylib
in /Volumes/*/SwifTest.app/Contents/MacOS/../Frameworks/libswiftDispatch.dylib
(This was using a deployment target of 10.7)
Try the following code:
It's working without StoryBoard:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
// Create a nav/vc pair using the custom ViewController class
let nav = UINavigationController()
let vc = ViewController(nibName: "ViewController", bundle: nil)
// Push the vc onto the nav
nav.pushViewController(vc, animated: false)
// Set the window’s root view controller
self.window!.rootViewController = nav
// Present the window
self.window!.makeKeyAndVisible()
return true
}
Yes, in fact Apple has announced that Swift apps will be backward compatible with iOS 7 and OS X Mavericks. Furthermore the WWDC app is written in the Swift programming language.
When it comes to Swift Frameworks.
As for today, with Xcode version 6.1.1 (6A2008a), if the Swift framework is targeted to iOS 7.1, linker report warning
ld: warning: embedded dylibs/frameworks only run on iOS 8 or later.
and application can't be submitted to AppStore. Check this issue : Lint to prevent dynamic libraries and frameworks from passing with iOS 7
Quick Update, effective from February 15th, 2015, we cannot submit apps to the store that were developed using an SDK prior to iOS 8. So, keeping that in mind , its better to not to worry about this issue as many people have suggested that apps made in Swift can be deployed to OS X 10.9 and iOS 7.0 as well.
Answered code-snippet posted by Leandros seems bit old. I have fixed and made it compilable in Swift 5.
Swift 5
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let controller = UIViewController()
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
view.backgroundColor = UIColor.red
controller.view = view
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 284)
label.textAlignment = NSTextAlignment.center
label.text = "I'am a test label"
controller.view.addSubview(label)
self.window!.rootViewController = controller
self.window!.makeKeyAndVisible()
return true
}

EventKit in Universal app (OS3.2)

I made iPhone app with eventkit framework. However, i upgrade to universal app, app can't run in ipad. I got error message
dyld: Library not loaded: /System/Library/Frameworks/EventKit.framework/EventKit
Yes, event kit only work for iOS 4.0 or later. So, how to make it for universal.
I want to make , if iPad app, don't use event kit framework.
However, I can't add
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
in run time
You need to do two things:
First, weak link against the EventKit framework. You do this in Target->Get Info->General.
Secondly, you need to decide at run time whether or not certain functionality is available:
Class someClass = NSClassFromString(#"SomeClass");
if(someClass) {
// iOS4 code
}
else {
// iOS3.2 code
}
Need to make weak linking.
Right Click on Target -> Get Info -> General Tab
change Eventkit Framework required to weak.
I know this is a really weak answer, but the only answer if you need EventKit on the iPad: Wait until next month when iOS 4.2 ships for the iPad. At this time use the 4.2 beta to develop your universal app. Maybe your app can be ready when the App Store starts accepting apps requiring iOS 4.2.