ios – Spotify:使用web api登录,不接受重定向网址

我正在尝试使用Web API登录Spotify,因为我不需要会话对象.在我的authorize方法中,我需要传入重定向url,但是在.GET请求中不接受格式化iOS重定向url的方式.

func authorize() {
    // create the url
    let url = "https://accounts.spotify.com/authorize"

    // parameters
    let parameters = ["client_id" : kClientID,"response_type" : "code","redirect_uri" : "spotify-discover-login://callback","state" : kState,"scope" : kScopes]

    // response code
    var responseCode = 401

    Alamofire.request(.GET,url,parameters: parameters,headers: nil)
        .responseString{response in
            print(response)
            switch response.result {
            case .Success:
                if let response = response.response {
                    responseCode = response.statusCode
                }
            case .Failure:
                print("fail")
                return
            }

            switch responseCode {
            case 200:
                print("200")
            case 202:
                print("@ACCEPTED")
            case 400:
                print("@BAD REQUEST")
            case 401:
                print("@AUTH FAIL")
            case 403:
                print("@FORBIDDEN")
            case 1004:
                print("@Could NOT CONNECT")
            default: break
            }
    }
}

更新:

这是Xcode给我的错误

FAILURE: Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL”
UserInfo={NSUnderlyingError=0x7fc89b4677a0 {Error
Domain=kcfErrorDomainCFNetwork Code=-1002 “(null)”},
NSErrorFailingURLStringKey=spotify-discover-login://callback/?code=AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXssp8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA&state=random-string-state,
NSErrorFailingURLKey=spotify-discover-login://callback/?code=AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXssp8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA&state=random-string-state,
NSLocalizedDescription=unsupported URL}

解决方法

NSURLErrorDomain代码-1002指向NSURLErrorUnsupportedURL错误.根据 Apple,此错误意味着:

NSURLErrorUnsupportedURL

Returned when a properly formed URL cannot be handled by the framework.

The most likely cause is that there is no available protocol handler for the URL.

Available in iOS 2.0 and later.

根据NSHipster,此错误意味着:

“The connection Failed due to an unsupported URL scheme.”

所以你的URL是正确形成的,但没有协议处理程序知道如何处理“spotify-discover-login”协议.

但当然这是您的自定义URL方案.确保您已在info.plist中正确注册自定义URL方案并已实施
应用:的OpenURL:选项:见tutorial.

另外,您的自定义URL方案名称非常通用.另一个应用程序或Spotify应用程序本身可能现在或将来使用完全相同的方案,这可能会让您在调试时感到头疼.确保方案独一无二,最有可能包括您的应用程序或您公司在计划中的名称,例如“PoKobros-spotify-discover-login”.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...