Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Azure > Windows Azure (Mobile Services) and iOS (iPhone , iPad) > ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย iOS / iPhone



Clound SSD Virtual Server

ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย iOS / iPhone

ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย iOS / iPhone ถ้าเคยใช้พวก App บน Mobile บ่อย ๆ เราอาจจะเคยเห็นว่า App บางตัวสามารถที่จะติดต่อกับ Social Network พวก Twitter , Facebook หรือ Google Account ได้ เช่นการโพสข้อความ หรือการแสดงข้อมูลต่าง ๆ จาก Social Network ที่เรา Login ในเครื่องนั้น ได้ และบน Mobile Services ของ Windows Azure ก็สามารถที่จะทำการเชื่อมต่อไปยัง Social Network เหล่านั้นได้เช่นเดียวกัน โดยในปัจจุบันของรับ 4 ตัวด้วยกันคือ

  • Microsoft Account
  • Facebook login
  • Twitter login
  • Google login


ในการเขียน Mobile Services เพื่อทำการ Authentication Account จาก Social และ Provider เหล่านี้ เราจะต้องทำการ Register เพื่อขอพวก ID จาก Provider นั้น ๆ ก่อน

Authentication  Azure Mobile Services iOS iPhone

ตอนนี้เรามี Mobile Services ทีได้สร้างไว้ก่อนหน้านี้แล้ว 1 ตัว

Authentication  Azure Mobile Services iOS iPhone

หน้าจอ Dashboard ของ Mobile Services จะมี URL ของ Mobile Services อยู่ ซึ่งเราจะต้องเอา URL นี้ไปทำการ Register กับ Social ของค่ายนั้น ๆ ก่อน

Authentication  Azure Mobile Services iOS iPhone

หน้าจอสำหรับการกำหนด ID ของ Social แต่ล่ะค่าย

ลงทะบียนของแต่ล่ะค่ายได้ตาม Link นี้









Example ในตัวอย่างนี้เราจะกำหนดว่าจะต้อง Login ผ่าน Social Provider ของ Google ก่อนถึงจะสามารถดูข้อมูลได้

Authentication  Azure Mobile Services iOS iPhone

ให้ไปที่ DATA เลือก Table ที่ต้องการ

Authentication  Azure Mobile Services iOS iPhone

ในส่วนของ PERMISSIONS ให้เลือกเป็นแบบ Only Authenticateds Users

จากนั้นในส่วนของ Code สามารถที่จะเขียนดักการ Authentication ด้วยคำสั่งง่าย ๆ นี้

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Start Service
    self.memberService = [MyMemberService defaultService];
    
    MSClient *client = self.memberService.client;

    [client loginWithProvider:@"Google" controller:self animated:YES completion:^(MSUser *user, NSError *error) {
        if (client.currentUser == nil) {
            UIAlertView *alert =[[UIAlertView alloc]
                                 initWithTitle:@"Error"
                                 message:@"You must log in. Login Required" delegate:self
                                 cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
        else
        {
            [self.memberService refreshDataOnSuccess:^
             {
                 [myTable reloadData];
             }];
        }
    }];
    
}


มาดูตัวอย่างการทำ Authentication ผ่าน Google provider

Authentication  Azure Mobile Services iOS iPhone

ในหน้า Project iOS บน Xcode โครงสร้างไฟล์

Authentication  Azure Mobile Services iOS iPhone

ก่อนการแสดงผลข้อมูลเราจะเขียนดักเงื่อนไขตรวจสอบการ Login ของ Account ที่ต้องการตรวจสอบหรือไม่ เป็นการสร้างเงื่อนไข if แบบง่าย ๆ

Screenshot

Authentication  Azure Mobile Services iOS iPhone

กรณีที่ Authen ผ่าน

Authentication  Azure Mobile Services iOS iPhone

กรณีที่ Authen ไม่ผ่าน

ในไฟล์ ViewController.h และ ViewController.m เขียน Code ดังนี้

ViewController.h
//
//  ViewController.h
//  myAppMobileService
//
//  Created by Weerachai on 5/12/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITableView *myTable; 
}

@end


ViewController.m
//
//  ViewController.m
//  myAppMobileService
//
//  Created by Weerachai on 5/12/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
#import "MyMemberService.h"

#import "ViewController.h"

@interface ViewController ()
 
// Private properties
@property (strong, nonatomic)   MyMemberService   *memberService;

@end


@implementation ViewController

@synthesize memberService;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Start Service
    self.memberService = [MyMemberService defaultService];
    
    MSClient *client = self.memberService.client;

    [client loginWithProvider:@"Google" controller:self animated:YES completion:^(MSUser *user, NSError *error) {
        if (client.currentUser == nil) {
            UIAlertView *alert =[[UIAlertView alloc]
                                 initWithTitle:@"Error"
                                 message:@"You must log in. Login Required" delegate:self
                                 cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
        else
        {
            [self.memberService refreshDataOnSuccess:^
             {
                 [myTable reloadData];
             }];
        }
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
                                       reuseIdentifier : CellIdentifier] autorelease];
    }
    
    NSDictionary *item = [self.memberService.items objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"name"];
    cell.detailTextLabel.text= [item objectForKey:@"email"];
    
    return cell;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Always a single section
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of items in the todoService items array
    return [self.memberService.items count];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [myTable release];
    [super dealloc];
}
@end


อันนี้ Code ในไฟล์ MyMemberService.h และ MyMemberService.m

MyMemberService.h
//
//  MyMemberService.h
//  myAppMobileService
//
//  Created by Weerachai on 7/21/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>
#import <Foundation/Foundation.h>
 

typedef void (^QSCompletionBlock) ();
typedef void (^QSBusyUpdateBlock) (BOOL busy);


@interface MyMemberService : NSObject

@property (nonatomic, strong)   NSArray *items;
@property (nonatomic, strong)   MSClient *client;
@property (nonatomic, copy)     QSBusyUpdateBlock busyUpdate;


+ (MyMemberService *)defaultService;

- (void)refreshDataOnSuccess:(QSCompletionBlock)completion;

- (void)handleRequest:(NSURLRequest *)request
                 next:(MSFilterNextBlock)next
             response:(MSFilterResponseBlock)response;

@end


MyMemberService.m
//
//  MyMemberService.m
//  myAppMobileService
//
//  Created by Weerachai on 7/21/56 BE.
//  Copyright (c) 2556 Weerachai. All rights reserved.
//

#import "MyMemberService.h"
#import <WindowsAzureMobileServices/WindowsAzureMobileServices.h>


@interface MyMemberService() <MSFilter>

@property (nonatomic, strong)   MSTable *table;
@property (nonatomic)           NSInteger busyCount;

@end

@implementation MyMemberService

@synthesize items;


+ (MyMemberService *)defaultService
{
    // Create a singleton instance of MyMemberService
    static MyMemberService* service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        service = [[MyMemberService alloc] init];
    });
    
    return service;
}

-(MyMemberService *)init
{
    self = [super init];
    
    if (self)
    {
        // Initialize the Mobile Service client with your URL and key
        MSClient *client = [MSClient clientWithApplicationURLString:@"https://thaicreate.azure-mobile.net/"
        applicationKey:@"uJPAhkAkcTBuTyCNxaOSnDKFzkoYqB49"];
        
        // Add a Mobile Service filter to enable the busy indicator
        self.client = [client clientWithFilter:self];
        
        // Create an MSTable instance to allow us to work with the TodoItem table
        self.table = [_client tableWithName:@"MyMember"];
        
        self.items = [[NSMutableArray alloc] init];
        self.busyCount = 0;
    }
    
    return self;
}

- (void)refreshDataOnSuccess:(QSCompletionBlock)completion
{
    // Query the TodoItem table and update the items property with the results from the service
    [self.table readWithCompletion:^(NSArray *results, NSInteger totalCount, NSError *error)
     {
         [self logErrorIfNotNil:error];
         
         items = [results mutableCopy];
         
         // Let the caller know that we finished
         completion();
     }];
    
}

- (void)busy:(BOOL)busy
{
    // assumes always executes on UI thread
    if (busy)
    {
        if (self.busyCount == 0 && self.busyUpdate != nil)
        {
            self.busyUpdate(YES);
        }
        self.busyCount ++;
    }
    else
    { 
        if (self.busyCount == 1 && self.busyUpdate != nil)
        {
            self.busyUpdate(FALSE);
        }
        self.busyCount--;
    }
}

- (void)logErrorIfNotNil:(NSError *) error
{
    if (error)
    {
        NSLog(@"ERROR %@", error);
    }
}


- (void)handleRequest:(NSURLRequest *)request
                 next:(MSFilterNextBlock)next
             response:(MSFilterResponseBlock)response
{
    // A wrapped response block that decrements the busy counter
    MSFilterResponseBlock wrappedResponse = ^(NSHTTPURLResponse *innerResponse, NSData *data, NSError *error)
    {
        [self busy:NO];
        response(innerResponse, data, error);
    };
    
    // Increment the busy counter before sending the request
    [self busy:YES];
    next(request, wrappedResponse);
}

@end


Download Code ทั้งหมดได้จากท้ายของบทความ

บทความถัดไปที่แนะนำให้อ่าน









บทความที่เกี่ยวข้อง


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-06-15 19:51:04 / 2017-03-24 11:50:42
  Download : Download  ตอนที่ 8 : การทำ Authentication in Azure Mobile Services ด้วย iOS / iPhone
 Sponsored Links / Related

 
ตอนที่ 1 : รู้จัก iOS กับ Mobile Services บน Windows Azure คืออะไร
Rating :

 
ตอนที่ 2 : เตรียมความพร้อมของ iOS / iPhone ก่อนที่จะเขียนบน Mobile Services
Rating :

 
ตอนที่ 3 : การสร้าง iOS / iPhone กับ Mobile Services และการเรียกใช้งานแบบง่าย ๆ
Rating :

 
ตอนที่ 4 : สร้าง Project iOS / iPhone และการเชื่อมต่อกับ Library ของ Mobile Services
Rating :

 
ตอนที่ 5 : iOS / iPhone สร้างตาราง Table บน Mobile Services และการ Insert ข้อมูล
Rating :

 
ตอนที่ 6 : iOS / iPhone อ่าน Data จาก Table ของ Mobile Service และแสดงผลบน App
Rating :

 
ตอนที่ 7 : อ่านข้อมูล Mobile Services แบบมีเงื่อนไข Where และแสดงผล iOS / iPhone
Rating :

 
ตอนที่ 9 : การทำ Push Notifications in Mobile Services ด้วย iOS / iPhone
Rating :

 
ตอนที่ 10 : การทำ Validate และ Modify data in Mobile Services บน iOS / iPhone
Rating :

 
ตอนที่ 11 : การทำ Refine Mobile Services queries with paging บน iOS / iPhone
Rating :

 
ตอนที่ 12 : การเขียน Scripts to authorize users in Mobile Services บน iOS / iPhone
Rating :

 
ตอนที่ 13 : Show Case 1 : Register Form (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 14 : Show Case 2 : Login User Password (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 15 : Show Case 3 : Update Data (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 16 : Show Case 4 : Delete Data (iOS / iPhone and Mobile Services)
Rating :

 
ตอนที่ 17 : บทความอื่น ๆ เกี่ยวกับ iOS / iPhone Mobile Services บน Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่