OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用适用于 iPad 的 Apple IOS 设置 Basecamp 凭据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:20
标题: ios - 如何使用适用于 iPad 的 Apple IOS 设置 Basecamp 凭据

我正在尝试为我们正在构建的内部项目请求我公司的 Basecamp 信息。我了解如何在 ASP.NET 环境中添加凭据,但我是 iPad 开发的新手,似乎无法从 Basecamp 获得适当的响应。这就是我正在做的事情:

NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString"http://mybasecampname.basecamphq.com/projects.xml"]
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0 ];

我正在添加 Bsaecamp 需要的 HTTP header ,如下所示:

[theRequest setValue"application/xml" forHTTPHeaderField"Content-Type" ];
    [theRequest setValue"application/xml" forHTTPHeaderField"Accept" ];

我知道我还需要发送我的凭据以进行身份​​验证 - 我的身份验证 token 和我喜欢的任何密码,但我不确定执行此操作的最佳方法。这是我正在尝试的:

NSURLCredential *credential = [NSURLCredential credentialWithUser"MY TOKEN HERE"
                                                             password"x"
                                                persistence:NSURLCredentialPersistenceForSession];

我的问题是:我是在正确的轨道上还是我完全错过了什么?

这里是 Basecamp API 详细信息的链接,它解释了它需要什么:http://developer.37signals.com/basecamp/

帮助表示赞赏。



Best Answer-推荐答案


一如既往,我最终解决了这个问题。一旦我开始使用 didReceiveAuthenticationChallenge 方法,事情就开始到位了。

所以。我简化了我的请求方法:

- (void)startRequest
{
    NSURL *url = [NSURL URLWithString"http://mybasecampproject.basecamphq.com/projects.xml"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    // Start the connection request
    urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    }

然后设置接收认证挑战的方法:

- (void)connectionNSURLConnection *)connection 
didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)challenge
{
    NSLog(@"Authentication challenge..."); 

    NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser"my basecamp token here" password"X"
                                                           persistence:NSURLCredentialPersistenceForSession] autorelease];
        [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];

}

然后设置一个 didReceiveData 方法来捕获返回的数据:

- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)data
{
    NSLog(@"Did receive data"); 

    NSString * strResult = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
    NSLog(strResult);
}

希望这对其他人有所帮助。

仍然很高兴听到更好的方法

JJ

关于ios - 如何使用适用于 iPad 的 Apple IOS 设置 Basecamp 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913269/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4