iProgram
iProgram

Reputation: 6547

Error in Spotify's iOS SDK tutorial

I am currently following Spotify's tutorial on the iOS SDK. I am also converting the Objective-C code t Swift.

Spotify wants me to run this code:

-(BOOL)application:(UIApplication *)application
           openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication
        annotation:(id)annotation {

    // Ask SPTAuth if the URL given is a Spotify authentication callback
    if ([[SPTAuth defaultInstance] canHandleURL:url]) {
        [[SPTAuth defaultInstance] handleAuthCallbackWithTriggeredAuthURL:url callback:^(NSError *error, SPTSession *session) {

             if (error != nil) {
                 NSLog(@"*** Auth error: %@", error);
                 return;
             }

             // Call the -playUsingSession: method to play a track
             [self playUsingSession:session];
        }];
        return YES;
    }

    return NO;
}

I have converted it to Swift:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        if SPTAuth.defaultInstance().canHandleURL(url) {
            SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSErrorPointer, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
            })
            return true
        }
        return false
    }

The swift code, however, contains 2 errors:

1) enter image description here

Why am I getting an error when following Spotify's tutorial? Is it to do with converting Objective-C to Swift? How would I fix this?

2) enter image description here

Upvotes: 0

Views: 304

Answers (1)

SwiftStuff
SwiftStuff

Reputation: 36

When you handle the callback, you are casting the error as a NSErrorPointer rather than NSError.

SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSErrorPointer, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
 }) 

should be

SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURL(url, callback: {(error: NSError, session: SPTSession) -> Void in
                if error != nil {
                    NSLog("*** Auth error: %@", error)
                    return
                }
                playUsingSession(session)
 })

Upvotes: 2

Related Questions