1. Documents
  2. iOS SDK

Using the LINE SDK for iOS

You can integrate your iOS apps with LINE using the LINE SDK for iOS. This page explains how to embed the LINE SDK for iOS in your iOS apps.

Requirements

The following are required to use the LINE SDK for iOS.

Downloading the LINE SDK

To download the LINE SDK for iOS from the Channel Console, follow the steps below.

Download the LINE SDK for iOS

step1

Select your Channel from the list on the left.

capture_011

step2

Click Download SDK.

Download SDK

step3

Click the link of the SDK file and save the file to any directory.

iOS SDK

Note: We highly recommend using the latest version of the SDK. The previous versions of the SDK have been deprecated.

LINE starter application

The LINE starter application provides you with all the elements you need to start integrating the LINE SDK into your app.

Download the LINE starter application from the following link.

Linking the required frameworks for your project

  1. Go to the Build Phases section in your XCode project settings and add LineSDK.framework to Link Binary With Libraries by pressing the “+” button and then pressing Add Other.
    iOS SDK frameworks
  2. Add the following frameworks in the same way:
    • CoreTelephony.framework
    • Security.framework

Required build settings

Add “-ObjC” to Build Settings > Other Linker Flags

Enable Keychain Sharing

The LINE iOS SDK uses Keychain to store the user’s authentication credentials. As a result, your application must enable Keychain Sharing to use the SDK. You can do this from Capabilities > Keychain Sharing in your XCode Project settings.

Keychain Sharing

In addition, you must also:

  • Add the Keychain Sharing entitlement to your entitlements file.
  • Add the Keychain Sharing feature to your App ID.

Info.plist settings

Set your Channel ID in your application’s Info.plist as follows. Make sure you change “1234567890” to the correct Channel ID for your Channel.


<key>LineSDKConfig</key>
<dict>
    <key>ChannelID</key>
    <string>1234567890</string>
</dict>      

Add the required settings for app-to-app login to Info.plist.


<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>line3rdp.$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>lineauth</string>
    <string>line3rdp.$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Handling app-to-app login in AppDelegate.m

If a user logs into LINE using the LINE app, the application gives control to the LINE app that is installed on the device to authenticate the user. After authentication is complete, the LINE app returns control to your application and passes the authentication result. You must add the following code to AppDelegate.m to handle the return from the LINE app:


- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    return [[LineSDKLogin sharedInstance] handleOpenURL:url];
}

Import the LineSDK header file

Do not forget to import LineSDK.h in all files that the LINE SDK is used in.


#import <LineSDK/LineSDK.h>

Logging in using the LINE app (app-to-app login)

Login is started by calling the startLogin method. The application behaves as follows when startLogin is called:

  • If the LINE app is installed on the device, the application will perform app-to-app login using the LINE app. This allows the application to use the credentials that the LINE app is using to log the user in without prompting them for their login credentials.
  • If the LINE app is not installed on the device, the user is taken to the LINE Login screen in their browser and prompted for their LINE Login credentials (email address and password).

Here is an example of how to call startLogin


[[LineSDKLogin sharedInstance] startLogin];

Logging in using Web Login

If you would like to force the SDK to use Web Login, you can call the startWebLoginWithSafariViewController method in place of the startLogin method. This method takes a parameter called useSFViewControllerIfAvailable. If you set this parameter to YES, the application will open the LINE Login screen in a Safari Web View within the application instead of opening it in a browser:


[[LineSDKLogin sharedInstance] startWebLoginWithSafariViewController:YES];

Note: It is not necessary to include the SafariServices framework to use LINE Login in a Safari view controller.

If you set the useSFViewControllerIfAvailable parameter to NO, the LINE Login screen will be opened in an external browser. Note that this is the same behavior that occurs if startLogin is called and the LINE app is not installed:


[[LineSDKLogin sharedInstance] startWebLoginWithSafariViewController:NO];

Handling the login results using LineSDKLoginDelegate

After logging in using the desired login method, your application will need to listen for and handle the results of the login process. You can do this using LineSDKLoginDelegate.

To handle the login results, set the delegate as the view controller that you would like to receive the login results. You can do this in the view’s viewDidLoad method.


- (void)viewDidLoad {
    [super viewDidLoad];
 
 
    ...
     
    // Set the LINE Login Delegate
    [LineSDKLogin sharedInstance].delegate = self;
}

You can then handle the result using LineSDKLoginDelegate’s didLogin method:


#pragma mark LineSDKLoginDelegate
 
- (void)didLogin:(LineSDKLogin *)login
      credential:(LineSDKCredential *)credential
         profile:(LineSDKProfile *)profile
           error:(NSError *)error
{
    if (error) {
  
        // Login failed with an error. You can use the error parameter to help determine what the problem was.
    }
    else {
  
        // Login has succeeded. You can get the user's access token and profile information.
    }
}

The didLogin method has 3 parameters that you can get login information from:

Parameter Description
credential Contains the user’s access token.
profile Contains the user’s profile information.
error Contains login error information. This is nil if the login succeeds.

Getting the user’s access token

You can extract the user’s access token from the didLogin method’s credential parameter. Here is an example of extracting the access token and storing it in an NSString.


#pragma mark LineSDKLoginDelegate
 
- (void)didLogin:(LineSDKLogin *)login
      credential:(LineSDKCredential *)credential
         profile:(LineSDKProfile *)profile
           error:(NSError *)error
{
    if (error) {
        ...
    }
    else {
 
        NSString * accessToken = credential.accessToken.accessToken;
    }
}

After extracting the access token, you may send it to the server so that it can make server-side API calls. If you choose to send the token to the server in this way, we recommend hashing the access token and sending the hash over SSL to the server.

Getting the user’s profile information

You can extract the user’s profile information from the didLogin method’s profile parameter. Here is an example of extracting the user’s profile information and storing it in NSStrings.


#pragma mark LineSDKLoginDelegate
 
- (void)didLogin:(LineSDKLogin *)login
      credential:(LineSDKCredential *)credential
         profile:(LineSDKProfile *)profile
           error:(NSError *)error
{
    if (error) {
        ...
    }
    else {
 
        NSString * userID = profile.userID;
        NSString * displayName = profile.displayName;
        NSString * statusMessage = profile.statusMessage;
        NSURL * pictureURL = profile.pictureURL;
         
        NSString * pictureUrlString;
         
        // If the user does not have a profile picture set, pictureURL will be nil
        if (pictureURL) {
            pictureUrlString = profile.pictureURL.absoluteString;
        }
                 
    }
}

Note: If the user does not have a profile picture set, pictureURL will be nil.

Calling APIs

The APIs can be called through an instance of the LineSDKAPI object. You must initialize the object before using it to call the API:


apiClient = [[LineSDKAPI alloc] initWithConfiguration:[LineSDKConfiguration defaultConfig]];

Log out

– (void)logoutWithCompletion:(void(^)(BOOL success, NSError * _Nullable error))completion;

The logout method invalidates the access token that is currently being used by the SDK. Use this method to add logout functionality to your iOS application.

The success argument will be YES if the logout operation succeeds. If the logout operation fails, you can get information about the failure from the error argument. The error argument is nil if the logout operation succeeds.


[apiClient logoutWithCompletion:^(BOOL success, NSError * _Nullable error){
     
    if (success){
        // Logout Succeeded
    } else {
        // Logout Failed
        NSLog(@"Logout Failed: %@", error.description);
    }
     
}];
Executing the completion block in a different queue

logoutWithCompletion executes its completion block on the main queue. To execute the completion block in a different queue, specify the queue by calling the logoutWithCallbackQueueCompletion method.


[apiClient logoutWithCallbackQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) 
    completion:^(BOOL success, NSError * _Nullable error){
...
}];

Refresh access token

– (void)refreshTokenWithCompletion:(void(^)(LineSDKAccessToken * _Nullable accessToken, NSError * _Nullable error))completion;

The refreshTokenWithCompletion method refreshes the access token that is currently being used by the SDK. This API can be used to refresh the access token for up to 10 days after it expires. If you cannot refresh the access token using the method, you must prompt the user to authenticate again.

If the token refresh succeeds, you can get the new refresh token from the accessToken argument. If the refresh operation fails, you can get more information about the failure from the error argument. The error argument will be nil if the logout operation succeeds.


[apiClient refreshTokenWithCompletion:^(LineSDKAccessToken * _Nullable accessToken, NSError * _Nullable error) {
     
    if (error){
         
        // The token refresh failed.
        NSLog(@"Error occurred when refreshing the access token: %@", error.description);
 
 
    } else {
 
 
        // The token refresh succeeded so we can get the refreshed access token.
        NSString * newAccessToken = accessToken.accessToken;       
         
    }
     
}];

Get current access token

– (nullable LineSDKAccessToken *)currentAccessToken;

This method retrieves the access token that is currently being used by the SDK. The currentAccessToken method returns a LineSDKAccessToken object. You can get it in NSString by using LineSDKAccessToken’s accessToken property as shown below:


LineSDKAccessToken * accessTokenObject = [apiClient currentAccessToken];
NSString * accessTokenString = accessTokenObject.accessToken;

You can also get the current access token by simply using LineSDKAPI’s accessToken object:


NSString * accessToken = apiClient.currentAccessToken.accessToken;

Verify access token

– (void)verifyTokenWithCompletion:(void(^)(LineSDKVerifyResult * _Nullable result, NSError * _Nullable error))completion;

You can check to see if the access token is valid or not using the verifyTokenWithCompletion method. This method returns a LineSDKVerifyResult object that contains the result. You can use the error argument to check if the token is valid. If the error argument is nil, the token is valid. Otherwise the token is invalid, expired, or the verify API failed in some manner. If error is not nil, you can use it to get more information about the failure.


[apiClient verifyTokenWithCompletion:^(LineSDKVerifyResult * _Nullable result, NSError * _Nullable error) {
     
    if (error) {
        // Token is invalid
        NSLog(@"Token is Invalid: %@", error.description);
    } else {
        // Token is Valid
    }
}];

You can also get a list of permissions that are associated with the access token from the result object’s permissions property. The following example demonstrates how to display a list of an access token’s permissions in a dialog.


[apiClient verifyTokenWithCompletion:^(LineSDKVerifyResult * _Nullable result, NSError * _Nullable error) {
     
    if (error) {
        // Token is invalid
        NSLog(@"Token is Invalid: %@", error.description);
    } else {
        NSMutableString * text = [[NSMutableString alloc]initWithString:@"Access Token is Valid and contains the following permissions: "];
 
        for (NSString* permission in result.permissions){
            [dialog appendFormat:@"%@, ", permission];
        }
 
        NSString * label = @"Access Token is Valid";
        UIAlertController *alertController;
        alertController = [UIAlertController alertControllerWithTitle:label message:text preferredStyle:UIAlertControllerStyleAlert];
 
        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];
 
        [self presentViewController:alertController animated:YES completion:nil];
    }
}];
Executing the completion block in a different queue

verifyTokenWithCompletion executes its completion block on the main queue. To execute the completion block in a different queue, specify the queue by calling the verifyTokenWithCallbackQueue method.


[apiClient verifyTokenWithCallbackQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) 
    completion:^(LineSDKVerifyResult * _Nullable result, NSError * _Nullable error) {
...
}];

Get profile information

– (void)getProfileWithCompletion:(void(^)(LineSDKProfile * _Nullable profile, NSError * _Nullable error))completion;

This method retrieves the logged-in user’s profile information. If the API call succeeds, the profile argument will contain the user’s profile information. If the API call fails, the error argument will be a non-nil value. You can use the error argument to get more information about the API failure.

The following is an example of how to save the profile information into NSStrings.


[apiClient getProfileWithCompletion:^(LineSDKProfile * _Nullable profile, NSError * _Nullable error) {
     
    if(error){
         
        NSLog(@"Error getting profile: %@", error.description);
 
    } else {
      
        LineSDKProfile * profileInformation = profile;
 
        NSString * displayName = profileInformation.displayName;
        NSString * userID = profileInformation.userID;
        NSString * statusMessage = profileInformation.statusMessage;
        NSURL * pictureURL = profileInformation.pictureURL;
 
        NSString * pictureUrlString
 
        // If the user does not have a profile picture set, pictureURL will be nil
        if(pictureURL){
            NSString * profilePictureUrlString = profilePictureURL.absoluteString;
        }
 
    }
     
}];

Note: If the user does not have a profile picture set, the profile image URL will be nil.

Executing the completion block in a different queue

getProfileWithCompletion executes its completion block on the main queue. To execute the completion block in a different queue, specify the queue by calling the getProfileWithCallbackQueue method.


[apiClient getProfileWithCallbackQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) 
    completion:^(LineSDKProfile * _Nullable profile, NSError * _Nullable error) {
...
}];