I am updating an iOS app to be a good boy and clean out some deprecated methods. I have been using NSString initWithContentsOfURL and I am trying to implement the new version initWithContentsOfURL:usedEncoding:error:.
I have used the following in my view controller:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:#"http://myurl.com/directory/file.txt"];
NSError *error;
NSStringEncoding *encoding = NULL;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:encoding
error:NULL];
NSLog(#"string:%#", tryString);
[super viewDidLoad];
}
But I keep getting a thread break at the error:NULL point. I have tried error:nil and using a pointer to the predefined error object as nil or NULL but same thing happens. Has anyone any ideas or tips.
NSError *error = nil;
NSStringEncoding encoding = 0;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Both the error and encoding should be pass like this:
NSError *error = nil;
NSStringEncoding encoding = nil;
NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Related
I have used the following code to initialise an AVAudioPlayer object with NSData, which in turn has the data of an mp3 in my main bundle.
NSError *error;
NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio];
bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error];
bgAudioPlayer.delegate = self;
I have declared bgAudioPlayer in the .h file. But when I checked by setting breakpoint, I found that after this line bgAudioPlayer = [[AVAudioPlayer alloc] initWithData:bgAudioData error:&error]; the bgAudioPlayer is still shown to be nil. But the filePathForBgAudio and bgAudioData are initialised correctly and has data. just the bgAudioPlayer is nil. What is the problem in the above code? How to rectify this?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"bubble"
ofType:#"mp3"]];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else
{
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
}
}
- (IBAction)playsound:(id)sender
{
[_audioPlayer play];
}
NSError *error;
NSURL *filePathForBgAudio1 =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"]];
//NSString *filePathForBgAudio = [[NSBundle mainBundle] pathForResource:#"beebuzz10" ofType:#"mp3"];
// NSData *bgAudioData = [NSData dataWithContentsOfFile:filePathForBgAudio1];
bgAudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:filePathForBgAudio1 error:&error];
I am getting response from web service, the response is based on comma separated URl
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"ResponseData: %#", str);
And its output is like
ResponseData: http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-00-12-46.jpg,http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-01-12-32.jpg
I want to pass the response to my Images Gallery Which takes URL like below format
networkImages = [[NSArray alloc] initWithObjects:#"http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-00-12-46.jpg",#"http://www.digitalnet.com/androidapi/images/post_images/img_2013-12-09-01-12-32.jpg",nil];
How can I format my response to #"URL",#"URL" so that I can store it into some variable e,g;abc and pass that to
networkImages = [[NSArray alloc] initWithObjects:abc,nil];
NSString has a method componentsSeparatedByString: which does exactly this.
networkImages = [str componentsSeparatedByString:#","];
I think you have two ways :
First way; (that i have already using in my news App)
* You must use JSON result.
* If you want you can use SBJsonParser and SDWebImage
NSError *myError = nil;
NSString *link = [NSString stringWithFormat:#"http://www.myexamplenews.com/mobil-app.php?q=imageList&id=%i",galleryId];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:0 timeoutInterval:5];
NSURLResponse *response=nil;
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&myError];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:nil];
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
if (myError == nil)
{
NSArray *jsonGalleryDetail = [jsonParser objectWithString:jsonString];
jsonParser = nil;
GalleryDetailView *view = [[GalleryDetailView alloc] initWithFrame:[[UIScreen mainScreen] andImageList: jsonGalleryDetail];
[self.navigationController pushViewController:GalleryDetailView animated:YES];
}
GalleryDetailView.h
#interface GalleryDetailView : UIView
{
}
- (id)initWithFrame:(CGRect)frame andImageList:(NSArray *)myList;
#end
GalleryDetailView.m
#implementation GalleryDetailView
- (id)initWithFrame:(CGRect)frame andImageList:(NSArray *) myList;
{
self = [super initWithFrame:frame];
if (self)
{
//Your code here
for (NSString *str in myList)
{
NSURL *imageURL = [NSURL URLWithString:str];
//This is SDWebImage extension.
UIImage *image = [UIImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
}
return self;
}
#end
Second way ;
NSArray *arr = [ResponseData componentsSeparatedByString:#","];
GalleryDetailView *view = [[GalleryDetailView alloc] initWithFrame:[[UIScreen mainScreen] andImageList: arr];
[self.navigationController pushViewController:GalleryDetailView animated:YES];
I prefer to use JSON data. It is up to you. I hope this information will be helpful for you.
I need to get contents of an XML file which is hosted on web, and parse it.
I decided to use TouchXML for parsing process. However, I cannot get contents of the file since it is encoded with ISO-8859-9
XML File Url: http://rss.haberler.com/mobil/sondakika2.asp?kategori=manset
I have tried 2 different approachs.
1) Getting contents of url into NSString:
NSString *url = #"http://rss.haberler.com/mobil/sondakika2.asp?kategori=manset";
NSError *error = nil;
NSStringEncoding encoding;
NSString *xmlString = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:url] usedEncoding:&encoding error:&error];
xmlString becomes null, and error description says:
The operation couldn’t be completed. (Cocoa error 261.)
Instead of usedEncoding, I also tried specifying encoding explicitly, from UTF-8 to NSISOLatin1StringEncoding and NSISOLatin2StringEncoding (unfortunately, I could not find NSISOLatin9StringEncoding).
2) I also tried to load xml into NSData.
NSError *error = nil;
NSData *XMLData = nil;
XMLData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url] options:0 error:&error];
When I constructed XML Parser, specific characters are unknown and application terminates when I get string values.
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:XMLData options:0 error:nil];
NSArray *nodes = [doc nodesForXPath:#"//item" error:nil];
for (CXMLElement *node in nodes) {
for(int counter = 0; counter < [xmlElement childCount]; counter++) {
CXMLNode * child = [xmlElement childAtIndex:counter];
NSString *childName = child.name;
NSString * childValue = [child stringValue];
}
}
Getting stringValue of child terminates application with SIGABRT.
How can I fix the problem?
From the TouchXML CXMLDocument.m file, this is what initWithData: looks like
- (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError
{
return [self initWithData:inData encoding:NSUTF8StringEncoding options:inOptions error:outError];
}
What you could try doing is, in this file, replace NSUTF8StringEncoding with NSASCIIStringEncoding or whatever encoding it uses. That may fix it.
In my app I do an xmlparser but I have a problem during parser because in a tag I have a special letter "è" and it read it as "\U00e8"; and it close a tag of my xml...and I have an error in my parsing...
I set my path in utf-8 in this way
NSString *path = #"emaple.com";
NSError *error;
NSURL *url = [NSURL URLWithString:path];
NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
affayfeed = [[NSMutableArray alloc]init];
parser = [[NSXMLParser alloc] initWithData:data];
but I have the same problem...why?
I get the data from an XML file and I am storing it in NSData object. I want to convert that NSData into an NSDictionary and store that data in a plist.
My code is as follows:
NSURL *url = [NSURL URLWithString:#"http://www.fubar.com/sample.xml"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(#"%#", data);
To convert the data, I am using:
- (NSDictionary *)downloadPlist:(NSString *)url {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err];
if (!err) {
NSString *errorDescription = nil;
NSPropertyListFormat format;
NSDictionary *samplePlist = [NSPropertyListSerialization propertyListFromData:responseData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&errorDescription];
if (!errorDescription)
return samplePlist;
[errorDescription release];
}
return nil;
}
Can anyone please tell me how to do that?
or this:
NSString* dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJSON *jsonParser = [SBJSON new];
NSDictionary* result = (NSDictionary*)[jsonParser objectWithString:dataStr error:nil];
[jsonParser release];
[dataStr release];
Try this code:
NSString *newStr1 = [[NSString alloc] initWithData:theData1 encoding:NSUTF8StringEncoding];
NSString *newStr2 = [[NSString alloc] initWithData:theData2 encoding:NSUTF8StringEncoding];
NSString *newStr3 = [[NSString alloc] initWithData:theData3 encoding:NSUTF8StringEncoding];
NSArray *keys = [NSArray arrayWithObjects:#"key1", #"key2", #"key3", nil];
NSArray *objects = [NSArray arrayWithObjects:newStr1 , newStr2 , newStr3 , nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
for (id key in dictionary) {
NSLog(#"key: %#, value: %#", key, [dictionary objectForKey:key]);
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"Login" ofType:#"plist"];
[dictionary writeToFile:path atomically:YES];
//here Login is the plist name.
Happy coding