Ashok Domadiya
Ashok Domadiya

Reputation: 1122

How to open URL in WebView in iOS?

I have tried to open below url in WebView

http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com

But unable to make object of NSURL.

Upvotes: 0

Views: 8034

Answers (4)

dalegege
dalegege

Reputation: 21

where's your code? did u use a webView?

I used this method :

[_webView loadRequest:[NSURLRequest requestWithURL:url]];

Upvotes: 2

Nirav D
Nirav D

Reputation: 72460

Try to encode your URL using addingPercentEncoding instead of stringByAddingPercentEscapesUsingEncoding because it is deprecated.

In Swift:

  • For Swift 3:

    let strUrl = "http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com"
    let encodedUrl = strUrl.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    
  • For Swift 2.3 or lower:

     let encodedUrl = strUrl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
    

In Objective-C

NSString *strUrl = @"http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com";
NSString *encodedUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

Upvotes: 1

Maulik Pandya
Maulik Pandya

Reputation: 2230

For swift :

var url : NSString = "http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com" 
var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
var searchURL : NSURL = NSURL(string: urlStr)! 
    webviewInstance.loadRequest(NSURLRequest(URL: searchURL))

For objective-c

[webviewInstance loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSURL URLWithString:[your_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]];

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

Step-1

initially add NSAppTransportSecurity in your .plist, see this

step-2

call the url like

NSString *urlAddress =  @"http://1-dot-smartrefill-968.appspot.com/#/#mfucci@gmail_com"; 
[self.yourwebviewName loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]];

Upvotes: 1

Related Questions