iOS経由でiOSアプリケーションにログインする機能を追加します

エントリー


こんにちは、Habr!

アプリケーションでソーシャルネットワークへの「出口」を持つことは非常に便利です。 そして今日は、有名なソーシャルネットワークを自分のアプリケーションに「ねじ込む」という小さな経験を共有したいと思います。 これについて何が面白いですか? また、場合によっては、本格的なSDKを使用しても意味がありません(公式のVKontakteがないことも考慮に入れています)。 必要なのは、 user_idを見つけて、「Tell Friends」機能を追加することだけでした。 一般的に、興味のある方は皆、カットをお願いします! デザート用-ちょっとしたコーディング。



誰が役に立つでしょうか?


iOS開発の初心者、アプリケーションの機能を拡張したいが、サードパーティのSDKを使用したくない人、そしてもちろんこのビジネスに熱心な人。



始めましょう


まず、公式ドキュメントの小さな「概要」を読み、必要なすべてのキーを受け取って、アプリケーションをスタンドアロンとして登録することもできます。

さて、 access_token (これなしでは生きていけないもの)を取得するには、次の形式のリクエストを送信する必要があります。



http://oauth.vk.com/authorize? client_id=APP_ID& scope=SETTINGS& redirect_uri=REDIRECT_URI& display=DISPLAY& response_type=token
      
      





これらすべてのパラメーターの意味を繰り返しません。少なくとも上記のページで説明されており、せいぜい名前で直観的にわかりやすいものです。



今すぐコード!


ほら、みんなこれを待っていた! アプリケーションに特定のViewControllerがあり、その中に「VKontakte経由でログイン 」ボタンがあるとしましょう。 いいね



access_tokenを取得します
このクラスにいくつかのメソッドを追加します。 要するに、ボタンをクリックした後、 UIWebViewを表示し、ログインし、トークンを取得して、他のリクエストを行うために実行します。

注意! プロジェクトには自動参照カウントが含まれます。



 -(IBAction)vkontakteButton:(id)sender { // webView authWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 20, 300, 400)]; authWebView.tag = 1024; authWebView.delegate = self; UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [self.view addSubview:authWebView]; [authWebView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://oauth.vk.com/authorize?client_id=3038779&scope=wall,offline&redirect_uri=oauth.vk.com/blank.html&display=touch&response_type=token"]]]; //     [self.view.window makeKeyAndVisible]; //      closeButton.frame = CGRectMake(5, 15, 20, 20); closeButton.tag = 1025; [closeButton addTarget:self action:@selector(closeWebView:) forControlEvents:UIControlEventTouchUpInside]; [closeButton setTitle:@"x" forState:UIControlStateNormal]; [self.view addSubview:closeButton]; } -(void) closeWebView { [[self.view viewWithTag:1024] removeFromSuperview]; [[self.view viewWithTag:1025] removeFromSuperview]; } -(IBAction)closeWebViewButton:(id)sender { [self closeWebView]; }
      
      





実際、ここで必要な準備をすべて行いました。 最も重要なこと、つまり要求について詳しく説明します。



 http://oauth.vk.com/authorize?client_id=APP_ID&scope=wall,offline&redirect_uri=oauth.vk.com/blank.html&display=touch&response_type=token
      
      





client_id = APP_ID - APP_IDの代わりに、サイトにアプリケーションを登録した後に取得したものに置き換えます。

scope = wall、offline-壁を操作し、オフラインで作業するためのアクセス権を要求します(トークンが長期間有効期限切れにならないように)。

redirect_uri = oauth.vk.com / blank.html-ここで、要求されたトークンを見つけます。主なタスクは、このページへのリダイレクトを追跡し、すぐに認証ウィンドウを閉じることです(ページは完全にいため、ユーザーはまったく表示する必要はありません)。

display = touch -iPhoneではネイティブのように見えますが、すべてがタッチデバイスで動作するように最適化されています。

response_type = token-ええ 、実際には、取得したいものです。

次に、 oauth.vk.com/blank.htmlへの移行を追跡する必要があります。 UIWebViewには、次のページの読み込み後に呼び出される素晴らしいメソッドwebViewDidFinishLoadがあります(ヘッダーに追加することを忘れないでください)



 -(void) webViewDidFinishLoad:(UIWebView *)webView { // -    NSMutableDictionary* user = [[NSMutableDictionary alloc] init]; //     NSString *currentURL = webView.request.URL.absoluteString; NSRange textRange =[[currentURL lowercaseString] rangeOfString:[@"access_token" lowercaseString]]; //,        if(textRange.location != NSNotFound){ //, ,   NSArray* data = [currentURL componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]]; [user setObject:[data objectAtIndex:1] forKey:@"access_token"]; [user setObject:[data objectAtIndex:3] forKey:@"expires_in"]; [user setObject:[data objectAtIndex:5] forKey:@"user_id"]; [self closeWebView]; //      [[VkontakteDelegate sharedInstance] loginWithParams:user]; } else { //    ... textRange =[[currentURL lowercaseString] rangeOfString:[@"access_denied" lowercaseString]]; if (textRange.location != NSNotFound) { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Ooops! something gonna wrong..." message:@"Check your internet connection and try again!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; [self closeWebView]; } } }
      
      





不正な読み込みなどに関連する瞬間を処理できれば便利ですが、ページが乱雑にならないように、ここでは余分なコードを記述しません。



トークンは次に何を得ましたか?


次に、APIを操作するためのシングルトンを作成します(もちろん、ここでは小さな機能を拡張できます)

ヘッダー:



 #import <Foundation/Foundation.h> @interface VkontakteDelegate : NSObject @property NSString *username, *realName, *ID, *link, *email, *access_token; @property UIImage* photo; + (id)sharedInstance; -(void) loginWithParams: (NSMutableDictionary*) params; -(void) postToWall; @end
      
      







実装:



 #import "VkontakteDelegate.h" @implementation VkontakteDelegate @synthesize username, realName, ID, photo, access_token, email, link; + (id)sharedInstance { static VkontakteDelegate *__sharedInstance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ __sharedInstance = [[VkontakteDelegate alloc]init]; }); return __sharedInstance; } - (id) init { access_token = [[NSUserDefaults standardUserDefaults] objectForKey:@"vk_token"]; ID = [[NSUserDefaults standardUserDefaults] objectForKey:@"vk_id"]; return self; } -(void) loginWithParams:(NSMutableDictionary *)params { ID = [params objectForKey:@"user_id"]; access_token = [params objectForKey:@"access_token"]; //, ! [[NSUserDefaults standardUserDefaults] setValue:access_token forKey:@"vk_token"]; [[NSUserDefaults standardUserDefaults] setValue:ID forKey:@"vk_id"]; [[NSUserDefaults standardUserDefaults] synchronize]; //     NSString *urlString = [NSString stringWithFormat:@"https://api.vk.com/method/users.get?uid=%@&access_token=%@", ID, access_token] ; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSHTTPURLResponse *response = nil; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //    -     ,  ! //      . // , ... NSArray* userData = [responseString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\":{},[]"]]; realName = [userData objectAtIndex:14]; realName = [realName stringByAppendingString:@" "]; realName = [realName stringByAppendingString:[userData objectAtIndex:20]]; // ,  [[NSUserDefaults standardUserDefaults] setValue:@"vkontakte" forKey:@"SignedUpWith"]; [[NSUserDefaults standardUserDefaults] setValue:realName forKey:@"RealUsername"]; [[NSUserDefaults standardUserDefaults] synchronize]; } -(void) postToWall { // -   (   "+") NSString* message = @"vkontakte+wall+posting"; NSString *urlString = [NSString stringWithFormat:@"https://api.vk.com/method/wall.post?uid=%@&message=%@&attachments=http://google.com&access_token=%@", ID, message,access_token] ; NSURL *url = [NSURL URLWithString:urlString]; //,   ,     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSHTTPURLResponse *response = nil; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; }
      
      







そして、これはすべてがサーバー応答を解析する瞬間のように見えるはずの方法です:



 NSError *jsonParsingError = nil; NSMutableDictionary *userInfo = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError]; userInfo = [userInfo objectForKey:@"response"]; realName = [userInfo objectForKey:@"first_name"]; realName = [realName stringByAppendingFormat:[userInfo objectForKey:@"last_name"]];
      
      





ユーザー情報の要求は基本的です( ドキュメント ):



 https://api.vk.com/method/users.get?uid=%@&access_token=%@
      
      





私はそれを説明しません、私たちはより複雑な要求を検討します。 ここに彼は:



 https://api.vk.com/method/wall.post?uid=%@&message=%@&attachments=http://google.com&access_token=%@
      
      







彼はどんな人ですか?

uid =%@ - user_idを置き換えます

message =%@ -必要なメッセージ。

attachments = http://google.com-サイトへのリンクを追加します。

access_token =%@ -実際にはトークンに置き換えます。

詳細はこちら



まとめ


まあ、私たちは目標を達成しました。 費用もかからず、数百行の単純なコードが必要です。 既製のSDKの検索を回避しました-これは必要ありません。

さらに必要な場合は、してください 。 説明されている以外の操作を行う場合は、ユーザーに許可を求めることを忘れないでください。



私はこれをやめて、初心者(「地獄のプログラマー」-ほとんど)に役立つことを願っています。 あなたのコメントとコメントを待っています。 彼らが言うように、「ありがとう、私たちのチャンネルを購読してください」を読んでくれてありがとう。



PSおそらくコードは汚れているかもしれませんが、APIとの相互作用の状況を明確に示しています。



All Articles