Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 7 UIWebView doesn't load url

I use UIWebView in my app and it all worked fine in Xcode4,5,6 simulator. but not for Xcode 7 simulator, I don't know why, there is no warning or error in simulator, and the screen is just showing blank page. Please help me. Thanks.

#import "IndexViewController.h"

@interface IndexViewController ()

@end

@implementation IndexViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *urlString = nil;
    NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
    if ([languageCode isEqualToString:@"zh-Hans"]) {
        urlString = @"http://www.originoftime.net/index-cn";
    }else if ([languageCode isEqualToString:@"zh-Hant"]) {
        urlString = @"http://www.originoftime.net/index-cn";
    }else{
        urlString = @"http://www.originoftime.net/index-en";
    }
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

    NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];

    [_Index loadRequest:urlrequest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
like image 443
wang hai peng Avatar asked Sep 22 '15 14:09

wang hai peng


2 Answers

Xcode 7 with iOS9 now force you to not use HTTP call, but HTTPS ones.

this is a security point enhanced in AppTransportSecurity.

Try this:

  • Go to your info.plist
  • Add a dictionary called NSAppTransportSecurity
  • Add a boolean attribute to this one, called NSAllowsArbitraryLoads
  • Pass it to TRUE

Reload your app.

I advice you that if Apple want to block HTTP (unsecured) calls is for a good reason. http://www.originoftime.net/index-cn has a HTTPS one but the server certificate seems to be self-signed.

Let me know if this workaround work for you

Cheers from france

like image 110
jlngdt Avatar answered Nov 01 '22 13:11

jlngdt


Do you implement the web view delegate methods? In particular:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

If a load fails, that will tell you what the issue is.

It could well be the error related to the new security model which is being enforced for network access. You can override this new behavior by adding the following into your Info.plist file. Just edit the XML and paste this in:

<key>NSAppTransportSecurity</key>  
<dict> 
   <key>NSAllowsArbitraryLoads</key><true/>  
</dict>  

The changes are summarized here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

like image 45
Rory McKinnel Avatar answered Nov 01 '22 14:11

Rory McKinnel