简体   繁体   中英

iPhone SDK: NSURL string errors/ % sign confrontation

in my piece of coding to get a url to reach a website, I encounter a warning. The app still loads but the url does not load the web view. This is my code:

   NSString *strWebsiteUlr = [NSString stringWithFormat:@"https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1359826314&rver=6.1.6195.0&wp=MBI_SSL&wreply=https:%2F%2Flogin.secure.co1.msn.com%2Fwlsignin.aspx%3Fru%3Dhttp%253a%252f%252"];
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[eMail loadRequest:requestObj];

With this code, I receive a warning saying: More % conversions than data arguments. What I'm guessing it's saying is that I have percentage signs that don't correspond ot an argument; however, this is part of the url and when I try to load this the webview stays blank. Any idea on how to fix this?

You don't need to use stringWithFormat: in this case because you aren't trying to insert any values in the string. Change your code to the following:

NSString *strWebsiteUlr = @"https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1359826314&rver=6.1.6195.0&wp=MBI_SSL&wreply=https:%2F%2Flogin.secure.co1.msn.com%2Fwlsignin.aspx%3Fru%3Dhttp%253a%252f%252";
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[eMail loadRequest:requestObj];

You could also skip the strWebsiteUlr variable entirely and just do:

NSURL *url = [NSURL URLWithString:@"https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1359826314&rver=6.1.6195.0&wp=MBI_SSL&wreply=https:%2F%2Flogin.secure.co1.msn.com%2Fwlsignin.aspx%3Fru%3Dhttp%253a%252f%252"];

If you did want to insert values in the string using stringWithFormat: you can escape the percent signs with another percent, eg %% .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM