How to check for internet connection before showing an option to view an incentivized app?

瘦欲@ 提交于 2020-01-07 03:52:07

问题


For my current sprite kit game that is coded in OBJECTIVE - C, I want to check for internet connection before providing the user with an option to view an incentivized ad. How would I do this? I only want the option to appear if the user is connected to the internet.


回答1:


Step-1) Download the Reachability project from GitHub Project from here.

Step-2) Add Reachability.h and Reachability.m files in your project from github project.

Step-3) In your viewController.m -> #import "Reachability.h" add below code in your viewController.m

#define KWebURL  @"http://yourWebservicesbaseURl_or_hostName"

-(BOOL)isConnected{
     Reachability *aReachability = [Reachability reachabilityWithHostName:your_Server_MainURL];

    NetworkStatus netStatus = [aReachability currentReachabilityStatus];

    if(netStatus==0)
    {
        return NO;
    }
    else if(netStatus==1)
    {
        return YES;
    }
    else if(netStatus==2)
    {
    return YES;
    }
    else
    {
        return YES;
    }
}

Step-4) Uses: now check conectivity using below code in your ViewDidload or where you have to impliment.

if ([self isConnected]) {
    // your internet is connected.
}else{
    // your internet is not connected.
}

-- OR --

if you have to make one global Object Class to use it globally from anywhere then floow from Step-3) like....

Step-3.0) First make New NSObject Class i.e Webservice.h and Webservice.m

Step-3.1) In your Webservice.h -->

#import <Foundation/Foundation.h>
#import "Reachability.h"

@interface Webservice : NSObject

+(BOOL)isConnected;

@end

Step-3.2) In your Webservice.m -->

#import "Webservice.h"
#import "Reachability.h"

@implementation Webservice
+(BOOL)isConnected
{
    Reachability *aReachability = [Reachability reachabilityWithHostName:your_Server_MainURL];
    NetworkStatus netStatus = [aReachability currentReachabilityStatus];

    if(netStatus==0)
    {
        return NO;
    }
    else if(netStatus==1)
    {
    return YES;
    } 
    else if(netStatus==2)
    {
        return YES;
    }
    else
    {
        return YES;
    }
}

@end

Step-4) Uses: now check conectivity using below code in your any viewControllers or anywhere you have to impliment.

if ([Webservice isConnected]) {
    // your internet is connected.
}else{
    // your internet is not connected.
}


来源:https://stackoverflow.com/questions/34960301/how-to-check-for-internet-connection-before-showing-an-option-to-view-an-incenti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!