ios - 加密 iOS Xcode

标签 ios base64

我对私有(private)网络服务有疑问。我的意思是,为了访问此网络服务,我需要用户名。我如何才能访问我的网络服务内部。您有什么建议吗?

要有视觉效果 -- http://www.test.com/event (这是一个例子)

-(void)startConnection
{
    NSString *urlString = GET_EVENT_URL_STRING;

    NSURL *url = [NSURL URLWithString:urlString];

    if (!url)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

        theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];

    // Set the HTTP method of the request to POST
    [theRequest setHTTPMethod:@"POST"];


    if (!theRequest)
    {
        NSString *reason = [NSString stringWithFormat:@"Could not create URL request from string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (!theConnection)
    {
        NSString *reason = [NSString stringWithFormat:@"URL connection failed for string %@", GET_EVENT_URL_STRING];
        [self.delegate didGetEventInCorrect:reason];
        return;
    }

    if (theConnection)
    {
        myData = [[NSMutableData alloc]init];
    }
}

例如,当我单击此链接时,我会看到一个带有用户名和密码屏幕的警报..当我输入我可以访问的信息时..此代码用于连接我的网络服务,我该如何管理它?

最佳答案

您需要向 theRequest 对象添加身份验证 header 。

NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];

关于ios - 加密 iOS Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693291/

相关文章:

ios - Swift 3 发布后,Swift 2 应用程序可以正常运行吗?

ios-charts LineChartView 中的可见数据

ios - 使用 Storyboard 时如何使用 Typhoon 导航到导航 Controller 中的 Controller

iOS:带有 subview Controller 的自动布局

java - 无法解析符号 BASE64Decoder(Java 版本 9.0.1)

java - 如何使用来自图像的数据 URI 作为 InputStream?

java - 如何将 Base64 blob 字符串转换为 android 中的图像?

ios - 更改 Storyboard约束关系

html - Base64 PNG 数据到 HTML5 Canvas

java - RESTful Web 服务中的字节数组与 Base 64 字符串