iOS SDK login function crashes after upgrading application to Swift 3 (iOS 10, Xcode 8.0)

Dear all,

I have developed an iOS application named “MMWSwift” to let the user interact with a number of Photons used for various domotica tasks using the iOS SDK package. My program was originally written in Swift 2 and has functioned without any problems.

After upgrading to iOS 10 / Xcode 8.0 / Swift 3 my application, crashes when calling the Particle cloud login function. I have converted all my application Swift 2 source code to Swift 3 and Xcode compiles and builds the converted program with no errors or warnings. But as soon as the application tries to log into the Particle cloud the program crashes.

My podfile contains the following lines:

target 'MMWswift' do
pod 'Spark-SDK'
end

I have run the ‘pod update’ command in order to use the latest version of the Spark-SDK and AFNetworking libraries, getting the following output message:

Update all pods 
Updating local specs repositories

CocoaPods 1.1.0.rc.2 is available. 
To update use: `sudo gem install cocoapods --pre` 
[!] This is a test version we'd love you to try. 
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.1.0.rc.2 

Analyzing dependencies 
Downloading dependencies 
Using AFNetworking (3.1.0) 
Using Spark-SDK (0.5.1) 
Generating Pods project 
Integrating client project 
Pod installation complete! There is 1 dependency from the Podfile and 2 total 
pods installed.

My initial Swift (2) code that ran without problems was:

SparkCloud.sharedInstance().loginWithUser("username@email.com", password: "userpass") { (error:NSError?) -> Void in
    if let e=error {
        println("Wrong credentials or no internet connectivity, please try again")
    }
    else {
        println("Logged in")
    }
}

After converting to Swift 3 my code is:

SparkCloud.sharedInstance().login(withUser: "username@email.com", password: "userpass") { (error:Error?) -> Void in
            if let _ = error {
                  print("Wrong credentials or no internet connectivity, please try again")
            }
            else {
                 print("Logged in")
            }
}

I have set a breakpoint on the first line of this code. The program runs to this point with no problems but crashes immediately when the Xcode debugger is trying to proceed any further. The debugger produces the following messages:

2016-09-16 11:31:52.501 MMWswift[4292:268166] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'
*** First throw call stack:
(
	0   CoreFoundation                      0x00000001055af34b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010501021e objc_exception_throw + 48
	2   CoreFoundation                      0x00000001055b3442 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x0000000104ba6edd -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
	4   MMWswift                            0x0000000103ee446a -[KeychainItemWrapper writeToKeychain] + 970
	5   MMWswift                            0x0000000103ee396a -[KeychainItemWrapper setObject:forKey:] + 218
	6   MMWswift                            0x0000000103ef8ac2 -[SparkSession storeSessionInKeychainAndSetExpiryTimer] + 1234
	7   MMWswift                            0x0000000103ef8408 -[SparkSession initWithNewSession:] + 1016
	8   MMWswift                            0x0000000103ee62e0 __48-[SparkCloud loginWithUser:password:completion:]_block_invoke + 208
	9   MMWswift                            0x0000000103eac504 __116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke.97 + 228
	10  MMWswift                            0x0000000103ecf099 __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2.150 + 201
	11  libdispatch.dylib                   0x0000000108bf6980 _dispatch_call_block_and_release + 12
	12  libdispatch.dylib                   0x0000000108c200cd _dispatch_client_callout + 8
	13  libdispatch.dylib                   0x0000000108c00a1d _dispatch_main_queue_callback_4CF + 733
	14  CoreFoundation                      0x00000001055734f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
	15  CoreFoundation                      0x0000000105538f8d __CFRunLoopRun + 2205
	16  CoreFoundation                      0x0000000105538494 CFRunLoopRunSpecific + 420
	17  GraphicsServices                    0x000000010b243a6f GSEventRunModal + 161
	18  UIKit                               0x0000000105f8cf34 UIApplicationMain + 159
	19  MMWswift                            0x0000000103e902cf main + 111
	20  libdyld.dylib                       0x0000000108c6c68d start + 1
	21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I do not know if the underlying problem causing the crash is in the Spark-SDK library or in the AFNetworking library or in my line of code (see above) that calls the Spark Cloud login function.

Has anyone experienced a similar problem and could you tell me how you solved it?

Thanks in advance!

I am having the exact same issue. From what I can tell, it has to do with the change to Swift 3 and Xcode 8. I have not been able to locate a fix anywhere online yet. Please update your post, if you get this figured out. I have a feeling, it may need to be addressed in the Podfiles, which means that we have to play the waiting game until Particle Updates their code for iOS 10. As this was my first Particle related iOS project that I began working on yesterday, I spent way too much of my time, trying to figure out what the issue was, only to find out, that I am not alone. Best of luck.

cc @ido, our Lead Mobile Developer

We haven’t yet started converting our app/sample code to Swift 3 / iOS 10 / XCode 8 compatibility.

Both SDKs (Cloud and Setup lib) are written in Objective-C but maybe Apple introduced some API changes/deprecations in underlying parts of the SDK. I’ll see if I can publish a quick fix after I investigate this issue. For the meantime I suggest using XCode 7 for building your app.

1 Like

Thank you very much for the quick response time, and for investigating this issue. Please update your post when you have updated the Particle Documentation, or have a fix for this ready to go. I am really excited to get this working, as I have some really cool plans for how to use these in my iOS App I am currently building. Thank you.

Thank you for your responses. I will check out tomorrow (it is almost midnight now in The Netherlands) if I can continue using Xcode 7 and generate code that can run on iOS 10. I will let you know.
But anyhow I will be glad if you would be able to generate a quick temporary solution and thereupon a final upgrade of your iOS SDK’s to Swift 3 / iOS 10 / Xcode 8.

1 Like

Thanks @will and @ido for helping to clear things up!

Dear Ido,

I have gone back to Xcode 7. That allows me to continue development for the time being. However I hope that you will have time to make the ‘quick fix’ available and thereupon move swiftly to Swift 3 / iOS 10 / Xcode 8 as far as sample code and the iOS SDK is concerned.

For anyone in a similar situation:

I have downloaded Xcode 7.3.1 (from https://developer.apple.com/download/more/ ) and installed it in a separate directory. So now Xcode 8 and Xcode 7 are both available on my Mac.
Furthermore in order to be able to generate a working application using Xcode 7 on iOS devices running iOS 10 I have copied the Developer Disk Image necessary for iOS 10 from my installed Xcode 8 package to the Xcode 7 package (path: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneO‌ S.platform/DeviceSup‌port/10.0 (14A345) ).

1 Like

Any luck with a quick fix to allow for Xcode 8 and Swift 3 to work properly? When Apple releases MacOS tomorrow to the public, I have a feeling they will be pulling Xcode 7 from the store, so a fix sooner than later would be very much appreciated. Thank you.

Not yet, this seems to be a bigger task than what I thought it would be.
Still unsure on the right way to wrap the Obj-C APIs with a Swift 3 header, I am messing around with NS_REFINED_FOR_SWIFT directive but not sure how adding a Swift “header” file to the SDK would impact the cocoapod distribution / configuration (and carthage…)

Links I am using for reference:
https://swift.org/migration-guide/


Looking forward is this situation not an inspiration to consider rewriting the iOS SDK completely in Swift 3? Obj-C will become more and more obsolete in the years ahead…

1 Like

That’s a huge effort, we don’t have the manpower to do that now. You are more than welcome to contribute a PR.

I’m afraid that goes beyond my Swift skills at this moment…

Any idea on when you guys think you may be able to have this resolved? I realize that MacOS just came out yesterday, but Xcode 8 and Swift 3 are now officially here and out of beta, so I would hope you guys are pushing pretty hard for a fix here. If this is something that you believe may take a while to repair, then I may need to explore other platforms for my projects. An ETA on how long this is going to take to fix, so it works with Apple’s current Xcode 8 build and Swift 3, would be very much appreciated. Thank you.

Working on it my friend, I’ll know more by the end of this week… I do advise to continue working with XCode 7.3.1 for the meanwhile especially if you have a tight deadline for your app.

1 Like

Good news - this exception is unrelated to Particle porting the SDK to Swift 3 / XCode 8 but an app entitlement issue that has been changed by Apple. Please look here for the solution:

1 Like

Terrific. I have not tested this yet. I will give it a shot later this afternoon. Hope this takes care of the issue. I will report back after i get a chance to test it.

Wow, what an easy fix. A flip of the switch and its all fixed. Those are my favorite kind. Keep up the great work. Thank you very much. I can confirm that this worked for me.

1 Like

That is great news in the early morning!
I have tried it and it works perfectly! Thanks a lot for the searching and finding the solution.

Is this SDK available for download? What about a very simple application for example? I had a version of my app running on a much earlier version of xcode in objective-c but it broke with an upgrade and now I’d like to use the latest version including SwiftUI. My app, an alarm system, requires only Particle Cloud login plus function calls and reading variables.
github/dont45/master-remote. Thanks

1 Like