سؤال

أحاول الحصول على عميل صغير يعمل على Twitter وجرى مشكلة عند اختبار مكالمات API التي تتطلب المصادقة.

كلمة المرور الخاصة بي تحتوي على أحرف خاصة فيها، لذلك عندما أحاول استخدام التعليمات البرمجية التالية لا تعمل.

NSString *post = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@%@/statuses/update.json", username, password, TwitterHostname]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

بدأت في النظر إلى Base64 ووضع المصادقة في الرؤوس. وجدت ديف ديبين نشر تطبيقه في Base64، وبدا أنه منطقي. ومع ذلك، عندما حاولت استخدامه، بدأ التحويل البرمجي يشكو كيفية العثور على مكتبات OpenSSL. لذلك قرأت أنني بحاجة إلى الارتباط في مكتبة Libcrypto، ولكن لا يبدو أنه موجود لجهاز iPhone.

لقد قرأت أيضا أن الناس يقولون أن Apple لن تسمح للتطبيقات التي تستخدم مكتبات التشفير، والتي لا معنى لها.

حتى الآن أنا عالق كيندا والارتباك. ما هي أسهل طريقة للحصول على المصادقة الأساسية في تطبيقي؟

هتافات

هل كانت مفيدة؟

المحلول

شيئان. أولا، يجب عليك استخدام أساليب ASYNC بدلا من الطريقة المتزامنة / الطبقة.

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req]
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:30.0];

// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

تتم إدارة المصادقة عن طريق تنفيذ هذه الطريقة في مندوبك:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

وربما تحتاج أيضا إلى تنفيذ هذه الأساليب أيضا:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

يؤدي استخدام طريقة ASYNC إلى إعطاء تجربة مستخدم أفضل على أي حال، حتى على الرغم من التعقيد الإضافي يستحق القيام به حتى دون القدرة على القيام بمصادقة.

نصائح أخرى

يمكنك مباشرة ASLO كتابة قم بتنزيل اسم المستخدم وكلمة المرور في URL الرئيسي IE https: // اسم المستخدم: password@yoururl.com/

بادئ ذي بدء، تحتاج إلى استدعاء ملف مفوض NSurlConnection: -

  • (BOOL) اتصال: (Nsurlconnection *) اتصال CanauthentiCateAgainStrotctionStionSpacePace: (nsurlprotectionspace *) الحماية

    {

    العودة نعم؛ }

ثم اتصل - (الفراغ) اتصال: (NSurlConnection *) اتصال DidreceiveAuthenticationchallenge: (NSurlauthenticationChallenge *) التحدي

{
if ([challenge previousFailureCount] == 0)
        {
            NSURLCredential *newCredential;

            newCredential = [NSURLCredential credentialWithUser:@"username"
                                                       password:@"password"
                                                    persistence:NSURLCredentialPersistenceForSession];
            NSLog(@"NEwCred:- %@ %@", newCredential, newCredential);
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        }
        else
        {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            NSLog (@"failed authentication");
        }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top