IOS UIFileSharingEnabled,

Source: Internet
Author: User

IOS UIFileSharingEnabled,
1. Enable iOS apps to exchange Documents through iTunes

Enable iOS apps to exchange files through iTunes

Some apps need to use iTunes to allow users to upload and download documents. To enable iOS programs to support iTunes file exchange,

You only needInfo. plistAdd a key:UIFileSharingEnabled(Application supports iTunes file sharing), Assign valuesYES.

In this way, after compilation, when running on the machine, connect the device to iTunes for file exchange.

If the test is performed in iOS Simulator, you can put the file in the "user directory" of the application.Documents. You need to know the path of the user directory under OSX,

YesNSLog (@ "% @", NSHomeDirectory ());Run it and you will know,

The path of the user's home directory under OSX is similar to this:

/Users/venj/Library/Application Support/iPhone Simulator/4.3.2/Applications/158C149B-FF57-4C62-AEDB-DFB7A3BD8AFB.

I made a simple program for testing. When running in Simulator, I put the file under the user directory, which is the content of the user directory under OSX,

Put the fileDocumentsYou can:

Then, test whether the file is successfully recognized by the Program:

NSFileManager * manager = [NSFileManager defaultManager]; NSString * dbPath = [[NSHomeDirectory () stringByAppendingPathComponent: @ "events"] stringByAppendingPathComponent: @ "db. sqlite"];

If ([manager fileExistsAtPath: dbPath]) {self. navigationItem. title = @ "Ready To Go";} else {self. navigationItem. title = @ "No DB File ";}

The following is the running status of the program before and after adding files:

 

Ii. Methods for transferring apps and iTunes files and descriptions of iOS App file structure

Just like many player apps on iOS, a program can be written in this article to put files in it through iTunes. For example, a music player program can be written to put song files in it through itune, then, I used this App to play this song. First, I created a common SingleView program called filebench demo. The following is how to transfer files to the App through iTunes:

But by default, the created App cannot appear in the left sidebar. You need to add a line in plist:

The next task is to determine whether the data has been imported. I wrote the following code in viewdidload:

NSFileManager * manager = [NSFileManager defamanager manager];

NSString * path = [[NSHomeDirectory () stringByAppendingPathComponent: @ "Documents"] stringByAppendingPathComponent: @”2.mp3 "];

If ([manager fileExistsAtPath: path]) {

ShowLabel. text = @ "have file ";

} Else {

ShowLabel. text = @ "have no file ";

}

NSLog (@ "NSHomeDirectory % @", NSHomeDirectory ());

Here are some notes: First NSHomeDirectory () is printed

/Var/mobile/Applications/8f01293c-c02d-4ef8-a9f7-1a7b42444f33

This is the program path. 8f01293c-c02d-4ef8-a9f7-1a7bda-44f33 is a unique tag of each App. For example, angry birds may be another string of information. What is the specific cause. Then I added/Documents and/2.mp3 at the end. I used [manager fileExistsAtPath: path] to determine whether the path does not exist and printed it with a label. If you drag it in, you can see that the label content is "have file ", you can view/var/mobile/Applications/8f01293c-c02d-4ef8-a9f7-1a7b42444f33/Documents/2.mp3 through the label information of the program running.

Then we can use this code to play the song:

NSError * err;

Player = [[AVAudioPlayer alloc]

InitWithContentsOfURL: [NSURL fileURLWithPath: path]

Error: & err];

Player. volume = 0.7;

[Player play];

The previous path can be used directly here.

The structure of the file is as follows:

Each program will contain these files. Documents is the place where Documents imported from iTunes are stored, and file1_demo is the Xcode project package. If you use Xcode to store materials and everything in the project, the last two do not know what specific functions are.

Note:

If the app does not involve document upload and download, Application supports iTunes file sharing should be set to NO or delete this key, otherwise it will be rejected.

 

Reference link:

Https://my.oschina.net/hmj/blog/112592

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.