<div dir="ltr"><font face="arial, sans-serif">Awesome explanation. Thanks for that.</font><br><div><span style="font-family:arial,sans-serif;font-size:12.800000190734863px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">+1 for </span><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">not polluting the code</span></div>

<div><span style="font-family:arial,sans-serif;font-size:12.800000190734863px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:12.800000190734863px">-- Passos</span></div><div class="gmail_extra">

<br><br><div class="gmail_quote">On Fri, Aug 1, 2014 at 7:47 AM, Christos Vasilakis <span dir="ltr">&lt;<a href="mailto:cvasilak@gmail.com" target="_blank">cvasilak@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

Hi all,<br>
<br>
a heads up on my observations during porting our quickstart app [1]  (Swift port) and making it  compatible to also run on iOS 7 too [2]. Indeed, Swift apps can run on iOS 7 due to the swift-runtime embedded in the application (and can be observed on the generated app archive [3] ).<br>


<br>
A major obstacle faced is that although you can utilise at runtime a form of dynamic version check (e.g. respondsToSelector[] ) to decide to call either iOS 7 or iOS 8 API, the runtime is strict on class loading and fails even when the code-path that instantiates the class is not executed.<br>


<br>
Let me give you a concrete example. Here is snippet of code that uses either the new push registration API available in iOS 8 or fails back to the old one if not:<br>
<br>
--<br>
if application.respondsToSelector(Selector(&quot;registerUserNotificationSettings:&quot;)) {<br>
    let settings = UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil)<br>
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)<br>
    UIApplication.sharedApplication().registerForRemoteNotifications()<br>
} else {<br>
    UIApplication.sharedApplication().registerForRemoteNotificationTypes( .Badge | .Sound | .Alert)<br>
}<br>
<br>
If your Target build SDK is set to ‘Latest (8.0)’ (to avoid compilation error of missing classes) but your deployment target is set to 7.x (to support older versions) and you have some form of dynamic selection on runtime (e.g .respondsToSelector[] ), this fails when running on iOS 7 with a missing symbol:  (note that the code path that instantiates UIUserNotificationSettings is not reached but yet fails at runtime)<br>


<br>
--<br>
dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings<br>
  Referenced from: /var/mobile/Applications/E8C53BD1-285E-4DDD-B71C-C99D61195671/Contacts.app/Contacts<br>
  Expected in: /System/Library/Frameworks/UIKit.framework/UIKit<br>
 in /var/mobile/Applications/E8C53BD1-285E-4DDD-B71C-C99D61195671/Contacts.app/Contacts<br>
--<br>
<br>
In contrast, the obj-c  version of the same code runs fine on iOS 7.<br>
<br>
Apparently this has been observed [4] and some workarounds exist (basically use an objective-c bridge to workaround it, but that is just a ‘hack&#39;). This is to the fact that the obj-c compiler does some form of weak-linking the symbols that are only available on later versions than the deployment target.<br>


<br>
I imagine having the same form of issues when trying to utilise a any new iOS 8 class that doesn’t exist on iOS 7. For that matter (at least for the current state of the Swift runtime) i am leading towards not using any hacks to workaround issues on Swift running on iOS 7.  I am sure Apple with the current pace of dev will come up with techniques, but let’s not pollute the code for the time being.<br>


<br>
Let me know your thoughts.<br>
<br>
-<br>
Christos<br>
<br>
[1] <a href="https://github.com/aerogear/aerogear-push-quickstarts/tree/swift" target="_blank">https://github.com/aerogear/aerogear-push-quickstarts/tree/swift</a><br>
[2] <a href="https://github.com/cvasilak/aerogear-push-quickstarts/tree/swift-ios7" target="_blank">https://github.com/cvasilak/aerogear-push-quickstarts/tree/swift-ios7</a><br>
[3] <a href="http://tinyurl.com/swift-runtime" target="_blank">http://tinyurl.com/swift-runtime</a><br>
[4] <a href="http://stackoverflow.com/questions/24256583/swift-write-code-for-ios-7-and-8" target="_blank">http://stackoverflow.com/questions/24256583/swift-write-code-for-ios-7-and-8</a><br>
_______________________________________________<br>
aerogear-dev mailing list<br>
<a href="mailto:aerogear-dev@lists.jboss.org">aerogear-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/aerogear-dev</a><br>
</blockquote></div><br></div></div>