Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无internet连接xcode警报视图_Xcode_Ios6 - Fatal编程技术网

无internet连接xcode警报视图

无internet连接xcode警报视图,xcode,ios6,Xcode,Ios6,我是应用程序开发新手,制作了一个应用程序,它使用多个ViewController和UIWebView。 我想在iphone未连接到internet时添加alertview。 我尝试过实现不同的代码,但是alertview不会弹出 这是我的密码 // // ViewControllersocialgo.h // GO!SocialMedia // // Created by Adam Rais on 28/11/2012. // Copyright (c) 2012 SocialGO. A

我是应用程序开发新手,制作了一个应用程序,它使用多个ViewController和UIWebView。 我想在iphone未连接到internet时添加alertview。 我尝试过实现不同的代码,但是alertview不会弹出

这是我的密码

//
//  ViewControllersocialgo.h
//  GO!SocialMedia
//
//  Created by Adam Rais on 28/11/2012.
//  Copyright (c) 2012 SocialGO. All rights reserved.
//

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

@interface ViewControllersocialgo : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *socialgo;

@end
//
//ViewControllersocialgo.h
//走!社会媒体
//
//由Adam Rais于2012年11月28日创建。
//版权所有(c)2012 SocialGO。版权所有。
//
#进口
#导入“可达性.h”
@界面ViewControllersocialgo:UIViewController
@属性(强、非原子)IBUIWebView*socialgo;
@结束
和ViewControllerpractical.m

//
//  ViewControllerpractical.m
//  GO!SocialMedia
//
//  Created by Adam Rais on 28/11/2012.
//  Copyright (c) 2012 SocialGO. All rights reserved.
//

#import "ViewControllersocialgo.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>





@interface ViewControllersocialgo ()

@end

@implementation ViewControllersocialgo


-(BOOL) isInternetAvailable {
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability =        SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct     sockaddr*)&zeroAddress);
if(reachability != NULL) {
    //NetworkStatus retVal = NotReachable;
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
        if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
        {
            // if target host is not reachable
            return NO;
        }

        if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
        {
            // if target host is reachable and no connection is required
            //  then we'll assume (for now) that your on Wi-Fi
            return YES;
        }


        if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
             (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
        {
            // ... and the connection is on-demand (or on-traffic) if the
            //     calling application is using the CFSocketStream or higher APIs

            if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
            {
                // ... and no [user] intervention is needed
                return YES;
            }
        }

        if ((flags & kSCNetworkReachabilityFlagsIsWWAN) ==     kSCNetworkReachabilityFlagsIsWWAN)
        {
            // ... but WWAN connections are OK if the calling application
            //     is using the CFNetwork (CFSocketStream?) APIs.
            return YES;
        }
    }
}

return NO;

}




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[_socialgo setDelegate:self];
NSString *fullURL=@"http://adam182testing.comoj.com/socialgo.html";
NSURL *url=[NSURL URLWithString:fullURL];
NSURLRequest *requestOBJ=[NSURLRequest requestWithURL:url];
[_socialgo loadRequest:requestOBJ];
}

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

@end
//
//  ViewControllerpractical.m
//  GO!SocialMedia
//
//  Created by Adam Rais on 28/11/2012.
//  Copyright (c) 2012 SocialGO. All rights reserved.
//

#import "ViewControllerpractical.h"

@interface ViewControllerpractical ()

@end

@implementation ViewControllerpractical

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Can't connect. Please check your internet Connection"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
[_practical setDelegate:self];
NSString *fullURL=@"http://adam182testing.comoj.com/practical.html";
NSURL *url=[NSURL URLWithString:fullURL];
NSURLRequest *requestOBJ=[NSURLRequest requestWithURL:url];
[_practical loadRequest:requestOBJ];
}

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

@end
//
//ViewControllerpractical.m
//走!社会媒体
//
//由Adam Rais于2012年11月28日创建。
//版权所有(c)2012 SocialGO。版权所有。
//
#导入“ViewControllersocialgo.h”
#进口
#进口
#进口
@界面ViewControllersocialgo()
@结束
@实现ViewControllersocialgo
-(BOOL)可跨网络访问{
零地址中的结构sockaddr_;
bzero(&zeroAddress,sizeof(zeroAddress));
zeroAddress.sin_len=sizeof(zeroAddress);
zeroAddress.sin_family=AF_INET;
SCNetworkReachabilityRef reachability=SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault,(const struct sockaddr*)和zeroAddress);
if(可达性!=NULL){
//NetworkStatus retVal=不可访问;
SCNetworkReachabilityFlags标志;
if(SCNetworkReachabilityGetFlags(可达性和标志)){
if((标志&kSCNetworkReachabilityFlagsReachable)==0)
{
//如果无法访问目标主机
返回否;
}
if((标志和kSCNetworkReachabilityFlagsConnectionRequired)=0)
{
//如果目标主机可访问且不需要连接
//那么我们就假设(目前)你的手机是无线上网的
返回YES;
}
如果(((标志和kSCNetworkReachabilityFlagsConnectionOnDemand)!=0)||
(标志和KSC网络可达性FlagsConnectionContraffic)!=0)
{
//…并且如果
//调用应用程序正在使用CFSocketStream或更高的API
if((标志和KSCNetworkReachabilityFlagsIntervationRequired)=0)
{
//…无需[用户]干预
返回YES;
}
}
if((标志&kSCNetworkReachabilityFlagsIsWWAN)=kSCNetworkReachabilityFlagsIsWWAN)
{
//…但是如果调用应用程序
//正在使用CFNetwork(CFSocketStream?)API。
返回YES;
}
}
}
返回否;
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
[[u socialgo setDelegate:self];
NSString*fullURL=@”http://adam182testing.comoj.com/socialgo.html";
NSURL*url=[NSURL URLWithString:fullURL];
NSURLRequest*requestOBJ=[nsurlRequestRequestWithURL:url];
[_socialgoloadrequest:requestOBJ];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束

谢谢

您的视图控制器必须实现
UIWebViewDelegate
,请尝试以下代码

ViewControllerpractical.h

//
//  ViewControllerpractical.h
//  GO!SocialMedia
//
//  Created by Adam Rais on 28/11/2012.
//  Copyright (c) 2012 SocialGO. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewControllerpractical : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *practical;

@end
无论如何,正如Eli Ganem所说,使用可达性更好

我在项目中使用此功能:

-(BOOL) isInternetAvailable {
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);
    if(reachability != NULL) {
        //NetworkStatus retVal = NotReachable;
        SCNetworkReachabilityFlags flags;
        if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
            if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
            {
                // if target host is not reachable
                return NO;
            }

            if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
            {
                // if target host is reachable and no connection is required
                //  then we'll assume (for now) that your on Wi-Fi
                return YES;
            }


            if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
                 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
            {
                // ... and the connection is on-demand (or on-traffic) if the
                //     calling application is using the CFSocketStream or higher APIs

                if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
                {
                    // ... and no [user] intervention is needed
                    return YES;
                }
            }

            if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
            {
                // ... but WWAN connections are OK if the calling application
                //     is using the CFNetwork (CFSocketStream?) APIs.
                return YES;
            }
        }
    }

    return NO;

}

您的视图控制器必须实现
UIWebViewDelegate
,请尝试以下代码

ViewControllerpractical.h

//
//  ViewControllerpractical.h
//  GO!SocialMedia
//
//  Created by Adam Rais on 28/11/2012.
//  Copyright (c) 2012 SocialGO. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewControllerpractical : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *practical;

@end
无论如何,正如Eli Ganem所说,使用可达性更好

我在项目中使用此功能:

-(BOOL) isInternetAvailable {
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);
    if(reachability != NULL) {
        //NetworkStatus retVal = NotReachable;
        SCNetworkReachabilityFlags flags;
        if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
            if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
            {
                // if target host is not reachable
                return NO;
            }

            if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
            {
                // if target host is reachable and no connection is required
                //  then we'll assume (for now) that your on Wi-Fi
                return YES;
            }


            if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
                 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
            {
                // ... and the connection is on-demand (or on-traffic) if the
                //     calling application is using the CFSocketStream or higher APIs

                if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
                {
                    // ... and no [user] intervention is needed
                    return YES;
                }
            }

            if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
            {
                // ... but WWAN connections are OK if the calling application
                //     is using the CFNetwork (CFSocketStream?) APIs.
                return YES;
            }
        }
    }

    return NO;

}

不要等到出现错误才告诉用户他没有互联网连接。使用苹果自己的方法来检测连通性——或者使用ARC。如果用户失去连接,它会立即告诉您。

不要等到出现错误才告诉用户他没有internet连接。使用苹果自己的方法来检测连通性——或者使用ARC。如果用户失去连接,它会立即通知您。

出现错误,请尝试新的。它正在工作,我已经测试过了非常感谢你,让UIAlertView正常工作。下一步将尝试使用Apple可达性。zeroAddress中的行结构sockaddr_存在问题;它说,“变量有不完整的类型'struct sockaddr#u in'”啊,你需要这个#import#import#import(对于这个,你必须将SystemConfiguration.framework添加到你的项目中)我已经添加了可访问性代码,但是当在没有互联网连接的情况下运行应用程序时,没有警告告诉用户没有互联网连接?我已经更新了上面的代码@JCESAR有个错误,试试新的。它正在工作,我已经测试过了非常感谢你,让UIAlertView正常工作。下一步将尝试使用Apple可达性。zeroAddress中的行结构sockaddr_存在问题;它说,“变量有不完整的类型'struct sockaddr#u in'”啊,你需要这个#import#import#import(对于这个,你必须将SystemConfiguration.framework添加到你的项目中)我已经添加了可访问性代码,但是当在没有互联网连接的情况下运行应用程序时,没有警告告诉用户没有互联网连接?我已经更新了上面的代码@我非常感谢你。我将学习如何在我的项目中实现这一点。如果你觉得我的答案有用,请考虑接受它作为正确的答案。非常感谢。我将学习如何在我的项目中实现这一点。如果你觉得我的答案有用,请考虑接受它作为正确的答案。