没有互联网连接 xcode 警报视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13650211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No internet connection xcode alert view
提问by Adam
I'm new at app developing and have made an app that uses several viewcontrollers with uiwebviews. I want to add an alertview when the iphone is not connected to the internet. I have tried implementing different code, but the alertview wont pop up.
我是应用程序开发的新手,并制作了一个应用程序,该应用程序使用多个带有 uiwebviews 的视图控制器。我想在 iphone 未连接到互联网时添加一个警报视图。我尝试实现不同的代码,但不会弹出警报视图。
Heres my code.
继承人我的代码。
//
// 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
and ViewControllerpractical.m
和 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
Thanks
谢谢
回答by Eli Ganem
Don't wait for an error to tell the user he has no internet connection. Use Apple's own method to detect connectivity - Apple sample projector this versionthat uses ARC. It will tell you instantly if the user loses connection.
不要等待错误告诉用户他没有互联网连接。使用 Apple 自己的方法来检测连接性 - Apple 示例项目或使用 ARC 的此版本。如果用户失去连接,它会立即告诉您。
回答by jcesarmobile
your view controller have to implement the UIWebViewDelegate
, try this code
你的视图控制器必须实现UIWebViewDelegate
,试试这个代码
ViewControllerpractical.h
视图控制器实用.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
ViewControllerpractical.m
ViewControllerpractical.m
//
// 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
anyways, as Eli Ganem said, is better using reachability.
无论如何,正如 Eli Ganem 所说,使用可达性更好。
I use this function in my projects :
我在我的项目中使用这个功能:
-(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;
}