<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Haroon Baig on Medium]]></title>
        <description><![CDATA[Stories by Haroon Baig on Medium]]></description>
        <link>https://medium.com/@PyBaig?source=rss-d50d9af5c19e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yLENjFUIYhlD76ZIds3lMw.jpeg</url>
            <title>Stories by Haroon Baig on Medium</title>
            <link>https://medium.com/@PyBaig?source=rss-d50d9af5c19e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 15:59:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@PyBaig/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Distribute your Swift code, Libraries and Frameworks using CocoaPods]]></title>
            <link>https://medium.com/@PyBaig/distribute-your-swift-code-libraries-and-frameworks-using-cocoapods-b41c62cd7c94?source=rss-d50d9af5c19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b41c62cd7c94</guid>
            <dc:creator><![CDATA[Haroon Baig]]></dc:creator>
            <pubDate>Thu, 19 Feb 2015 19:22:57 GMT</pubDate>
            <atom:updated>2015-02-19T19:22:57.975Z</atom:updated>
            <content:encoded><![CDATA[<h3>Distribute your Swift code using CocoaPods</h3><p>It was the most exciting day for me when Apple announced Swift in its WWDC 2K14 event. Suddenly, it created a hype around the iDevelopers community, and a lot of them started porting their libraries and frameworks to Swift.<br>CocoaPods is, and will remain the de-facto standard for distributing libraries and frameworks. The beauty of it is that its centralised, which means you can easily trace the version numbers and releases without any hassle. But, the problem was, that the existing version of CocoaPods didn’t support Swift based libraries and frameworks (at the time I’m writing this article). But now, the wait it over. <em>CocoaPods 0.36</em> (pre-release version) comes with the support for Frameworks and Swift. Lovely!</p><h3>What you have to do ?</h3><p>Well, you have to create a <em>.podspec </em>file into the root of your Xcode project. Thats it! Lets take an example of an OpenSourced Swift based CoreMotion wrapper called <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>. I made this wrapper in pure Swift and it gained a lot of traction in the iOS Dev community. If we look closely at the directory structure of it, we will find a file named ‘MotionKit.podspec’. That’s the file where all the magic is, and you only have to add this file to the root of your Xcode project, you’d be ready to distribute your code then. Lets dig deeper into whats in there.</p><p>Also, make sure that you are using the pre-release version of the CocoaPods. You can grab it with just following command.</p><pre>[sudo] gem install cocoapods --pre</pre><h3>The .podspec file — With all its Glory</h3><p>Have a look at the sample .podspec file taken from the famous iOS CoreMotion wrapper, The <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>.</p><pre>Pod::Spec.new do |s|<br>  s.name = &#39;MotionKit&#39;<br>  s.version = &#39;0.6&#39;<br>  s.license = &#39;MIT&#39;<br>  s.summary = &#39;CoreMotion Made insanely simple&#39;<br>  s.homepage = &#39;https://github.com/MHaroonBaig/MotionKit&#39;<br>  s.social_media_url = &#39;https://twitter.com/PyBaig&#39;<br>  s.authors = { &#39;Haroon Baig&#39; =&gt; &#39;haroon.prog@gmail.com&#39; }<br>  s.source = { :git =&gt; &#39;https://github.com/MHaroonBaig/MotionKit.git&#39;, :tag =&gt; s.version }<br><br>  s.ios.deployment_target = &#39;8.0&#39;<br><br>  s.source_files = &#39;MotionKit/*.swift&#39;<br><br>  s.requires_arc = true<br>end</pre><p>Note that the podspec file uses the ruby syntax. Lets go through each of the fields because this is the only thing that you have to understand.</p><ul><li><strong>s.name</strong> is the name of your Pod</li><li><strong>s.version</strong> is the version number (important)</li><li><strong>s.source</strong> is the the link to where the source files of your Code/Framework are.</li><li><strong>s.source_files</strong> tells the podspec file to look for the source files specifically under the MotionKit directory, with the extension of <em>*.swift</em></li></ul><p>Thats all you need. Just push that file to the root of your repository (if you’re using GitHub), or the root of your Xcode project.</p><h3>Now Test your newly created Pod</h3><p>Now is the time to test your pod, to make sure it works fine. Quickly create the pod file into your test project and add the following line to it.</p><pre>pod &#39;[s.name]&#39;, :git =&gt; &#39;[s.source]&#39;</pre><p>Replace the ‘[s.name]’ with the s.name of your podspec file, and ‘[s.source]’ with the s.source that you entered into your podspec file. In the perspective of the example podspec file presented above, it should probably look like this.</p><pre>pod &#39;MotionKit&#39;, :git =&gt; &#39;https://github.com/MHaroonBaig/MotionKit.git&#39;</pre><p>Now run the pod install command from your terminal and see if everything works fine. If the Pod worked out correctly, it’d the be the perfect time for you to take the ownership of your Pod and publish it with the new Trunk Service released with the 0.36 version of CocoaPods.</p><h3>Publish your Pod—The new Trunk Service</h3><p>Here, you just have to do two things. First, register yourself and second, publish your Pod. All it takes is just a few commands and you’re ready to go.</p><h4>Registering yourself</h4><pre>$ pod trunk register haroon.prog@gmail.com &quot;Haroon Baig&quot;</pre><h4>Publishing your Pod</h4><pre>$ pod trunk push [s.name].podspec</pre><p>Replace the ‘[s.name]’ with the one you had entered in your podspec file.</p><p>Having that done, you can now distribute your pod like this.</p><pre>pod &#39;[s.name]&#39;, &#39;~&gt; [s.version]&#39;</pre><p>Replace the s.name and s.version with the ones that you entered in your podspec file. Happy Coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b41c62cd7c94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build your own Cocoa Touch Frameworks, in Swift]]></title>
            <link>https://medium.com/@PyBaig/build-your-own-cocoa-touch-frameworks-in-swift-d4ea3d1f9ca3?source=rss-d50d9af5c19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4ea3d1f9ca3</guid>
            <dc:creator><![CDATA[Haroon Baig]]></dc:creator>
            <pubDate>Thu, 19 Feb 2015 04:17:23 GMT</pubDate>
            <atom:updated>2015-02-19T17:33:22.807Z</atom:updated>
            <content:encoded><![CDATA[<h3>Build your own Cocoa Touch Frameworks, in pure Swift</h3><p>If you are an experienced iDeveloper, you’d have probably heard of a term called Frameworks, more specifically, the ‘Dynamically Linked Frameworks’. Now this is the most intimidating term for most of the folks out there but believe me, its insanely simple to build one. Bear with me. The only thing that you have to remember, is that Apple now allow developers to make dynamically linked Cocoa Touch Frameworks for iOS, to package up your code in a most efficient way. Thats it! (I’m not going into much of the details though, its a pretty deep topic)</p><p>Well, I was developing a nice and clean wrapper around the CoreMotion Framework, called the <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>, which allows you to retrieve the raw data from the Accelerometer, Gyroscope and Magnetometer in just a few lines of code. I kept it completely open sourced from the day first, and soon I started receiving hunderds of emails from many of the fellow Developers out there, asking for the licensing scheme of <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>, so that they could leverage the power of sensors into their apps while making them more engaging and enhancing the overall user experience. I didn’t know this wrapper will gain so much of traction, so I decided to distribute it through CocoaPods and Carthage but wait a minute! Carthage only supports Dynamic Frameworks and CocoaPods hate swift (at the time I’m writing this article). So, the only choice left, was to make a Cocoa Touch Framework, and distribute it through Pods and Carthage.</p><p>It was very intimidating when I thought about the distribution of <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> through CocoaPods and Carthage, because of the fact that it was purely written in Swift. But, it came out that it was really easy to do so, at least for me. Lets have a look.</p><h3>A simple Swift file</h3><p>Its very obvious that you need to have some kind of functionality built right into the Framework that you wanna build. So I’ll start with just a simple Swift file that I made. A very simple one.</p><pre>class SwiftFrameworks {<br>      init (){<br>         println(&quot;Class has been initialised&quot;)<br>      }<br>      <br>     fun doSomething(){        <br>          println(&quot;Yeah, it works&quot;)<br>       }<br> }</pre><p>Long story short, to make it work seamlessly with your Framework, you have to make every method and class initialisers public. Simple enough. The above swift file would then look something like this.</p><pre>public class SwiftFrameworks {<br>      public init (){<br>         println(&quot;Class has been initialised&quot;)<br>      }<br>      <br>     public fun doSomething(){        <br>          println(&quot;Yeah, it works&quot;)<br>       }<br>  }</pre><p>Lets name that file, ‘MotionKit.swift’ (Please don’t mind my naming conventions, I’m made that way).</p><p>Now head over to the Xcode, and create a new CocoaTouch Framework. I’ll name it MotionKit. Just launch the Xcode ► Start New Project ► Under the iOS, select Framework &amp; Library ► Select Cocoa Touch Framework ► Name it and Press Enter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vtLDAcW0sKCX6XnrtxfiFw.png" /></figure><p>Xcode would then give you a template with a boilerplate and guess what? You’re now ready! Your screen should look like the screenshot below. (Note: I named my Framework ‘<a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>’, yours could be different)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8oYYHXZUPT0sO2ON9-j-0A.png" /></figure><p>Now you just have to do one, and only one thing, and that is, click on the small + button on the lower left corner of the screen and select ‘Add New Files’, then select that Swift file that we previously created (Or any other file that you want to include in your Framework), press CMD + B and and and, <strong>we’re done now</strong>.</p><h3>Test your newly developed Framework</h3><p>Now, its the time to add that framework to any of your projects to test its functionality. Right click on the ‘MotionKit.framework’ and select ‘Show in Finder’. (The name might be different in your case, like [Whatever.framework] etc).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Lsestu_Ar_njwulfcqLwxA.png" /></figure><p>After that, a window will appear, where your frameworks would be listed in an order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*zBVVFUo2L2v2jQpkTl_jHg.png" /></figure><p>Now click and drag this .framework file into any of the projects that you want. In my case, I’d drag into my own project, which is <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>. Lets quickly do that. Your screen will appear somewhat like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*cpc5BufgTFIZKiVVU_Pbhg.png" /></figure><p>In your case, the icon would be like a briefcase (a slight problem on my side though). Having that done, select your parent project and link these binaries to it, like you can see that I added ‘MotionKit.framework’ in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BlYr01R7QtPNaQc4uS3Kuw.png" /></figure><p>Now build your app by pressing CMD + B. If it succeeded, that means everything worked out corectly. Now you can import this Framework into your projects just like the regular Frameworks like UIKit and Foundation. In our case, or in my case, I’d do it like this.</p><pre>import MotionKit //The public class included in your framework</pre><pre>public class ExampleToUseFrameworks{<br>      public init (){<br>         let motionKit = MotionKit()<br>          motionKit.doSomething()</pre><pre>      }<br>  }</pre><p>This is all what it takes to build a very nice Dynamic Framework in Pure Swift. Ping me if you have any Queries, I’d love to hear from you. Till then, Happy coding.</p><p>Please don’t forget to visit my repository, the <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>. Feel free to fork it, give it a star, clone it or send me a pull request. I’d really appreciate and love your suggestions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4ea3d1f9ca3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MotionKit — The Missing iOS CoreMotion Wrapper written in Swift]]></title>
            <link>https://medium.com/ios-os-x-development/motionkit-the-missing-ios-coremotion-wrapper-written-in-swift-99fcb83355d0?source=rss-d50d9af5c19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/99fcb83355d0</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Haroon Baig]]></dc:creator>
            <pubDate>Mon, 16 Feb 2015 12:18:01 GMT</pubDate>
            <atom:updated>2015-03-11T17:18:09.108Z</atom:updated>
            <content:encoded><![CDATA[<h3>MotionKit — The Missing iOS CoreMotion Wrapper written in Swift</h3><p>If you’ve ever worked with the CoreMotion Framework, you’d probably agree, that its a curse. Apple puts too many restrictions, and provides various guidelines for the developers who want to leverage the power of sensors into their apps. Everybody knows that sensor enabled apps are power hungry , and many apps are rejected every year just because of the fact that they weren’t able to follow the proper guidelines, thus decreasing the battery performance drastically.</p><p>Same thing happened to me as well, a while ago. I recently joined <a href="http://www.messiahapp.com/">Messiah</a> as a Cofounder. Messiah is actually an emergency alert messenger, truly <a href="https://github.com/MessiahApp">open sourced</a>, to relay distress signal for the user in the case of an accident or emergency. Accident Detection was the core feature of this app, which needed some heavy computations with the data retrieved from some sensors like Accelerometer, Gyroscope and services like Device Motion. With the passage of time, our algorithm got pretty complex and it became really hard to maintain the code, specially the asynchronous calls of the CoreMotion Framework and the object lifecycle of the CMMotionManager throughout the app. So, long story short, I decided to make my own wrapper around the CoreMotion Framework, which just works and delivers what it says.</p><p>I named my wrapper, the <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>. <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> makes it easy for the developers to focus just on the logic, instead of dwindling around the core syntax and the object life-cycle stuff, having the performance metric in mind. You can do pretty much everything with it, i.e. you can retrieve all the sensor data with either push or pull method, recieve sensor data updates by using the powerful Trailing Closures or through the provided Delegates, use the helper methods which are included into the Kit, get the refined and processed data through the Device Motion service of the iOS which uses various sensor fusion algorithms to reduce the noise from the raw sensor data, and much much more.</p><h3>How to install ?</h3><p>Just go <a href="https://github.com/MHaroonBaig/MotionKit">Here</a>, clone the repository or <a href="https://github.com/MHaroonBaig/MotionKit/archive/master.zip">Download</a> it. Having that done, just copy the MotionKit.swift file into your Xcode project. Thats it. Now grab a cup of coffee, and see how the magic is been done.</p><h3>Usage ( My favourite part )</h3><p>The Magic Part is here.</p><p>First, initialise the <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> instance. Its a Must.</p><pre>let motionKit = MotionKit()</pre><h4>Getting Accelerometer Data</h4><p>Now lets say you wanna retrieve the Raw Accelerometer data from the MotionKit. Well, you can do this with just ‘two’ lines of code (without the Braces and formal stuff of course).</p><pre>motionKit.getAccelerometerValues(interval: 1.0){<br>        (x, y, z) in<br>        //Do whatever you want with the x, y and z values<br>        println(&quot;X: \(x) Y: \(y) Z \(z)&quot;)<br>      }</pre><h4>Getting Gyroscope Data</h4><p>Wanna use the Gyroscope ? Well, we’ve got you covered. Getting the raw Gyroscope data is as easy as getting a pie (and eating it too!)</p><pre>motionKit.getGyroValues(interval: 1.0){<br>        (x, y, z) in<br>        //Do whatever you want with the x, y and z values<br>        println(&quot;X: \(x) Y: \(y) Z \(z)&quot;)<br>     }</pre><h4>Getting Magnetometer Data</h4><p>Wanna feel the presence of a ghost inside your room by the Magnetic Field around your iDevice ? Well, it’ll take as much time as it takes to call a rescue service.</p><pre>motionKit.getMagnetometerValues(interval: 1.0){<br>        (x, y, z) in<br>        //Do whatever you want with the x, y and z values<br>        println(&quot;X: \(x) Y: \(y) Z \(z)&quot;)<br>     }</pre><h3>What about the Device Motion service ?</h3><p>I know you were thinking about this. I’m a ghost, haha.</p><p>Lemme talk a little bit about Device Motion service. If you are already familiar with this service, you can skip the beautiful and informative paragraph below (just kidding, its kinda boring, to be honest).</p><p>Actually, CoreMotion offers a ‘Device Motion’ service, which automatically processes and refines the raw sensor data coming from both the Accelerometer and Gyroscope. It uses something called ‘Sensor Fusion Algorithm’, which refines the raw data by measuring the Device’s attitude, rotation rate and the gravity metrics. CMDeviceMotion is an instance which holds all of these magical, processed and refined values but wait a minute ? Is that easy to grab those values ? Yes! but I’ve made it more easy through my <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> wrapper !</p><h4>Getting the CMDeviceMotion instance</h4><p>Well, you can grab that instance which has all those magical values by just … ok, I mean, have a look below.</p><pre>motionKit.getDeviceMotionObject(interval: 1.0){<br>        (deviceMotion) -&gt; () in<br>          var accelerationX = deviceMotion.userAcceleration.x<br>          var gravityX = deviceMotion.gravity.x<br>          var rotationX = deviceMotion.rotationRate.x<br>          var magneticFieldX = deviceMotion.magneticField.x<br>          var attitideYaw = deviceMotion.attitude.yaw<br>          ....<br>        }</pre><p>Thats it ! This was the magic I was talking about. But, there’s much more to it. Follow along with me, it’ll make your entire day.</p><h4>Getting refined and processed Acceleration data</h4><p>You can retrieve the user acceleration, which is already being processed and refined by the Device Motion service, having less noise than the raw Accelerometer data.</p><pre>motionKit.getAccelerationFromDeviceMotion(interval: 1.0){<br>        (x, y, z) -&gt; () in<br>          // Grab the x, y and z values<br>       }</pre><p>As easy as pie. Seriously. Wait, you can grab other sensors data as well, exactly like the method described above.</p><h4>Getting Gravitational Acceleration</h4><p>It’s easy Easy ! Thats what I wanna say !</p><pre>motionKit.getGravityAccelerationFromDeviceMotion(interval: 1.0) {<br>          (x, y, z) -&gt; () in<br>          // x, y and z values are here<br>      }</pre><h4>Getting Magnetic Field around your device</h4><p>See by yourself. You’ll die wondering how easy it was.</p><pre>motionKit.getMagneticFieldFromDeviceMotion(interval: 1.0) {<br>        (x, y, z, accuracy) -&gt; () in<br>        // Get the values with accuracy<br>       }</pre><h4>Getting the Attitude metrics</h4><p>The Device Attitude’s property has the following metrics (I wish I would’ve taken my Physics class in high school)<br>- Roll<br>- Pitch<br>- Yaw<br>- Rotation Matrix<br>- Quaternion<br>I dont know what those Alien looking terms actually mean (just kidding, I never took the physics classes but I was an active student at <a href="https://www.khanacademy.org/">Khan’s Academy</a>). You can grab all those values by just staring at the screen haha.</p><pre>motionKit.getAttitudeFromDeviceMotion(interval: 1.0) {<br>        (attitude) -&gt; () in<br>          var roll = attitude.roll<br>          var pitch = attitude.pitch<br>          var yaw = attitude.yaw<br>          var rotationMatrix = attitude.rotationMatrix<br>          var quaternion = attitude.quaternion<br>       }</pre><p>Easy Enough. Thanks to my parents who insisted me to learn Physics, me lucky.</p><h4>Getting the Rotation Rate of your device</h4><p>Warning ! Don’t blend your iDevice to measure its rotation rate !</p><pre>motionKit.getRotationRateFromDeviceMotion(interval: 1.0) {<br>        (x, y, z) -&gt; () in<br>        // There you go, grab the x, y and z values<br>       }</pre><p>I hope you haven’t tried blending your device to measure the Rotation Rate, huh!</p><h3>Precautionary measures</h3><p>Apple suggests using just one instance of the CMMotionmanager throughout your app to keeps things smoother by using less resources and battery power. I suggest you to stop receiving updates from the sensors as soon as you’re done. The methods for doing this are the following.</p><pre>    //Make sure to call the required function when you&#39;re done<br>    motionKit.stopAccelerometerUpdates()<br>    motionKit.stopGyroUpdates()<br>    motionKit.stopDeviceMotionUpdates()<br>    motionKit.stopmagnetometerUpdates()</pre><h3>Delegate Methods</h3><p><a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> provides the following Delegate Methods. You can either use Trailing Closures or these Delegate methods to get the sensor data. Your choice. Do as you please. Have a look at the provided Delegate Methods <a href="https://github.com/MHaroonBaig/MotionKit#delegates">Here</a>.</p><h3>Our Reddit Channel</h3><p>I’d love to hear from you. I have created a <a href="https://www.reddit.com/r/MotionKit/">Reddit channel</a> to discuss everything about <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a>. You can either create an issue over there on GitHub to request a feature set, Or, you can ping me <a href="https://www.reddit.com/r/MotionKit/">here</a>. Our Reddit channel is <a href="https://www.reddit.com/r/MotionKit/">/r/MotionKit/</a></p><p>For Questions, you can also use the StackOverflow tag ‘MotionKit’.</p><h3>Verdict</h3><p>Nothing else is left to say. Please support <a href="https://github.com/MHaroonBaig/MotionKit">MotionKit</a> by giving it a star. Fork it and send me a pull request if you wanna contribute. Till then, have a good time. Happy Coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99fcb83355d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-os-x-development/motionkit-the-missing-ios-coremotion-wrapper-written-in-swift-99fcb83355d0">MotionKit — The Missing iOS CoreMotion Wrapper written in Swift</a> was originally published in <a href="https://medium.com/ios-os-x-development">iOS App Development</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>