Showing posts with label initWithContentsOfURL example. Show all posts
Showing posts with label initWithContentsOfURL example. Show all posts

Wednesday, June 5, 2013

AVAudioPlayer initWithContentsOfURL example in Objective C (iOS).


AVAudioPlayer initWithContentsOfURL

Initializes and returns an audio player for playing a designated sound file.

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError

Parameters of [AVAudioPlayer initWithContentsOfURL]
url
A URL identifying the sound file to play. The audio data must be in a format supported by Core Audio. For a list of supported formats, see “Using Audio” in Multimedia Programming Guide.
outError
Pass in the address of a nil-initialized NSError object. If an error occurs, upon return the NSError object describes the error. If you do not want error information, pass in NULL.

Return Value of [AVAudioPlayer initWithContentsOfURL]
On success, an initialized AVAudioPlayer object. If nil, the outError parameter contains a code that describes the problem.

AVAudioPlayer initWithContentsOfURL example.
/*sound loop stuff*/
NSString *soundFilePath = [[NSBundle mainBundle]
pathForResource:@"gong" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.loopSoundPlayer = newPlayer;
[newPlayer release];
[loopSoundPlayer setDelegate:self];
[loopSoundPlayer setNumberOfLoops:0];
[loopSoundPlayer prepareToPlay];


Example of [AVAudioPlayer initWithContentsOfURL].
NSString *path = [[NSBundle mainBundle] pathForResource:@"PIC10"
ofType:@"mp3"];
        AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        theAudio.delegate = self;
        [theAudio play];


AVAudioPlayer initWithContentsOfURL example.
                if ([resourceURL isFileURL]) {

                        [[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback error:nil];
                        audioFile.player = [[ AVAudioPlayer alloc ]
initWithContentsOfURL:resourceURL error:&error];
                } else {
                        NSData* data = [NSData dataWithContentsOfURL:resourceURL];
                        audioFile.player = [[ AVAudioPlayer alloc ] initWithData:data error:&error];
                }

End of AVAudioPlayer initWithContentsOfURL example article.

Friday, May 31, 2013

NSArray initWithContentsOfURL example in Objective C (iOS).


NSArray initWithContentsOfURL

Initializes a newly allocated array with the contents of the location specified by a given URL.

- (id)initWithContentsOfURL:(NSURL *)aURL

Parameters
aURL
The location of a file containing a string representation of an array produced by the writeToURL:atomically: method.

Return Value of [NSArray initWithContentsOfURL]
An array initialized to contain the contents specified by aURL. Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array. The returned object might be different than the original receiver.

Discussion of [NSArray initWithContentsOfURL]
The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects). The objects contained by this array are immutable, even if the array is mutable.

NSArray initWithContentsOfURL example.
-(void)readPlistFromServerAndSaveToDocs
{
    //save plist from server to docs:
    NSArray *tempArr1 = [[NSArray alloc]initWithContentsOfURL:
                         [NSURL URLWithString:@\"http://dl.dropbox.com/u/4082823/AppsFiles/Black/stores.plist\"]];
    NSString *error;
    NSString *rootPath =
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@\"stores.plist\"];
    NSArray *tmpArr = [NSArray arrayWithArray:tempArr1];
    NSData *tmpData = [NSPropertyListSerialization dataFromPropertyList:tmpArr
                                                                 format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
   
    NSLog(@\"plistPath=%@\",plistPath);
   
    if(tmpData)
    {
        [tmpData writeToFile:plistPath atomically:YES];
    }
   
    else
    {
        NSLog(@\"%@\",error);
    }
}

Example of [NSArray initWithContentsOfURL].
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.warpstudio.se/PizzaItems.plist"]];
    AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {
       
        pArray = [[NSMutableArray alloc] initWithContentsOfURL:request];
       
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {
 

End of NSArray initWithContentsOfURL example article.

Saturday, May 18, 2013

NSString initWithContentsOfURL example ios


initWithContentsOfURL: encoding: error:

Returns an NSString object initialized by reading data from a given URL interpreted using a given encoding.
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
Parameters
url
The URL to read.
enc
The encoding of the file at path.
error
If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
Return Value of [NSString initWithContentsOfURL]
An NSString object initialized by reading data from url. The returned object may be different from the original receiver. If the URL can’t be opened or there is an encoding error, returns nil.
NSString initWithContentsOfURL example 
NSString *myURL = [NSString stringWithString:@"http://mywebsite.com/api-call.php"];
NSURL *url = [[NSURL alloc] initWithString:myURL];
NSString *strResult = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",strResult);
NSString initWithContentsOfURL example 
NSError *error = nil;

NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSString initWithContentsOfURL example 

Thursday, May 9, 2013

NSDictionary initWithContentsOfURL example ios


initWithContentsOfURL:

Initializes a newly allocated dictionary using the keys and values found at a given URL.
- (id)initWithContentsOfURL:(NSURL *)aURL
Parameters
aURL
An URL that identifies a resource containing a string representation of a property list whose root object is a dictionary.
Return Value of [NSDictionary initWithContentsOfURL]
An initialized dictionary—which might be different than the original receiver—that contains the dictionary ataURL, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.
Discussion of [NSDictionary initWithContentsOfURL]
The dictionary representation in the file identified by path must contain only property list objects (NSString,NSData, NSDate, NSNumber, NSArray, or NSDictionary objects). For more details, see Property List Programming Guide. The objects contained by this dictionary are immutable, even if the dictionary is mutable.
Example of [NSDictionary initWithContentsOfURL]
- (void)viewDidLoad
{ 

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

    NSStringEncoding *encoding;
    NSError *error;

    NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:&encoding error:&error];
    [self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
    [myHtml release];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
Example of [NSDictionary initWithContentsOfURL]
- (void)viewDidLoad
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TermsConditions" withExtension:@"html"];

NSStringEncoding *encoding = NULL;
NSError *error;

NSString *myHtml = [[NSString alloc] initWithContentsOfURL:modelURL usedEncoding:encoding error:&error];
[self.TermsWebView loadHTMLString:myHtml baseURL:modelURL];
[myHtml release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Example of [NSDictionary initWithContentsOfURL]
NSURL * plist = [NSURL URLWithString: @ "http://www.example.com/example.plist"];
NSDictionary * dict = [[[NSDictionary alloc] initWithContentsOfURL:plist] autorelease];
  if ([dict count] == 0) {
      //no connection
  }